Listing groups that a person is a member of uses the listGroupMembershipsForPerson method of the GroupsApi.
For more information about this ReST API endpoint, see List Groups a Person is a Member of.
For a description of the common parameters, such as fields, see Common Parameters.
import org.alfresco.core.handler.GroupsApi; import org.alfresco.core.model.GroupEntry; import org.alfresco.core.model.GroupPaging; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.List; @Component public class ListPersonGroupMembershipsCmd { static final Logger LOGGER = LoggerFactory.getLogger(ListPersonGroupMembershipsCmd.class); @Autowired GroupsApi groupsApi; public void execute(String personId) throws IOException { Integer skipCount = 0; Integer maxItems = 100; String where = null; List<String> orderBy = null; List<String> include = null; List<String> fields = null; LOGGER.info("Listing group memberships for person {}", personId); GroupPaging groups = groupsApi.listGroupMembershipsForPerson( personId, skipCount, maxItems, orderBy, include, where, fields).getBody(); for (GroupEntry groupEntry: groups.getList().getEntries()) { LOGGER.info(" {}", groupEntry.getEntry().getDisplayName()); } } }
Executing this code will list the group memberships for passed in username. In this example we list group memberships for two of the out-of-the-box users abeecher and admin:
% java -jar target/rest-api-0.0.1-SNAPSHOT.jar list-person-group-memberships abeecher 2021-05-06 09:42:50.643 INFO 24597 --- [ main] o.a.tutorial.restapi.RestApiApplication : Started RestApiApplication in 3.63 seconds (JVM running for 4.106) 2021-05-06 09:42:50.645 INFO 24597 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[0]: list-person-group-memberships 2021-05-06 09:42:50.647 INFO 24597 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: abeecher 2021-05-06 09:42:50.647 INFO 24597 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : Listing group memberships for person abeecher 2021-05-06 09:42:50.821 INFO 24597 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : null 2021-05-06 09:42:50.821 INFO 24597 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : site_swsdp 2021-05-06 09:42:50.821 INFO 24597 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : site_swsdp_SiteCollaborator % java -jar target/rest-api-0.0.1-SNAPSHOT.jar list-person-group-memberships admin 2021-05-06 09:43:06.433 INFO 24599 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: admin 2021-05-06 09:43:06.433 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : Listing group memberships for person admin 2021-05-06 09:43:06.631 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : ALFRESCO_ADMINISTRATORS 2021-05-06 09:43:06.631 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : ALFRESCO_MODEL_ADMINISTRATORS 2021-05-06 09:43:06.631 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : ALFRESCO_SEARCH_ADMINISTRATORS 2021-05-06 09:43:06.631 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : EMAIL_CONTRIBUTORS 2021-05-06 09:43:06.631 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : null 2021-05-06 09:43:06.631 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : SITE_ADMINISTRATORS 2021-05-06 09:43:06.632 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : site_swsdp 2021-05-06 09:43:06.632 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : site_swsdp_SiteManager 2021-05-06 09:43:06.632 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : site_test 2021-05-06 09:43:06.632 INFO 24599 --- [ main] o.a.t.r.ListPersonGroupMembershipsCmd : site_test_SiteManager```