diff --git a/source/java/org/alfresco/rest/api/Groups.java b/source/java/org/alfresco/rest/api/Groups.java index 927a570810..9a3e17b502 100644 --- a/source/java/org/alfresco/rest/api/Groups.java +++ b/source/java/org/alfresco/rest/api/Groups.java @@ -81,6 +81,17 @@ public interface Groups */ CollectionWithPagingInfo getGroups(Parameters parameters); + /** + * Gets the list of groups for which the specified person is a member. + * + * @param personId the person's ID ("-me-" may be used as an alias for the current user.) + * @param parameters the {@link Parameters} object to get the parameters passed into the request + * including: + * - sort & paging params (orderBy, skipCount, maxItems) + * @return a paged list of {@code org.alfresco.rest.api.model.Group} objects + */ + CollectionWithPagingInfo getGroupsByPersonId(String personId, Parameters parameters); + /** * Gets a list of groups. * diff --git a/source/java/org/alfresco/rest/api/impl/GroupsImpl.java b/source/java/org/alfresco/rest/api/impl/GroupsImpl.java index e24d6803c2..5acd43a7b6 100644 --- a/source/java/org/alfresco/rest/api/impl/GroupsImpl.java +++ b/source/java/org/alfresco/rest/api/impl/GroupsImpl.java @@ -165,8 +165,6 @@ public class GroupsImpl implements Groups } final AuthorityType authorityType = AuthorityType.GROUP; - // TODO: I think this is where we need to do this slightly differently, i.e. - // getAllRootAuthoritiesByUserId(). What about root authorities? Are all group authorities root? final Set rootAuthorities = getAllRootAuthorities(authorityType); PagingResults pagingResult = getAuthoritiesInfo(authorityType, isRootParam, rootAuthorities, sortProp, paging); @@ -193,6 +191,49 @@ public class GroupsImpl implements Groups return CollectionWithPagingInfo.asPaged(paging, groups, pagingResult.hasMoreItems(), totalItems); } + @Override + public CollectionWithPagingInfo getGroupsByPersonId(String personId, Parameters parameters) + { + final List includeParam = parameters.getInclude(); + Paging paging = parameters.getPaging(); + + // Retrieve sort column. This is limited for now to sort column due to + // v0 api implementation. Should be improved in the future. + Pair sortProp = getGroupsSortProp(parameters); + + final AuthorityType authorityType = AuthorityType.GROUP; + final Set userAuthorities = authorityService.getAuthoritiesForUser(personId); + List groupList = userAuthorities.stream().map(this::getAuthorityInfo).collect(Collectors.toList()); + // TODO: if isRoot not null then filter + AuthorityInfoComparator authorityComparator = new AuthorityInfoComparator(sortProp.getFirst(), sortProp.getSecond()); + Collections.sort(groupList, authorityComparator); + PagingResults pagingResult = Util.wrapPagingResults(paging, groupList); + + // Create response. + final List page = pagingResult.getPage(); + int totalItems = pagingResult.getTotalResultCount().getFirst(); + + + final Set rootAuthorities = getAllRootAuthorities(authorityType); + List groups = new AbstractList() + { + @Override + public Group get(int index) + { + AuthorityInfo authorityInfo = page.get(index); + return getGroup(authorityInfo, includeParam, rootAuthorities); + } + + @Override + public int size() + { + return page.size(); + } + }; + + return CollectionWithPagingInfo.asPaged(paging, groups, pagingResult.hasMoreItems(), totalItems); + } + private PagingResults getAuthoritiesInfo(AuthorityType authorityType, Boolean isRootParam, Set rootAuthorities, Pair sortProp, Paging paging) { @@ -299,7 +340,8 @@ public class GroupsImpl implements Groups throw new InvalidArgumentException("id is null or empty"); } - if (!authorityService.authorityExists(id)) + // authorityService.authorityExists("GROUP_EVERYONE") returns false! + if (!id.equals("GROUP_EVERYONE") && !authorityService.authorityExists(id)) { throw new EntityNotFoundException(id); } diff --git a/source/java/org/alfresco/rest/api/people/PersonGroupsRelation.java b/source/java/org/alfresco/rest/api/people/PersonGroupsRelation.java index 9abbcee535..e330d9e7c0 100644 --- a/source/java/org/alfresco/rest/api/people/PersonGroupsRelation.java +++ b/source/java/org/alfresco/rest/api/people/PersonGroupsRelation.java @@ -63,7 +63,6 @@ public class PersonGroupsRelation implements RelationshipResourceAction.Read readAll(String personId, Parameters params) { - // TODO: temp, this just gets all the groups, not by person ID... - return groups.getGroups(params); + return groups.getGroupsByPersonId(personId, params); } } diff --git a/source/test-java/org/alfresco/rest/api/tests/GroupsTest.java b/source/test-java/org/alfresco/rest/api/tests/GroupsTest.java index b37f807ead..fa9079ed1d 100644 --- a/source/test-java/org/alfresco/rest/api/tests/GroupsTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/GroupsTest.java @@ -25,12 +25,7 @@ */ package org.alfresco.rest.api.tests; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - +import java.text.ParseException; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -45,6 +40,7 @@ import org.alfresco.rest.api.tests.client.PublicApiClient; import org.alfresco.rest.api.tests.client.PublicApiClient.Groups; import org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse; import org.alfresco.rest.api.tests.client.PublicApiClient.Paging; +import org.alfresco.rest.api.tests.client.PublicApiException; import org.alfresco.rest.api.tests.client.data.Group; import org.alfresco.rest.api.tests.client.data.GroupMember; import org.alfresco.rest.framework.resource.parameters.SortColumn; @@ -55,6 +51,8 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.*; + /** * V1 REST API tests for managing Groups * @@ -420,6 +418,12 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest return groupsProxy.getGroupMembers(groupId, createParams(paging, otherParams), errorMessage, expectedStatus); } + private ListResponse getGroupsByPersonId(String userId, final PublicApiClient.Paging paging, Map otherParams, String errorMessage, int expectedStatus) throws Exception + { + final Groups groupsProxy = publicApiClient.groups(); + return groupsProxy.getGroupsByPersonId(userId, createParams(paging, otherParams), errorMessage, expectedStatus); + } + private ListResponse getGroupMembers(String groupId, final PublicApiClient.Paging paging, Map otherParams) throws Exception { return getGroupMembers(groupId, paging, otherParams, "Failed to get group members", HttpServletResponse.SC_OK); @@ -445,6 +449,29 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest } } + @Test + public void testGetGroupsByUserId() throws Exception + { + try + { + createAuthorityContext(user1); + setRequestContext(networkAdmin); + canGetGroupsForUserId(); + // TODO: get details for -me- without 403 + } + finally + { + clearAuthorityContext(); + } + } + + private void canGetGroupsForUserId() throws ParseException, PublicApiException + { + Groups groupsProxy = publicApiClient.groups(); + ListResponse groups = groupsProxy.getGroupsByPersonId(user1, null, "Couldn't get user's groups", 200); + assertEquals(5L, (long) groups.getPaging().getCount()); + } + private void testGetGroupMembersByGroupId() throws Exception { Paging paging = getPaging(0, 4); diff --git a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java index c26c7335cc..130e7fe32f 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java @@ -2354,5 +2354,23 @@ public class PublicApiClient } return null; } + + public ListResponse getGroupsByPersonId(String userId, Map params, String errorMessage, int expectedStatus) + throws PublicApiException, ParseException + { + HttpResponse response = getAll("people", userId, "groups", null, params, errorMessage, expectedStatus); + + if (response != null && response.getJsonResponse() != null) + { + JSONObject jsonList = (JSONObject) response.getJsonResponse().get("list"); + if (jsonList != null) + { + return Group.parseGroups(response.getJsonResponse()); + } + } + return null; + } + + } }