Foros de discusión

How to get all roles of a User ?

thumbnail
Gurumurthy Godlaveeti, modificado hace 11 años.

How to get all roles of a User ?

Regular Member Mensajes: 208 Fecha de incorporación: 12/08/11 Mensajes recientes
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
thumbnail
Jignesh Vachhani, modificado hace 11 años.

RE: How to get all roles of a User ?

Liferay Master Mensajes: 803 Fecha de incorporación: 10/03/08 Mensajes recientes
Hi Guru

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

To get community roles :

RoleLocalServiceUtil.getUserGroupRoles(userId,groupId)
thumbnail
Gurumurthy Godlaveeti, modificado hace 11 años.

RE: How to get all roles of a User ?

Regular Member Mensajes: 208 Fecha de incorporación: 12/08/11 Mensajes recientes
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
thumbnail
Vishal Panchal, modificado hace 11 años.

RE: How to get all roles of a User ?

Expert Mensajes: 289 Fecha de incorporación: 20/05/12 Mensajes recientes
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
thumbnail
Jignesh Vachhani, modificado hace 11 años.

RE: How to get all roles of a User ?

Liferay Master Mensajes: 803 Fecha de incorporación: 10/03/08 Mensajes recientes
Sorry Guru,

By mistake I wrote themDisplay,

Please check the above thead post again.
thumbnail
Gurumurthy Godlaveeti, modificado hace 11 años.

RE: How to get all roles of a User ?

Regular Member Mensajes: 208 Fecha de incorporación: 12/08/11 Mensajes recientes
Hello Jignesh ,

It's giving only regular roles only , not included organisation roles of a user .

userRoles = 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 ?
thumbnail
Sampsa Sohlman, modificado hace 11 años.

RE: How to get all roles of a User ?

Regular Member Mensajes: 230 Fecha de incorporación: 27/09/07 Mensajes recientes
So what exactly you are doing, that you need single method for this?
thumbnail
Gurumurthy Godlaveeti, modificado hace 11 años.

RE: How to get all roles of a User ?

Regular Member Mensajes: 208 Fecha de incorporación: 12/08/11 Mensajes recientes
Yes Mr.Sampsa Sohlman .
thumbnail
Sampsa Sohlman, modificado hace 11 años.

RE: How to get all roles of a User ?

Regular Member Mensajes: 230 Fecha de incorporación: 27/09/07 Mensajes recientes
Mr Gurumurthy Godlaveeti, what is your the point?

Try following

	public static List<role> findAllRolesForUser(User user) throws SystemException, PortalException {
		List<role> roles = new ArrayList<role>();;
		roles.addAll(RoleLocalServiceUtil.getUserRoles(user.getUserId()));
		roles.addAll(RoleLocalServiceUtil.getUserRelatedRoles(user.getUserId(), user.getGroupIds()));
		return roles;
	}
</role></role></role>


FYI, I did not test the code anyway.
thumbnail
Leon Fleysher, modificado hace 10 años.

RE: How to get all roles of a User ?

New Member Mensajes: 5 Fecha de incorporación: 24/02/13 Mensajes recientes
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:

...
        List<role> roles = new ArrayList<role>();
        roles.addAll(user.getRoles());
        roles.addAll(getUserGroupRolesOfUser(user));
        roles.addAll(getUserExplicitRoles(user));
...
private static List<role> getUserExplicitRoles(User user) throws SystemException, PortalException {
        List<role> roles = new ArrayList<role>();
        List<usergrouprole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(user.getUserId());
        for (UserGroupRole userGroupRole : userGroupRoles) {
            roles.add(userGroupRole.getRole());
        }
        return roles;
    }

private static List<role> getUserGroupRolesOfUser(User user) throws SystemException, PortalException {
        List<role> roles = new ArrayList<role>();
        List<usergroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(user.getUserId());
        List<usergroupgrouprole> userGroupGroupRoles = new ArrayList<usergroupgrouprole>();
        for (UserGroup userGroup : userGroupList) {
            userGroupGroupRoles.addAll(UserGroupGroupRoleLocalServiceUtil.getUserGroupGroupRoles(userGroup
                .getUserGroupId()));
        }
        for (UserGroupGroupRole userGroupGroupRole : userGroupGroupRoles) {
            Role role = RoleLocalServiceUtil.getRole(userGroupGroupRole.getRoleId());
            roles.add(role);
        }
        return roles;
    }

</usergroupgrouprole></usergroupgrouprole></usergroup></role></role></role></usergrouprole></role></role></role></role></role>