groovyliferayliferay-6.2

How do you get a list of users for a Liferay 6.2 site


I am trying to get a list of users for a Liferay 6.2 site, but I can't seem to find a way to do so. I have the groupId, which I know is correct, since I can get the documents, but the query only gets directly added users, not inherited ones (e.g. from the organisation).

I just use the method UserLocalService.getGroupUsers(groupId), then loop through them. How can I get all users (i.e. the same as Site Memberships in Site Administration, but without the paging)?

update

I have a Liferay portal instance, it has several organisations with associated sites. Org A has User 1, 2, 3 etc. Org B has User 4, 5, 6. They have the same company id's (since they are part of the same portal), but different group id's. I only want those who are in Org A (directly added, part of any user groups or organisations which have been assigned). The site could also be a non organisation site (i.e. Org A and B assigned, but not any others, e.g. a new Org C), for document sharing between organisations. From my understanding, all sites are internally known as groups, from doing work with document libraries.


Solution

  • You could do two calls to get the site and the organization users, then one call to get the assigned organizations, iterate through them and get their users. Then combine all the results:

    HashSet<User> groupAndOrganizationUsersSet = new LinkedHashSet<>();
    
    groupAndOrganizationUsersSet.addAll(UserLocalServiceUtil.getGroupUsers(groupId));
    groupAndOrganizationUsersSet.addAll(UserLocalServiceUtil.getOrganizationUsers(
            GroupLocalServiceUtil.getGroup(groupId).getOrganizationId()));
    
    for (Organization organization : 
            OrganizationLocalServiceUtil.getGroupOrganizations(groupId)) {
    
        groupAndOrganizationUsersSet.addAll(UserLocalServiceUtil.getOrganizationUsers(
                organization.getOrganizationId()));
    }
    
    List<User> groupAndOrganizationUsers = new ArrayList<>(groupAndOrganizationUsersSet);