We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15566
    • 73 Posts
    I have managed to return the user’s information by using the API like this:

    $profile = $modx->user->getOne('Profile');


    Following this I want to the get the user’s group role to find out if they are allowed to see certain parts of my snippet. I have tried all of the following but they don’t work

    $profile->getOne('UserRole');
    $profile->getOne('modUserRole');
    $profile->getMany('UserRole');
    $profile->getMany('modUserRole');


    The getMany() lines return empty arrays, getOne() returns nothing. I would presume getMany() is likely to be correct as a user could be long to multiple groups and therefore multiple roles.

    The user I am testing it on has the role of ’Moderator’ within a user group. I don’t need to know which group they are in, just the role (name or id) within it. Reason: All Moderators can update elements regardless of the group they are in.

    Any idea? I have trawled the forums, the web and looked through Bob’s Guides but I haven’t found the answer as yet.

    Any Ideas?
      • 28215
      • 4,149 Posts
      $c = $modx->newQuery('modUserGroupMember');
      $c->innerJoin('modUserGroupRole','UserGroupRole');
      $c->where(array(
         'UserGroupRole.name' => 'Moderator',
         'modUserGroupMember.member' => $modx->user->get('id'),
      ));
      $membership = $modx->getObject('modUserGroupMember');
      if ($membership) {
         /* they're a member, do code. */
      }
      


      should work.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 15566
        • 73 Posts
        It works, but with a small tweak:

        $c = $modx->newQuery(’modUserGroupMember’);
        $c->innerJoin(’modUserGroupRole’,’UserGroupRole’);
        $c->where(array(
        ’UserGroupRole.name’ => ’Moderator’,
        ’modUserGroupMember.member’ => $modx->user->get(’id’),
        ));
        $membership = $modx->getObject(’modUserGroupMember’, $c);
        if ($membership) {
        /* they’re a member, do code. */
        }

        Thanks for you help.
          • 38290
          • 712 Posts
          I'm trying to do something similar to this, but I'm trying to get the numeric ID for the role a user is in a given user group. This is being used for a duplicateusergroup.php script.
          https://gist.github.com/jpdevries/7c17dce93df93ca932e2
            jpdevries
            • 38290
            • 712 Posts
            Got by with a little help from BobRay
            https://forums.modx.com/thread/30865/change-user-s-role-in-snippet#dis-post-528516

            I'm using this
                    $userGroupMember = $modx->getObject('modUserGroupMember', array('user_group'=>$userGroup, 'member'=>$userID));
                    $role = $userGroupMember->get('role');
            
              jpdevries