Hello I have a plugin that will run for logged manager users on OnManagerPageBeforeRender.
I need the code check for the current user ROLE base on that I’ll do some stuff but can’t get the current user role name, can anyone help me?
Thanks!
You can take a look at how it’s done in the security/profile/get
processor: https://github.com/modxcms/revolution/blob/e59f4e0de39adf5b3091ff0281d3062f4cdadb2f/core/model/modx/processors/security/profile/get.class.php#L63
In a plugin, I think that the $modx object is available, through which you can retrieve the current user as a modUser object with the method $modx->getUser().
Instead of duplicating code that is specific to the current implementation, you might prefer to use the MODX API. You can use the getUserGroups() method of the modUser object to get a list of IDs of the groups the user is defined in, and the getUserGroupNames() method to get a list of the names of these groups.
$currentUser = $modx->getUser();
$groupIdsList = $currentUser->getUserGroups();
$groupNamesList = $currentUser->getUserGroupNames()
It might be that the user is already defined in a variable $user when the plugin is called, so you might want to test if the first line is necessary.
This approach will be more future proof as it is not dependant on a specific implementation of the MODX API.
You can see the API definition at https://api.modx.com/revolution/2.2/db_core_model_modx_moduser.class.html#\modUser::getUserGroups() and https://api.modx.com/revolution/2.2/db_core_model_modx_moduser.class.html#\modUser::getUserGroupNames()
For getUser(). https://docs.modx.com/current/en/extending-modx/modx-class/reference/modx.getuser
Thanks mate, yes at the end I didn’t use the role to define the group I just create a new user group and make the search based on.
$groupIdsList = $currentUser->getUserGroups();
I don’t know why the role is so complicate to find. But thank for your help mate.
Cheers!
I tend to not use roles much (I give everyone in a user group the same role, with the possible exception of the admin super user). If users should have different capabilities, I put them in different groups. It makes life much easier.
I just realise that!
Thanks Bob!