We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36763
    • 70 Posts
    Hello all,

    I was just curious of the best way to get a list/array of the usergroups a user is in? I'm working to be able to integrate this in with a project, so any full php examples would be great. smiley

    At the moment when I print_r($_SESSION) I get two entries on the bottom of that list displaying the following:

    [modx.user.2.userGroups] => Array
            (
                [0] => 7
                [1] => 8
            )



    Is this "[modx.user.2.userGroups]" a constant, or is the 2 based on the user's (who is logged in) id?

    What is considered to be the best practice at this stage? I have had a look at http://www.shawnwilkerson.com/ and it seems all good and well, but is this kind of thing integrated in to the API core yet?

    (And yes, although I would consider myself a competent php programmer, I still have lots to learn - especially with this xPDO and OOP). smiley

    Thanks!
    -Steve [ed. note: vrdriver last edited this post 13 years, 1 month ago.]
      • 3749
      • 24,544 Posts
      I'm sure there's a way to get them with fewer database queries using joins, but this should do it:

      /* Get the modUserGroupMember objects */
      /* $user must be a User object */
      
      $ugms = $user->getMany('UserGroups');
      
      $groupNames = array();
      foreach ($ugms as $ugm) {
         /* Get the modUrderGroupMember's User Group */
         $group = $ugm->getOne('UserGroup');
      
         /* Store the group Name in $groupNames */
         $groupsNames[] = $group->get('name');
      }
      
      echo "\n<pre>" . print_r($groups, true);
        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
        • 40706
        • 128 Posts
        There are also these two modUser Methods:

        getUserGroupNames( ) :
        Gets all the User Group names of the groups this user belongs to.

        getUserGroups( ) :
        Gets all the User Group IDs of the groups this user belongs to.

        which would just be something like that
        foreach($modx->user->getUserGroupNames( ) as $group) {
            echo "<li>{$group}</li>";
        }
        
          • 36763
          • 70 Posts
          Gentlemen, thank you very much! Both methods worked very well. smiley
            • 3749
            • 24,544 Posts
            Thanks Michael, I forgot about those two methods, which are obviously much easier and probably more efficient.
              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
              • 36763
              • 70 Posts
              Hi guys,

              Further to my last questions, I'm taking this a little further and maybe for good reason...

              Would this be a good way (or hack) of doing the same thing too? I don't know if there are many other weird cross referenced tables there needed for the permissions so I'm not 100% sure this is a good idea.

              SELECT * FROM modx_member_groups, modx_membergroup_names WHERE user_group = modx_membergroup_names.id AND
              modx_member_groups.member = 1


              The reason I ask, is that as I'm building an application, and I want to be able to use the a <select>, <option> dropdown in html and have the correct user group for the ids, and have those match up with the fields that are being edited by the user, so the correct records are stored with the correct credentials so the correct users can see only the records associated with them.

              I know it must sound complicated, but I get these results with the current query as above.

              id,user_group,member,role,rank,id,name,description,parent,rank,dashboard
              10,1,1,2,0,1,Administrator,NULL,0,0,1
              12,8,1,1,0,8,"Business Sample 1","This is for business 1",0,0,1


              So basically this is what I'm looking to output eventually: eg.

              <select id="usergroups">
              <option value="1">Adminstrator</option>
              <option value="8">Business...</option>
              </select>


              The current built-in api functions don't seem to have this built in... and it's either one or the other - not combined?


              What I'm scared of doing is using an array feature with getUserGroups and getUserGroupNames and storing them such as
              Userdata_name[0] = "Admin"
              Userdata_number[0] ="1"
              Userdata_name[1] = "Business..."
              Userdata_number[1] "8"

              and not have them come out in the same correct order where maybe the 8 and the 1 get swapped or similar (especially when there are more than 2 groups).

              Are these fair questions, or am I going about this the wrong way?
              Thanks again. -Steve
                • 42046
                • 436 Posts
                Just to chime in with something, pdoTools now contains a snippet called pdoUsers which is like getResources for users/usergroups.

                http://modx.com/extras/package/pdotools
                http://store.simpledream.ru/packages/utilities/pdotools.html#tab=properties