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

    I am trying to currently figure out how to automatically add a user to a certain usergroup, which can then be "checked" to see if a user has permission to do a certain action & whatnot. What I'm currently looking at doing in the grand scheme of things is to make a system which lets users "purchase" a usergroup upgrade (which is masquerading as a "template/download purchase") and then, from this I will now be able to check if that user has permission through php, and from that, display a download link if they do have the permission.

    The part which I'm expecting to need the most help with would be the script that adds a user to the user group upon completion of a paypal purchase - I'm going to be reading up paypal's API soon and working with their sandbox, but I have no idea how to do this.

    On a side note - if there is a better way of implementing these permissions, please do let me know. I'm trying to make something similar to Xenforo's Account upgrades.

    Thanks,
    Adam
    • Once you get the success return from PayPal, it isn't difficult to add the user to a group.
      $modx->user->joinGroup('UserGroupNameOrId','OptionalRoleNameOrId');}

      You do not need to use $user->save() afterwards, as the joinGroup does not make any changes to the $user object itself.

      http://rtfm.modx.com/revolution/2.x/administering-your-site/security/users
      http://rtfm.modx.com/revolution/2.x/administering-your-site/security/user-groups
        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
        • 43432
        • 19 Posts
        Quote from: sottwell at Jun 17, 2014, 06:26 PM
        Once you get the success return from PayPal, it isn't difficult to add the user to a group.
        $modx->user->joinGroup('UserGroupNameOrId','OptionalRoleNameOrId');}

        You do not need to use $user->save() afterwards, as the joinGroup does not make any changes to the $user object itself.

        http://rtfm.modx.com/revolution/2.x/administering-your-site/security/users
        http://rtfm.modx.com/revolution/2.x/administering-your-site/security/user-groups

        So is OptionalRoleNameOrId the users' username that will be promoted?

        Edit: I checked out the pages you linked to, and that does not appear to be it -
            $user->joinGroup('UserGroupNameOrId','OptionalRoleNameOrId');}
        

        So for the username I just make "$user" have the assigned value of the username that purchased the product, and then I run it to that desired group? [ed. note: muffinjello last edited this post 9 years, 10 months ago.]
        • Yes. the "OptionalRoleNameOrId" is optional, and is to indicate the role in the assign group. Roles are a rather obscure topic. http://rtfm.modx.com/revolution/2.x/administering-your-site/security/roles

          The $modx->user object is the current user; if you need to specify a user you first have to create the $user object.
          $user = $modx->getObject('modUser', array('username'=>$username));
          $user->joinGroup($joingroup);
          [ed. note: sottwell last edited this post 9 years, 10 months ago.]
            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
            • 22019
            • 390 Posts
            Bit rusty as I haven't really touched MODX/Paypal for a couple of years, but just to let you know it's definitely possible. I used Angell Eye PHP libraries to do the standard work with paypal, then on successful subscribe moved my users from one group to another (guests to members, if memory serves). This is then used to display (or not) links to premium content. By using the libraries/listener I also had a mechanism to unsubscribe them and move them back the other way.
              Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
              • 43432
              • 19 Posts
              Quote from: sottwell at Jun 17, 2014, 09:18 PM
              Yes. the "OptionalRoleNameOrId" is optional, and is to indicate the role in the assign group. Roles are a rather obscure topic. http://rtfm.modx.com/revolution/2.x/administering-your-site/security/roles

              The $modx->user object is the current user; if you need to specify a user you first have to create the $user object.
              $user = $modx->getObject('modUser', array('username'=>$username));
              $user->joinGroup($joingroup);
              So, sottwell, from
              $user = $modx->getObject('modUser', array('username'=>$username));
              $user->joinGroup($joingroup);

              it is correct to infer that the "user object" is initialised for the username that is specified with "$username", and thus, the user object is that of a user of my own choice, rather than that of the current / visiting user.

              Also, from using the external initialization (http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/loading-modx-externally ) the following code will allow me to use the MODx functions/classes that we're using here, from a .php file located in a sub-directory of my MODx installation?

              require_once '/absolute/path/to/modx/config.core.php';
              require_once MODX_CORE_PATH.'model/modx/modx.class.php';
              $modx = new modX();
              $modx->initialize('web');
              $modx->getService('error','error.modError', '', '');


              On a side note, does this mean that if anyone knew where my config.core.php file was located, they could run MODx functions from their own php scripts, and change things like permissions of accounts, etc.

              Thanks again for the help! [ed. note: muffinjello last edited this post 9 years, 10 months ago.]
              • If someone has that kind of access to your server, then you have much bigger problems.
                  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
                  • 43432
                  • 19 Posts
                  Quote from: sottwell at Jun 18, 2014, 04:25 AM
                  If someone has that kind of access to your server, then you have much bigger problems.

                  I guess I do, hahaha.