MODX 2.8.4: How to get user group in templates?

Hello,

with [[!+modx.user.id]] modx delivers the user id of current user.
And with :userinfo=email for example I can get email adress without using a custom snippet.

Is there a trick to get the usergroups?

Thank you in advance.

bye
Chris

I don’t think this is possible with an output modifier.
You probably have to write a custom snippet.


There is an output modifier to check if a user is in a certain group,

[[!+modx.user.id:memberof=`Administrator`]]

but that is probably not what you want.

Sounds good. But what is the output of memberof?

I believe it’s either 1 (the user is in the group) or 0.

ok, this is what i expected too. But I only get the userid in all cases. :thinking:

Oh, I tried:

[[!+modx.user.id:memberof=Administrator:then=Yes:else=No]]

this runs…

many thanks :pray:

additional question:

If I programmatically change the user group, how could I refresh permissions for this user, so that it s active?

I think if you use $user->joinGroup({groupName or id}); that will happen automatically. If not, something like this should do it:

$modx->user->loadAttributes(array(), 'web', true);

BTW, it sounds like you’ve solved your problem, but if you need to list or retrieve all the user’s user groups, this will do it for the current user (this is for others who find this page when searching):

$output = "";
$memberGroups = $modx->user->getMany('UserGroupMembers', 
      array('member' => $modx->user->get('id')));

foreach($memberGroups as $memberGroup) {
    $group = $memberGroup->getOne('UserGroup');
    $output .= "<br>" . $group->get('name');
}
return $output;

oh, that is fine.

thank you very much. :pray:

The following two methods on modUser are useful as well:

  • getUserGroups()
  • getUserGroupNames()

@joshualuckers Doh!. Thanks for being so polite about it. For most use cases, those are much better approaches than the one I suggested. :wink: