2
votes

I wanted to know the list of site role names assigned for the user. So I tried as follows,

List<Role> userRolesList = RoleLocalServiceUtil.getUserRoles(userid);
                if (userRolesList != null) {
            for (Role role : userRolesList) {  
                    System.out.println("rolesID:"+ role.getTypeLabel());
                }
            }

I am able to see the user regular type roles only. Not site types. But in my case the user is a site administrator. So How can I get the users site roles names by using api calls?

2

2 Answers

5
votes

RoleLocalServiceUtil will return you regular roles only. To get group / site roles you need to use getUserGroupRoles(long userId, long groupId) of UserGroupRoleLocalServiceUtil as following:

List<UserGroupRole> userGroupRoleList =
    UserGroupRoleLocalServiceUtil.getUserGroupRoles(userId, groupId);
    if (userGroupRoleList != null) {
    for (UserGroupRole userGroupRole : userGroupRoleList) {
        /* Get Role object based on userGroupRole.getRoleId() */
        Role role = RoleLocalServiceUtil.getRole(userGroupRole.getRoleId());
        System.out.println("roleId : " + role.getRoleId());
        System.out.println("roleName : " + role.getName());
    }
}
2
votes

Make use of com.liferay.portal.service.UserGroupRoleLocalServiceUtil.java's api methods listed below.

 UserGroupRoleLocalServiceUtil.getUserGroupRoles(
        long userId)

UserGroupRoleLocalServiceUtil.getUserGroupRoles(
        long userId, long groupId)

It returns UserGroupRole object from which you can get Role object and hence name of Role.

Using first method you can get all Site Roles assigned to user and using second method you can get all Site Roles assigned to user with particular Site/Group.