Search code examples
liferayliferay-6user-permissions

How to get all users of a site role in Liferay 6.1?


I have LDAP imported user groups which I have mapped to site roles (as mapping them to organization roles was not possible for Liferay 6.1).

So for example I have mapped the user group 'my_site administrators' to the site role 'Site Administrators' of the site 'my_site'.

How can I get all the users that are members of a site role taking into account the user group memberships too?

I have tried the following code but did not work.

Set<User> siteMembers = new HashSet<User>();
Group group = GroupLocalServiceUtil.getGroup(layout.getGroupId());
Integer[] types = new Integer[]{Integer.valueOf(2)}; //site roles
List<Role> siteRoles = RoleLocalServiceUtil.search(group.getCompanyId(), null, types, 0, 10, null);
Set<UserGroupRole> siteUserGroupRoles = new HashSet<UserGroupRole>();

for (Iterator<Role> iterator = siteRoles.iterator(); iterator.hasNext();) {
    Role siteRole = (Role) iterator.next();
    List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(group.getGroupId(), siteRole.getRoleId());
    siteUserGroupRoles.addAll(userGroupRoles);
}
for (Iterator<UserGroupRole> it1 = siteUserGroupRoles.iterator(); it1.hasNext();) {
    UserGroupRole userGroupRole = (UserGroupRole) it1.next();
    User userGroupUser = userGroupRole.getUser();
    siteMembers.add(userGroupUser);
}

Solution

  • Finally found the following solution:

    Set<User> siteMembers = new HashSet<User>();
    Group group = GroupLocalServiceUtil.getGroup(layout.getGroupId());
    long groupId = group.getGroupId();
    Integer[] types = new Integer[]{Integer.valueOf(2)}; //site roles
    List<Role> siteRoles = RoleLocalServiceUtil.search(group.getCompanyId(), null, types, 0, 10, null);
    Set<UserGroupGroupRole> siteUserGroupGroupRoles = new HashSet<UserGroupGroupRole>();
    
    for (Iterator<Role> iterator = siteRoles.iterator(); iterator.hasNext();) {
        Role siteRole = (Role) iterator.next();
        List<UserGroupGroupRole> userGroupGroupRoles = UserGroupGroupRoleLocalServiceUtil.getUserGroupGroupRolesByGroupAndRole(groupId, siteRole.getRoleId());
        siteUserGroupGroupRoles.addAll(userGroupGroupRoles);
    }
    for (Iterator<UserGroupGroupRole> it1 = siteUserGroupGroupRoles.iterator(); it1.hasNext();) {
        UserGroupGroupRole userGroupGroupRole = (UserGroupGroupRole) it1.next();
        long userGroupId = userGroupGroupRole.getUserGroupId();
        List<User> userGroupUsers = UserLocalServiceUtil.getUserGroupUsers(userGroupId);
        siteMembers.addAll(userGroupUsers);
    }
    siteMembers.addAll(UserLocalServiceUtil.getGroupUsers(groupId));
    

    It does not seem straight-forward. I would expect a method fetching all site members, even the indirect ones through site role-user group-user mapping.

    I had to fetch separately all users belonging to all user groups having a site role association with the site and on top of that fetch all users with direct membership to the site.

    Any other more straight-forward solution would be welcome.