mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged mward/5.2.n-repo-1583-groupmem (5.2.1) to 5.2.N (5.2.1)
134375 mward: REPO-1583: added -me- support and improved implementation git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@134400 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1342,6 +1342,7 @@
|
|||||||
<bean id="groups" class="org.alfresco.rest.api.impl.GroupsImpl">
|
<bean id="groups" class="org.alfresco.rest.api.impl.GroupsImpl">
|
||||||
<property name="authorityService" ref="AuthorityService" />
|
<property name="authorityService" ref="AuthorityService" />
|
||||||
<property name="authorityDAO" ref="authorityDAO" />
|
<property name="authorityDAO" ref="authorityDAO" />
|
||||||
|
<property name="people" ref="people"/>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean id="Groups" class="org.springframework.aop.framework.ProxyFactoryBean">
|
<bean id="Groups" class="org.springframework.aop.framework.ProxyFactoryBean">
|
||||||
|
@@ -25,20 +25,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.alfresco.rest.api.impl;
|
package org.alfresco.rest.api.impl;
|
||||||
|
|
||||||
import java.text.Collator;
|
|
||||||
import java.util.AbstractList;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.alfresco.query.CannedQueryPageDetails;
|
import org.alfresco.query.CannedQueryPageDetails;
|
||||||
import org.alfresco.query.PagingRequest;
|
import org.alfresco.query.PagingRequest;
|
||||||
import org.alfresco.query.PagingResults;
|
import org.alfresco.query.PagingResults;
|
||||||
@@ -47,6 +33,7 @@ import org.alfresco.repo.security.authority.AuthorityInfo;
|
|||||||
import org.alfresco.repo.security.authority.UnknownAuthorityException;
|
import org.alfresco.repo.security.authority.UnknownAuthorityException;
|
||||||
import org.alfresco.rest.antlr.WhereClauseParser;
|
import org.alfresco.rest.antlr.WhereClauseParser;
|
||||||
import org.alfresco.rest.api.Groups;
|
import org.alfresco.rest.api.Groups;
|
||||||
|
import org.alfresco.rest.api.People;
|
||||||
import org.alfresco.rest.api.model.Group;
|
import org.alfresco.rest.api.model.Group;
|
||||||
import org.alfresco.rest.api.model.GroupMember;
|
import org.alfresco.rest.api.model.GroupMember;
|
||||||
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
|
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
|
||||||
@@ -65,6 +52,10 @@ import org.alfresco.util.AlfrescoCollator;
|
|||||||
import org.alfresco.util.Pair;
|
import org.alfresco.util.Pair;
|
||||||
import org.springframework.extensions.surf.util.I18NUtil;
|
import org.springframework.extensions.surf.util.I18NUtil;
|
||||||
|
|
||||||
|
import java.text.Collator;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Centralises access to groups services and maps between representations.
|
* Centralises access to groups services and maps between representations.
|
||||||
*
|
*
|
||||||
@@ -94,6 +85,8 @@ public class GroupsImpl implements Groups
|
|||||||
protected AuthorityService authorityService;
|
protected AuthorityService authorityService;
|
||||||
private AuthorityDAO authorityDAO;
|
private AuthorityDAO authorityDAO;
|
||||||
|
|
||||||
|
protected People people;
|
||||||
|
|
||||||
public AuthorityService getAuthorityService()
|
public AuthorityService getAuthorityService()
|
||||||
{
|
{
|
||||||
return authorityService;
|
return authorityService;
|
||||||
@@ -109,6 +102,11 @@ public class GroupsImpl implements Groups
|
|||||||
this.authorityDAO = authorityDAO;
|
this.authorityDAO = authorityDAO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPeople(People people)
|
||||||
|
{
|
||||||
|
this.people = people;
|
||||||
|
}
|
||||||
|
|
||||||
public Group create(Group group, Parameters parameters)
|
public Group create(Group group, Parameters parameters)
|
||||||
{
|
{
|
||||||
validateGroup(group);
|
validateGroup(group);
|
||||||
@@ -194,6 +192,9 @@ public class GroupsImpl implements Groups
|
|||||||
@Override
|
@Override
|
||||||
public CollectionWithPagingInfo<Group> getGroupsByPersonId(String personId, Parameters parameters)
|
public CollectionWithPagingInfo<Group> getGroupsByPersonId(String personId, Parameters parameters)
|
||||||
{
|
{
|
||||||
|
// Canonicalize the person ID, performing -me- alias substitution.
|
||||||
|
personId = people.validatePerson(personId);
|
||||||
|
|
||||||
final List<String> includeParam = parameters.getInclude();
|
final List<String> includeParam = parameters.getInclude();
|
||||||
Paging paging = parameters.getPaging();
|
Paging paging = parameters.getPaging();
|
||||||
|
|
||||||
@@ -201,35 +202,28 @@ public class GroupsImpl implements Groups
|
|||||||
// v0 api implementation. Should be improved in the future.
|
// v0 api implementation. Should be improved in the future.
|
||||||
Pair<String, Boolean> sortProp = getGroupsSortProp(parameters);
|
Pair<String, Boolean> sortProp = getGroupsSortProp(parameters);
|
||||||
|
|
||||||
final AuthorityType authorityType = AuthorityType.GROUP;
|
// Get all the authorities for a user, including but not limited to, groups.
|
||||||
final Set<String> userAuthorities = authorityService.getAuthoritiesForUser(personId);
|
final Set<String> userAuthorities = authorityService.getAuthoritiesForUser(personId);
|
||||||
List<AuthorityInfo> groupList = userAuthorities.stream().map(this::getAuthorityInfo).collect(Collectors.toList());
|
|
||||||
// TODO: if isRoot not null then filter
|
// Filter, transform and sort the list of user authorities into
|
||||||
AuthorityInfoComparator authorityComparator = new AuthorityInfoComparator(sortProp.getFirst(), sortProp.getSecond());
|
// a suitable list of AuthorityInfo objects.
|
||||||
Collections.sort(groupList, authorityComparator);
|
List<AuthorityInfo> groupAuthorities = userAuthorities.stream().
|
||||||
PagingResults<AuthorityInfo> pagingResult = Util.wrapPagingResults(paging, groupList);
|
filter(a -> a.startsWith(AuthorityType.GROUP.getPrefixString())).
|
||||||
|
map(this::getAuthorityInfo).
|
||||||
|
sorted(new AuthorityInfoComparator(sortProp.getFirst(), sortProp.getSecond())).
|
||||||
|
collect(Collectors.toList());
|
||||||
|
|
||||||
|
PagingResults<AuthorityInfo> pagingResult = Util.wrapPagingResults(paging, groupAuthorities);
|
||||||
|
|
||||||
// Create response.
|
// Create response.
|
||||||
final List<AuthorityInfo> page = pagingResult.getPage();
|
final List<AuthorityInfo> page = pagingResult.getPage();
|
||||||
int totalItems = pagingResult.getTotalResultCount().getFirst();
|
int totalItems = pagingResult.getTotalResultCount().getFirst();
|
||||||
|
|
||||||
|
// Transform the page of results into Group objects
|
||||||
final Set<String> rootAuthorities = getAllRootAuthorities(authorityType);
|
final Set<String> rootAuthorities = getAllRootAuthorities(AuthorityType.GROUP);
|
||||||
List<Group> groups = new AbstractList<Group>()
|
List<Group> groups = page.stream().
|
||||||
{
|
map(authority -> getGroup(authority, includeParam, rootAuthorities)).
|
||||||
@Override
|
collect(Collectors.toList());
|
||||||
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);
|
return CollectionWithPagingInfo.asPaged(paging, groups, pagingResult.hasMoreItems(), totalItems);
|
||||||
}
|
}
|
||||||
|
@@ -25,15 +25,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.alfresco.rest.api.tests;
|
package org.alfresco.rest.api.tests;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||||
import org.alfresco.rest.AbstractSingleNetworkSiteTest;
|
import org.alfresco.rest.AbstractSingleNetworkSiteTest;
|
||||||
import org.alfresco.rest.api.tests.client.PublicApiClient;
|
import org.alfresco.rest.api.tests.client.PublicApiClient;
|
||||||
@@ -41,8 +32,10 @@ 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.ListResponse;
|
||||||
import org.alfresco.rest.api.tests.client.PublicApiClient.Paging;
|
import org.alfresco.rest.api.tests.client.PublicApiClient.Paging;
|
||||||
import org.alfresco.rest.api.tests.client.PublicApiException;
|
import org.alfresco.rest.api.tests.client.PublicApiException;
|
||||||
|
import org.alfresco.rest.api.tests.client.RequestContext;
|
||||||
import org.alfresco.rest.api.tests.client.data.Group;
|
import org.alfresco.rest.api.tests.client.data.Group;
|
||||||
import org.alfresco.rest.api.tests.client.data.GroupMember;
|
import org.alfresco.rest.api.tests.client.data.GroupMember;
|
||||||
|
import org.alfresco.rest.api.tests.client.data.Person;
|
||||||
import org.alfresco.rest.framework.resource.parameters.SortColumn;
|
import org.alfresco.rest.framework.resource.parameters.SortColumn;
|
||||||
import org.alfresco.service.cmr.security.AuthorityService;
|
import org.alfresco.service.cmr.security.AuthorityService;
|
||||||
import org.alfresco.service.cmr.security.AuthorityType;
|
import org.alfresco.service.cmr.security.AuthorityType;
|
||||||
@@ -51,6 +44,10 @@ import org.junit.After;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -455,9 +452,7 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
createAuthorityContext(user1);
|
createAuthorityContext(user1);
|
||||||
setRequestContext(networkAdmin);
|
|
||||||
canGetGroupsForUserId();
|
canGetGroupsForUserId();
|
||||||
// TODO: get details for -me- without 403
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -467,9 +462,81 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
|
|||||||
|
|
||||||
private void canGetGroupsForUserId() throws ParseException, PublicApiException
|
private void canGetGroupsForUserId() throws ParseException, PublicApiException
|
||||||
{
|
{
|
||||||
|
Person personAlice;
|
||||||
|
{
|
||||||
|
publicApiClient.setRequestContext(new RequestContext(networkOne.getId(), networkAdmin, "admin"));
|
||||||
|
personAlice = new Person();
|
||||||
|
String aliceId = "alice-" + UUID.randomUUID() + "@" + networkOne.getId();
|
||||||
|
personAlice.setUserName(aliceId);
|
||||||
|
personAlice.setId(aliceId);
|
||||||
|
personAlice.setFirstName("Alice");
|
||||||
|
personAlice.setEmail("alison.smith@example.com");
|
||||||
|
personAlice.setPassword("password");
|
||||||
|
personAlice.setEnabled(true);
|
||||||
|
PublicApiClient.People people = publicApiClient.people();
|
||||||
|
people.create(personAlice);
|
||||||
|
}
|
||||||
|
|
||||||
Groups groupsProxy = publicApiClient.groups();
|
Groups groupsProxy = publicApiClient.groups();
|
||||||
ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(user1, null, "Couldn't get user's groups", 200);
|
|
||||||
assertEquals(5L, (long) groups.getPaging().getCount());
|
// As admin
|
||||||
|
setRequestContext(networkOne.getId(), networkAdmin, "admin");
|
||||||
|
|
||||||
|
// New user has only the one default group.
|
||||||
|
{
|
||||||
|
ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(personAlice.getId(), null, "Couldn't get user's groups", 200);
|
||||||
|
assertEquals(1L, (long) groups.getPaging().getTotalItems());
|
||||||
|
Iterator<Group> it = groups.getList().iterator();
|
||||||
|
assertEquals("GROUP_EVERYONE", it.next().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the user to a couple more groups and list them.
|
||||||
|
{
|
||||||
|
AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
|
||||||
|
authorityService.addAuthority(groupA.getId(), personAlice.getId());
|
||||||
|
authorityService.addAuthority(groupB.getId(), personAlice.getId());
|
||||||
|
|
||||||
|
ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(personAlice.getId(), null, "Couldn't get user's groups", 200);
|
||||||
|
assertEquals(4L, (long) groups.getPaging().getTotalItems());
|
||||||
|
Iterator<Group> it = groups.getList().iterator();
|
||||||
|
assertEquals("GROUP_EVERYONE", it.next().getId());
|
||||||
|
assertEquals(rootGroupName, it.next().getId());
|
||||||
|
assertEquals(groupA, it.next());
|
||||||
|
assertEquals(groupB, it.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get network admin's groups by explicit ID.
|
||||||
|
{
|
||||||
|
ListResponse<Group> groups = groupsProxy.getGroupsByPersonId(networkAdmin, null, "Couldn't get user's groups", 200);
|
||||||
|
assertEquals(6L, (long) groups.getPaging().getTotalItems());
|
||||||
|
}
|
||||||
|
|
||||||
|
// test -me- alias (as network admin)
|
||||||
|
{
|
||||||
|
ListResponse<Group> groups = groupsProxy.getGroupsByPersonId("-me-", null, "Couldn't get user's groups", 200);
|
||||||
|
assertEquals(6L, (long) groups.getPaging().getCount());
|
||||||
|
Iterator<Group> it = groups.getList().iterator();
|
||||||
|
assertEquals("GROUP_EVERYONE", it.next().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
// -ve test: attempt to get groups for non-existent person
|
||||||
|
{
|
||||||
|
groupsProxy.getGroupsByPersonId("i-do-not-exist", null, "Incorrect response", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// As Alice
|
||||||
|
// setRequestContext(networkOne.getId(), personAlice.getId(), "password");
|
||||||
|
|
||||||
|
// test -me- alias as Alice
|
||||||
|
// {
|
||||||
|
// ListResponse<Group> groups = groupsProxy.getGroupsByPersonId("-me-", null, "Couldn't get user's groups", 200);
|
||||||
|
// assertEquals(4L, (long) groups.getPaging().getCount());
|
||||||
|
// Iterator<Group> it = groups.getList().iterator();
|
||||||
|
// assertEquals("GROUP_EVERYONE", it.next().getId());
|
||||||
|
// assertEquals(rootGroupName, it.next().getId());
|
||||||
|
// assertEquals(groupA, it.next());
|
||||||
|
// assertEquals(groupB, it.next());
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testGetGroupMembersByGroupId() throws Exception
|
private void testGetGroupMembersByGroupId() throws Exception
|
||||||
|
Reference in New Issue
Block a user