Get Groups of a user

42 views
Skip to first unread message

Timotheus Titus

unread,
May 13, 2014, 4:16:15 AM5/13/14
to joomla-de...@googlegroups.com
Hi there,

I would like to know, how I can get the usergroups a user is member of.

So far I used

                    $user = JFactory::getUser();         
                    $usergroups = $user->getAuthorisedGroups();
                    foreach($usergroups as $usergroup) :
                    echo $usergroup."<br />";
                    endforeach;               



But this gets all groups, the user is authorised to. This is the output for the superuser:

1
6
8


(1: Public, 6: Manager, 8: Super Users)



The member is infact only member of the group "Super Users", so I would like to have only the output "Super Users". How can I do that?

Tuan Pham Ngoc

unread,
May 13, 2014, 4:19:51 AM5/13/14
to joomla-de...@googlegroups.com
Hi

You can use:

$user->get('groups');

That should do what you want.

Timotheus Titus

unread,
May 13, 2014, 8:04:58 AM5/13/14
to joomla-de...@googlegroups.com
Hello again :-)

That already works fine :-) Thank you!

At the moment I try to use the Usergroups Field (http://docs.joomla.org/Usergroup_form_field_type).

In my component there is an item, which is linked to an user-id. In this item-view I want to show the usergroups of the assigned user. Additionally I would like to save changes to the usergroups-mapping. If I select an item which is linked to user-id "123" I want to display the usergroups of user "123" in the Usergroups field. If I add another usergroup to the field I would like to save this changes to the usergroups-mapping.


How could I solve that?

Thanks in advance :-)

Timotheus Titus

unread,
May 14, 2014, 10:53:03 AM5/14/14
to joomla-de...@googlegroups.com
Hi there,

I solved it by overloading the store()-method:

http://d.pr/n/uAaI


Now the usergroups are saved correctly.



But now I want to load the usergroups to the field usergroups when the view renders. How can I do that?


Am Dienstag, 13. Mai 2014 10:16:15 UTC+2 schrieb Timotheus Titus:

Glenn Arkell

unread,
May 14, 2014, 5:12:34 PM5/14/14
to joomla-de...@googlegroups.com
Hi,

I tend to use the API JAccess::getGroupsByUser.  This returns all the groups that the user is mapped to.

ie $groups = JAccess::getGroupsByUser($userid);

Then $groups is an array of the groups that user is mapped to.  More info at http://api.joomla.org/cms-3/classes/JAccess.html

Hope this helps.  Cheers.
Reply all
Reply to author
Forward
0 new messages