-
☆ A M B ☆
- 24,524 Posts
Ok, after cleaning up and re-creating some groups, your unset($_SESSION...) line does the trick.
Good to hear. joinGroup() should really clear both session variables. I filed a bug report on this.
BTW, if you're sending group and/or role IDs rather than names, it's a good practice to do this:
$user->joinGroup( (int) $groupId, (int) $roleId);
The joinGroup() method uses is_string() to determine whether it's getting a name or an ID, so if it receives '1' it will try to add the user to a group named '1' rather than the group with the ID 1.
[ed. note: BobRay last edited this post 11 years ago.]
-
☆ A M B ☆
- 24,524 Posts
That's good to know. And thank you.
Here's the final working code:
<?php
$groupname = 'Group1';
if(!($modx->user->isMember($groupname))) {
$modx->user->joinGroup($groupname);
$userId = $modx->user->get('id');
unset($_SESSION["modx.user.{$userId}.userGroups"]);
$context = 'web';
$targets = explode(',', $modx->getOption('principal_targets',
null, 'modAccessContext,modAccessResourceGroup,modAccessCategory,sources.modAccessMediaSource'));
array_walk($targets, 'trim');
$modx->user->loadAttributes($targets, $context, true);
}