Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Gurumurthy Godlaveeti
How to get all roles of a User ?
August 27, 2012 10:36 PM
Answer

Gurumurthy Godlaveeti

Rank: Regular Member

Posts: 209

Join Date: August 11, 2011

Recent Posts

Hello There ,

How to get all roles (organization roles,community roles , regular roles) of a specific User ?

I tried to use
RoleLocalServiceUtil.getUserRoles(userId)
, so it's giving only regular roles . and again to get organization roles ,
RoleLocalServiceUtil.getUserGroupRoles(userId,scopeGroupId)
but is there any one particular method which gives all roles of a user ?

Note :- I used
themeDisplay.getUser().getRoles()
method also , it is just giving regular roles only .

My Liferay version is 6.0.6 .

Thanks ,
Gurumurthy .G
Jignesh Vachhani
RE: How to get all roles of a User ?
August 27, 2012 11:20 PM
Answer

Jignesh Vachhani

Rank: Liferay Master

Posts: 785

Join Date: March 10, 2008

Recent Posts

Hi Guru

To get regular you can use
RoleLocalServiceUtil.getUserRoles(userId); OR

To get community roles :

RoleLocalServiceUtil.getUserGroupRoles(userId,groupId)
Gurumurthy Godlaveeti
RE: How to get all roles of a User ?
August 27, 2012 10:56 PM
Answer

Gurumurthy Godlaveeti

Rank: Regular Member

Posts: 209

Join Date: August 11, 2011

Recent Posts

Hello Jignesh ,

I used <liferay-theme:defineObjects /> for themeDispaly object in JSP .

So through themeDispaly object , there was no getUserRoles(userId) method .


Liferay Version is 6.0.6 CE .

Thanks ,
Gurumurthy .G
Vishal Panchal
RE: How to get all roles of a User ?
August 27, 2012 11:07 PM
Answer

Vishal Panchal

Rank: Expert

Posts: 280

Join Date: May 20, 2012

Recent Posts

Gurumurthy Godlaveeti:
Hello Jignesh ,

I used <liferay-theme:defineObjects /> for themeDispaly object in JSP .

So through themeDispaly object , there was no getUserRoles(userId) method .


Liferay Version is 6.0.6 CE .

Thanks ,
Gurumurthy .G



Hi ,Gurumurthy Godlaveeti

Jignesh is right but in your case,

You can also use following code in your jsp file for accessing roles of a particular user.

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-theme:defineObjects /> <portlet:defineObjects />
<liferay-ui:user-display userId="<%= user.getRoles() %>" />


Thanks & Regards,
Vishal R. Panchal
Jignesh Vachhani
RE: How to get all roles of a User ?
August 27, 2012 11:16 PM
Answer

Jignesh Vachhani

Rank: Liferay Master

Posts: 785

Join Date: March 10, 2008

Recent Posts

Sorry Guru,

By mistake I wrote themDisplay,

Please check the above thead post again.
Gurumurthy Godlaveeti
RE: How to get all roles of a User ?
September 2, 2012 11:57 PM
Answer

Gurumurthy Godlaveeti

Rank: Regular Member

Posts: 209

Join Date: August 11, 2011

Recent Posts

Hello Jignesh ,

It's giving only regular roles only , not included organisation roles of a user .
1
2userRoles = RoleLocalServiceUtil.getUserRoles(userId);    


I want to get Organization roles of a user also along with regular roles , i mean i want to get regular roles + organization roles of user with single method ?
Sampsa Sohlman
RE: How to get all roles of a User ?
September 3, 2012 5:58 AM
Answer

Sampsa Sohlman

LIFERAY STAFF

Rank: Regular Member

Posts: 225

Join Date: September 27, 2007

Recent Posts

So what exactly you are doing, that you need single method for this?
Gurumurthy Godlaveeti
RE: How to get all roles of a User ?
September 3, 2012 6:14 AM
Answer

Gurumurthy Godlaveeti

Rank: Regular Member

Posts: 209

Join Date: August 11, 2011

Recent Posts

Yes Mr.Sampsa Sohlman .
Sampsa Sohlman
RE: How to get all roles of a User ?
September 3, 2012 8:27 AM
Answer

Sampsa Sohlman

LIFERAY STAFF

Rank: Regular Member

Posts: 225

Join Date: September 27, 2007

Recent Posts

Mr Gurumurthy Godlaveeti, what is your the point?

Try following

1    public static List<Role> findAllRolesForUser(User user) throws SystemException, PortalException {
2        List<Role> roles = new ArrayList<Role>();;
3        roles.addAll(RoleLocalServiceUtil.getUserRoles(user.getUserId()));
4        roles.addAll(RoleLocalServiceUtil.getUserRelatedRoles(user.getUserId(), user.getGroupIds()));
5        return roles;
6    }


FYI, I did not test the code anyway.
Leon Fleysher
RE: How to get all roles of a User ?
December 16, 2013 6:29 AM
Answer

Leon Fleysher

Rank: New Member

Posts: 5

Join Date: February 24, 2013

Recent Posts

I'm not sure there is a single method.
I have partial solution for the roles of the user and all roles of user groups to which this user belongs:
Here is the code snippet that worked for me:
 1
 2...
 3        List<Role> roles = new ArrayList<Role>();
 4        roles.addAll(user.getRoles());
 5        roles.addAll(getUserGroupRolesOfUser(user));
 6        roles.addAll(getUserExplicitRoles(user));
 7...
 8private static List<Role> getUserExplicitRoles(User user) throws SystemException, PortalException {
 9        List<Role> roles = new ArrayList<Role>();
10        List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(user.getUserId());
11        for (UserGroupRole userGroupRole : userGroupRoles) {
12            roles.add(userGroupRole.getRole());
13        }
14        return roles;
15    }
16
17private static List<Role> getUserGroupRolesOfUser(User user) throws SystemException, PortalException {
18        List<Role> roles = new ArrayList<Role>();
19        List<UserGroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(user.getUserId());
20        List<UserGroupGroupRole> userGroupGroupRoles = new ArrayList<UserGroupGroupRole>();
21        for (UserGroup userGroup : userGroupList) {
22            userGroupGroupRoles.addAll(UserGroupGroupRoleLocalServiceUtil.getUserGroupGroupRoles(userGroup
23                .getUserGroupId()));
24        }
25        for (UserGroupGroupRole userGroupGroupRole : userGroupGroupRoles) {
26            Role role = RoleLocalServiceUtil.getRole(userGroupGroupRole.getRoleId());
27            roles.add(role);
28        }
29        return roles;
30    }