MODX Community

Get current manager user Role

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?

You can take a look at how it’s done in the security/profile/get processor:

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\modUser::getUserGroups() and\modUser::getUserGroupNames()

For 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.

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!