<![CDATA[ Script to automatically add a user to a certain usergroup. - MODX Community Forums]]> https://forums.modx.com/thread/?thread=91586 <![CDATA[Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500665
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]]>
muffinjello Jun 17, 2014, 11:06 AM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500665
<![CDATA[Re: Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500715 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.]]>
muffinjello Jun 17, 2014, 11:47 PM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500715
<![CDATA[Re: Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500711 sottwell Jun 17, 2014, 11:25 PM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500711 <![CDATA[Re: Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500701 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!]]>
muffinjello Jun 17, 2014, 05:35 PM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500701
<![CDATA[Re: Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500700 odeclas Jun 17, 2014, 05:09 PM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500700 <![CDATA[Re: Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500698 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);
]]>
sottwell Jun 17, 2014, 04:18 PM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500698
<![CDATA[Re: Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500691 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?]]>
muffinjello Jun 17, 2014, 02:36 PM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500691
<![CDATA[Re: Script to automatically add a user to a certain usergroup.]]> https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500677
$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]]>
sottwell Jun 17, 2014, 01:26 PM https://forums.modx.com/thread/91586/script-to-automatically-add-a-user-to-a-certain-usergroup#dis-post-500677