Search code examples
liferayhookliferay-6liferay-theme

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?


Solution

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