Vinod
Vinod

Reputation: 2311

How can I get the user site roles in liferay

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?

Upvotes: 2

Views: 7738

Answers (2)

Parkash Kumar
Parkash Kumar

Reputation: 4730

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());
    }
}

Upvotes: 7

Pankaj Kathiriya
Pankaj Kathiriya

Reputation: 4210

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.

Upvotes: 2

Related Questions