mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
REPO-1307: Create group membership
- Added create group membership functionality - Added tests git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@134750 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -131,4 +131,12 @@ public interface Groups
|
||||
* @return a paged list of {@code org.alfresco.rest.api.model.GroupMember} objects
|
||||
*/
|
||||
CollectionWithPagingInfo<GroupMember> getGroupMembers(String groupId, Parameters parameters);
|
||||
|
||||
/**
|
||||
* Create a group member.
|
||||
*
|
||||
* @param groupId the identifier of a group.
|
||||
* @return a {@code org.alfresco.rest.api.model.GroupMember} object
|
||||
*/
|
||||
GroupMember createGroupMember(String groupId, GroupMember groupMember);
|
||||
}
|
||||
|
@@ -25,9 +25,14 @@
|
||||
*/
|
||||
package org.alfresco.rest.api.groups;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.rest.api.Groups;
|
||||
import org.alfresco.rest.api.model.GroupMember;
|
||||
import org.alfresco.rest.framework.WebApiDescription;
|
||||
import org.alfresco.rest.framework.WebApiParam;
|
||||
import org.alfresco.rest.framework.core.ResourceParameter;
|
||||
import org.alfresco.rest.framework.resource.RelationshipResource;
|
||||
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
|
||||
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
|
||||
@@ -40,7 +45,7 @@ import org.springframework.beans.factory.InitializingBean;
|
||||
* @author cturlica
|
||||
*/
|
||||
@RelationshipResource(name = "members", entityResource = GroupsEntityResource.class, title = "Group Members")
|
||||
public class GroupMembersRelation implements RelationshipResourceAction.Read<GroupMember>, InitializingBean
|
||||
public class GroupMembersRelation implements RelationshipResourceAction.Read<GroupMember>, RelationshipResourceAction.Create<GroupMember>, InitializingBean
|
||||
{
|
||||
private Groups groups;
|
||||
|
||||
@@ -56,9 +61,19 @@ public class GroupMembersRelation implements RelationshipResourceAction.Read<Gro
|
||||
}
|
||||
|
||||
@Override
|
||||
@WebApiDescription(title="A paged list of all the members of the group 'groupId'.")
|
||||
@WebApiDescription(title = "A paged list of all the members of the group 'groupId'.")
|
||||
public CollectionWithPagingInfo<GroupMember> readAll(String groupId, Parameters params)
|
||||
{
|
||||
return groups.getGroupMembers(groupId, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
@WebApiDescription(title = "Create group membership.")
|
||||
@WebApiParam(name = "entity", title = "A single group member", description = "A single group member, multiple groups members are not supported.", kind = ResourceParameter.KIND.HTTP_BODY_OBJECT, allowMultiple = false)
|
||||
public List<GroupMember> create(String groupId, List<GroupMember> entity, Parameters params)
|
||||
{
|
||||
List<GroupMember> result = new ArrayList<>(1);
|
||||
result.add(groups.createGroupMember(groupId, entity.get(0)));
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -25,11 +25,27 @@
|
||||
*/
|
||||
package org.alfresco.rest.api.impl;
|
||||
|
||||
import static org.alfresco.repo.security.authentication.AuthenticationUtil.runAsSystem;
|
||||
|
||||
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.PagingRequest;
|
||||
import org.alfresco.query.PagingResults;
|
||||
import org.alfresco.repo.security.authority.AuthorityDAO;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.security.authority.AuthorityDAO;
|
||||
import org.alfresco.repo.security.authority.AuthorityException;
|
||||
import org.alfresco.repo.security.authority.AuthorityInfo;
|
||||
import org.alfresco.repo.security.authority.UnknownAuthorityException;
|
||||
@@ -51,17 +67,11 @@ import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
|
||||
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalkerOrSupported;
|
||||
import org.alfresco.service.cmr.security.AuthorityService;
|
||||
import org.alfresco.service.cmr.security.AuthorityType;
|
||||
import org.alfresco.util.AlfrescoCollator;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.util.AlfrescoCollator;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.springframework.extensions.surf.util.I18NUtil;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.alfresco.repo.security.authentication.AuthenticationUtil.runAsSystem;
|
||||
|
||||
/**
|
||||
* Centralises access to groups services and maps between representations.
|
||||
*
|
||||
@@ -573,21 +583,7 @@ public class GroupsImpl implements Groups
|
||||
QueryHelper.walk(q, propertyWalker);
|
||||
|
||||
String memberTypeStr = propertyWalker.getProperty(PARAM_MEMBER_TYPE, WhereClauseParser.EQUALS, String.class);
|
||||
|
||||
if (memberTypeStr != null && !memberTypeStr.isEmpty())
|
||||
{
|
||||
switch (memberTypeStr)
|
||||
{
|
||||
case PARAM_MEMBER_TYPE_GROUP:
|
||||
authorityType = AuthorityType.GROUP;
|
||||
break;
|
||||
case PARAM_MEMBER_TYPE_PERSON:
|
||||
authorityType = AuthorityType.USER;
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException("MemberType is invalid (expected eg. GROUP, PERSON)");
|
||||
}
|
||||
}
|
||||
authorityType = getAuthorityType(memberTypeStr);
|
||||
}
|
||||
|
||||
PagingResults<AuthorityInfo> pagingResult = getAuthoritiesInfo(authorityType, groupId, sortProp, paging);
|
||||
@@ -614,6 +610,45 @@ public class GroupsImpl implements Groups
|
||||
return CollectionWithPagingInfo.asPaged(paging, groupMembers, pagingResult.hasMoreItems(), totalItems);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupMember createGroupMember(String groupId, GroupMember groupMember)
|
||||
{
|
||||
validateGroupId(groupId, false);
|
||||
validateGroupMember(groupMember);
|
||||
|
||||
AuthorityType authorityType = getAuthorityType(groupMember.getMemberType());
|
||||
|
||||
if (!authorityService.authorityExists(groupMember.getId()))
|
||||
{
|
||||
throw new EntityNotFoundException("Group member with id " + groupMember.getId() + " does not exists");
|
||||
}
|
||||
|
||||
authorityService.addAuthority(groupId, groupMember.getId());
|
||||
String authority = authorityService.getName(authorityType, groupMember.getId());
|
||||
|
||||
return getGroupMember(authority);
|
||||
}
|
||||
|
||||
private AuthorityType getAuthorityType(String memberType)
|
||||
{
|
||||
AuthorityType authorityType = null;
|
||||
if (memberType != null && !memberType.isEmpty())
|
||||
{
|
||||
switch (memberType)
|
||||
{
|
||||
case PARAM_MEMBER_TYPE_GROUP:
|
||||
authorityType = AuthorityType.GROUP;
|
||||
break;
|
||||
case PARAM_MEMBER_TYPE_PERSON:
|
||||
authorityType = AuthorityType.USER;
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException("MemberType is invalid (expected eg. GROUP, PERSON)");
|
||||
}
|
||||
}
|
||||
return authorityType;
|
||||
}
|
||||
|
||||
private PagingResults<AuthorityInfo> getAuthoritiesInfo(AuthorityType authorityType, String groupId, Pair<String, Boolean> sortProp, Paging paging)
|
||||
{
|
||||
Set<String> authorities;
|
||||
@@ -674,6 +709,13 @@ public class GroupsImpl implements Groups
|
||||
return groupMember;
|
||||
}
|
||||
|
||||
private GroupMember getGroupMember(String authorityId)
|
||||
{
|
||||
AuthorityInfo authorityInfo = getAuthorityInfo(authorityId);
|
||||
|
||||
return getGroupMember(authorityInfo);
|
||||
}
|
||||
|
||||
private void validateGroupId(String groupId, boolean inferPrefix)
|
||||
{
|
||||
if (groupId == null || groupId.isEmpty())
|
||||
@@ -735,6 +777,24 @@ public class GroupsImpl implements Groups
|
||||
}
|
||||
}
|
||||
|
||||
private void validateGroupMember(GroupMember groupMember)
|
||||
{
|
||||
if (groupMember == null)
|
||||
{
|
||||
throw new InvalidArgumentException("group member is null");
|
||||
}
|
||||
|
||||
if (groupMember.getId() == null || groupMember.getId().isEmpty())
|
||||
{
|
||||
throw new InvalidArgumentException("group member Id is null or empty");
|
||||
}
|
||||
|
||||
if (groupMember.getMemberType() == null || groupMember.getMemberType().isEmpty())
|
||||
{
|
||||
throw new InvalidArgumentException("group member type is null or empty");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean groupAuthorityExists(String authorityName)
|
||||
{
|
||||
return groupAuthorityExists(authorityName, true);
|
||||
|
Reference in New Issue
Block a user