We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6902
    • 126 Posts

    I’m setting up a paid membership for a site, and would like to have a snippet that is able to change a user’s role automatically (to allow access to more pages) upon a response of successful payment from the payment gateway.

    Is this possible? If so, how can I do it?...
      • 3749
      • 24,544 Posts
      It's definitely possible, but it's not that simple in Revolution.

      In Evolution, users have only one role. In Revolution, user's can have a different role in each user group they belong to.

      Assuming that you know the ID of the user group, the ID of the user and the ID of the user's current role in that group and the ID of the role you want to set, it would look something like this:


      <?php
      $userGroupMember = $modx->getObject('modUserGroupMember', array('user_group'=>$userGroupId, 'member'=>$userId, 'role'=>$oldRole));
      
      $userGroupMember->set('role', $newRole);
      
      $userGroupMember->save();
      


      The role IDs are listed in the Manager and you can get the user and group ID's like this:

      <?php
      $groupName = 'Editors';
      $userName = 'JoeBlow';
      $userGroup = $modx->getObject('modUserGroup', array('name'=> $groupName));
      $userGroupId = $userGroup->get('id');
      
      $user = $modx->getObject('modUser', array('username'=> $userName));
      $userId = $user->get('id');
      


      A whole different approach would be to add the user to another group rather than changing the user's role and give that group more rights. Then you could simply do this:

      <?php
      $userName = 'JoeBlow';
      $user = $modx->getObject('modUser', array('username'=> $userName));
      $user->joinGroup($group, $role);
      


      In the code above, the $group and $role variables can contain either the ID or the name of the group or role. [ed. note: BobRay last edited this post 8 years, 9 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
        • 6902
        • 126 Posts
        I gotta say that all of that was pretty dang helpful! Based on your feedback, I switched it from a role in a single group, to different groups altogether. Much easier. Thanks!
          • 3749
          • 24,544 Posts
          Glad you got it sorted. Thanks for reporting back. 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
            • 38290
            • 712 Posts
            I have a usergroup with an ID of 1 and a user with an ID of 1 (defaults) but I'm getting this

            $userGroupMember = $modx->getObject('modUserGroupMember', array('usergroup'=>1, 'member'=>1));


            Error 42S22 executing statement: 
            Array
            (
                [0] => 42S22
                [1] => 1054
                [2] => Unknown column 'modUserGroupMember.usergroup' in 'where clause'
            )
            


            I think it needs to be 'user_group'.
            $userGroupMember = $modx->getObject('modUserGroupMember', array('user_group'=>1, 'member'=>1));
              jpdevries
              • 3749
              • 24,544 Posts
              Definitely. http://bobsguides.com/modx-object-quick-reference.html#modUserGroupMember

              Thanks for reporting that (fixed above).
                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