-
- 24,544 Posts
Sorry, the code should work in a plugin during the registration process but not in a snippet for the reasons you mention.
If you want to test it in a snippet, you’d have to use $modx->user instead of $user and make sure a user is logged in first.
To actually do what you’re describing, a plugin is definitely the way to go.
-
- 138 Posts
Hi BobRay,
the test-snippet now works very well. Still not working is the plugin. For test-reasons I tried just this simple line:
<?php
$user->joinGroup(’Members’);
The plugin is listen to "OnUserFormSave". But new users are not added to the group "Members" which I defined before. Is there anything I forgot to add?
-
- 24,544 Posts
Sorry, I can’t think of any reason why that wouldn’t work.
-
- 5,888 Posts
Quote from: Letti at Mar 23, 2011, 03:31 PM
Is it possible to assign a user automatically to a certain usergroup depending for example on his name or a number he types in while the register-process or profile-update? All the names and numbers which should be assigned to "Group A" will be stored by us in a database.
Is this feasible?
Letti
what is the reason, they have to register themselves. If you have their names and emails in a database you can add them at once to the user-table and let modx send a mail with their login-details.
example-script:
<?php
$object = $modx->getObject($classname, $scriptProperties['object_id']);
$userfields = array(
'username' => $object->get('name'),
'fullname' => $object->get('name'),
'email' => $object->get('email'),
'groups' => json_encode(array(array('usergroup' => $config['usergroup'],'role' => $config['userrole'])))
, );
$userid=$object->get('userid');
if (empty($userid)) {
//add user to user-table
$user = $modx->runProcessor('security/user/create', array_merge($userfields,array(
'newpassword' => true,
'passwordnotifymethod' => 'e',
'passwordgenmethod' => 'g',
'class_key' => 'modUser',
)));
/* Above creates user np */
if ($user->isError()) {
if ($user->hasFieldErrors()) {
$fieldErrors = $user->getAllErrors();
$errormsg = implode("\n", $fieldErrors);
} else {
$errormsg = 'An error occurred: ' . $user->getMessage();
}
$updateerror = true;
return;
}
$userArray = $user->getObject();
/*This line seems to return an array not an object */
$userid = $userArray['id'];
$object->set('userid', $userid);
$object->save();
}
-
- 138 Posts
We tried it serveral times but the plugin is not activated by the registering-process although we linked the plugin to "OnUserFormSave".
When linking the plugin to "OnUserActivate" for testing it was successful when the registered user was activating his account by clicking on the activation-link.
Do you think this is a bug of MODx (that "OnUserFormSave" doesn´t work)? Or do we maybe have to change any settings in MODx to make it work? Maybe we have to change the "priority" of "OnUserFormSave" (at the moment it is set to 0)?
Letti
-
- 24,544 Posts
Quote from: Letti at Apr 15, 2011, 02:31 PM
We tried it sevreral times but the plugin is not activated by the registering-process although we linked the plugin to "OnUserFormSave".
When linking the plugin to "OnUserActivate" for testing it was successful when the registered user was activating his account by clicking on the activation-link.
Do you think this is a bug of MODx (that "OnUserFormSave" doesn´t work)? Or do we maybe have to change any settings in MODx to make it work? Maybe we have to change the "priority" of "OnUserFormSave" (at the moment it is set to 0)?
Letti
The Register/ConfirmRegister snippets bypass the Manager form and save the user directly. That’s why it doesn’t work when connected to OnUserFormSave. OnActivateUser is fired by the add-ons, so you could connect to that, but then you won’t get users who are bulk activated, created in the Manager, or created/activated by other add-ons.
I would connect it to OnUserBeforeSave, though, which is guaranteed to fire whenever a user object is saved. You can check the $modx variable to see if it’s a new user or an existing user.
-
- 138 Posts
Hi BobRay,
thanks a lot. "OnUserBeforeSave" works very fine. However for some reason it fires twice while the registering process and of course a third time when the user is activating his account. Do you have any idea how to make it firing only once while the initial registering-process? The third firing doesn´t bother us very much...
Letti
-
- 24,544 Posts
If you put this at the top of the code, it will only execute for brand new users:
if ($mode != modSystemEvent::MODE_NEW ) {
return;
}
As an alternative, you could test with this to see if the user is already a member:
$groups = array(
'Editors',
'Managers',
);
if (! $user->isMember($groups) ) {
/* do your stuff here */
}
This will tell you if the user is a member of *all* the listed groups:
if (! $user->isMember($groups, true) ) {
/* do your stuff here */
}