We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Ok, after cleaning up and re-creating some groups, your unset($_SESSION...) line does the trick.
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 3749
      • 24,544 Posts
      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 9 years, 4 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • 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);
        }
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org