We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Yes, my suggested fix was in the core file. If it worked, it could be proposed as a bug fix.
      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
      • 10378
      • 375 Posts
      Ah - ok, I'll try this and if it fixes the problem I'll file an issue!
      Thanks Bob!
        Freelancer @bitego http://www.bitego.com
        ---
        GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
        More infos here: http://www.bitego.com/extras/goodnews/
        • 10378
        • 375 Posts
        Issue filed: https://github.com/modxcms/revolution/issues/11883

        As I'd like to support all MODX 2.3 versions, I need to write my own joinGroup method otherwise my upcoming add-on won't work as expected.
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
          • 3749
          • 24,544 Posts
          You should be able to just copy the code from the one in modUser with my additional code above, but also change this line:

          $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'Role not found with key: '.$role);


          to this:

          $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'Role not found with key: '.$roleId);


          The current code will not display the key and can throw a PHP warning.

            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
            • 10378
            • 375 Posts
            FYI: Here is the modified/fixed joinGroup method from moduser.class.php to use in own projects until original method is fixed (all modifications are documented):

            /**
             * Join a User Group, and optionally assign a Role.
             * (This method is borrowed from MODX moduser.class.php and 
             * modified, as the original method has some bugs in 2.3.1.
             * All references to xpdo are replaced with modx.)
             *
             * @access public
             * @param mixed $groupId Either the name or ID of the User Group to join.
             * @param mixed $roleId Optional. Either the name or ID of the Role to assign to for the group.
             * @param modUser $user The user object (Needed because $this is not a modUser object as in moduser.class.php)
             * @return boolean True if successful.
             */
            public function joinGroup($groupId, $roleId = null, $user) {
                $joined = false;
            
                $groupPk = is_string($groupId) ? array('name' => $groupId) : $groupId;
                /** @var modUserGroup $userGroup */
                $userGroup = $this->modx->getObject('modUserGroup', $groupPk);
                if (empty($userGroup)) {
                    $this->modx->log(modX::LOG_LEVEL_ERROR, 'User Group not found with key: '.$groupId);
                    return $joined;
                }
            
                /** @var modUserGroupRole $role */
                if (!empty($roleId)) {
                    $rolePk = is_string($roleId) ? array('name' => $roleId) : $roleId;
                    $role = $this->modx->getObject('modUserGroupRole', $rolePk);
                    if (empty($role)) {
                        //$this->xpod->log(xPDO::LOG_LEVEL_ERROR,'Role not found with key: '.$role);
                        $this->modx->log(modX::LOG_LEVEL_ERROR, 'Role not found with key: '.$roleId); // bugfix
                        return $joined;
                    }
                }
            
                /** @var modUserGroupMember $member */
                $member = $this->modx->getObject('modUserGroupMember', array(
                    //'member' => $this->get('id'), // Original line from moduser.class.php
                    'member' => $user->get('id'),   // Needed because $this is not a modUser object as in moduser.class.php
                    'user_group' => $userGroup->get('id'),
                ));
                if (empty($member)) {
                    $rank = count($user->getMany('UserGroupMembers'));
                    $member = $this->modx->newObject('modUserGroupMember');
                    //$member->set('member', $this->get('id')); // Original line from moduser.class.php
                    $member->set('member', $user->get('id'));   // Needed because $this is not a modUser object as in moduser.class.php
                    $member->set('user_group', $userGroup->get('id'));
                    $member->set('rank', $rank);
                    if (!empty($role)) {
                        $member->set('role', $role->get('id'));
                    }
                    // -- additional code: allow joining User Groups without assigning a role
                    if ($roleId === '0' || $roleId === 0) { 
                        $member->set('role', 0);
                    }
                    // -- end: additional code
                    $joined = $member->save();
                    if (!$joined) {
                        $this->modx->log(modX::LOG_LEVEL_ERROR, 'An unknown error occurred preventing adding the User to the User Group.');
                    } else {
                        //unset($_SESSION["modx.user.{$this->get('id')}.userGroupNames"]); // Original line from moduser.class.php
                        unset($_SESSION["modx.user.{$user->get('id')}.userGroupNames"]);   // Needed because $this is not a modUser object as in moduser.class.php
                    }
                } else {
                    $joined = true;
                }
                return $joined;
            }
            
              Freelancer @bitego http://www.bitego.com
              ---
              GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
              More infos here: http://www.bitego.com/extras/goodnews/
              • 3749
              • 24,544 Posts
              Looks good. smiley
                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