Skip to content

Commit

Permalink
ACS-5506 Add description and hasSubgroups to groups API
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKinas committed Jul 4, 2023
1 parent 20582df commit 0c31dbb
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ public class RestGroupsModel extends TestModel implements IRestModel<RestGroupsM
private String id;
@JsonProperty(required = true)
private String displayName;
@JsonProperty()
private String description;
@JsonProperty(required = true)
private Boolean isRoot;
@JsonProperty(required = true)
private Boolean hasSubgroups;

@JsonProperty("parentIds")
private ArrayList<String> parentIds;
Expand Down Expand Up @@ -75,6 +79,22 @@ public void setDisplayName(String displayName)
this.displayName = displayName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Boolean getHasSubgroups() {
return hasSubgroups;
}

public void setHasSubgroups(Boolean hasSubgroups) {
this.hasSubgroups = hasSubgroups;
}

public Boolean getIsRoot()
{
return isRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void dataPreparation() throws Exception
public void createListUpdateAndDeleteGroup() throws Exception
{
String groupName = "ZtestGroup" + UUID.randomUUID().toString();
JsonObject groupBody = Json.createObjectBuilder().add("id", groupName).add("displayName", groupName).build();
String groupDescription = "ZtestGroup description" + UUID.randomUUID().toString();
JsonObject groupBody = Json.createObjectBuilder().add("id", groupName).add("displayName", groupName).add("description", groupDescription).build();
String groupBodyCreate = groupBody.toString();

//GroupCreation:
Expand All @@ -45,7 +46,9 @@ public void createListUpdateAndDeleteGroup() throws Exception
restClient.authenticateUser(adminUser).withCoreAPI().usingParams("include=zones").usingGroups().createGroup(groupBodyCreate)
.assertThat().field("zones").contains("APP.DEFAULT")
.and().field("isRoot").is(true)
.and().field("displayName").is(groupName);
.and().field("displayName").is(groupName)
.and().field("description").is(groupDescription)
.and().field("hasSubgroups").is(false);
restClient.assertStatusCodeIs(HttpStatus.CREATED);

//ListGroups:
Expand All @@ -55,11 +58,12 @@ public void createListUpdateAndDeleteGroup() throws Exception
.and().paginationField("maxItems").is("10");
restClient.assertStatusCodeIs(HttpStatus.OK);

groupBody = Json.createObjectBuilder().add("displayName", "Z"+groupName).build();
groupBody = Json.createObjectBuilder().add("displayName", "Z"+groupName).add("description", "Z"+groupDescription).build();
String groupBodyUpdate = groupBody.toString();
//UpdateGroup:
restClient.withCoreAPI().usingGroups().updateGroupDetails("GROUP_"+groupName, groupBodyUpdate)
.assertThat().field("displayName").is("Z"+groupName)
.and().field("description").is("Z"+groupDescription)
.and().field("id").is("GROUP_"+groupName)
.and().field("zones").isNull();
restClient.assertStatusCodeIs(HttpStatus.OK);
Expand All @@ -68,7 +72,8 @@ public void createListUpdateAndDeleteGroup() throws Exception
restClient.withCoreAPI().usingParams("include=zones").usingGroups().getGroupDetail("GROUP_"+groupName)
.assertThat().field("id").is("GROUP_"+groupName)
.and().field("zones").contains("APP.DEFAULT")
.and().field("isRoot").is(true);
.and().field("isRoot").is(true)
.and().field("hasSubgroups").is(false);
restClient.assertStatusCodeIs(HttpStatus.OK);

//DeleteGroup:
Expand All @@ -86,26 +91,43 @@ public void createListUpdateAndDeleteGroup() throws Exception
public void createListDeleteGroupMembership() throws Exception
{
String groupName = "ZtestGroup" + UUID.randomUUID().toString();
String subGroupName = "ZtestSubgroup" + UUID.randomUUID().toString();
JsonObject groupBody = Json.createObjectBuilder().add("id", groupName).add("displayName", groupName).build();
JsonObject subgroupBody = Json.createObjectBuilder().add("id", subGroupName).add("displayName", subGroupName).build();
String groupBodyCreate = groupBody.toString();
String subgroupBodyCreate = subgroupBody.toString();

//GroupCreation:
restClient.authenticateUser(adminUser).withCoreAPI().usingParams("include=zones").usingGroups().createGroup(groupBodyCreate);
restClient.assertStatusCodeIs(HttpStatus.CREATED);
restClient.authenticateUser(adminUser).withCoreAPI().usingParams("include=zones").usingGroups().createGroup(subgroupBodyCreate);
restClient.assertStatusCodeIs(HttpStatus.CREATED);

JsonObject groupMembershipBody = Json.createObjectBuilder().add("id", userModel.getUsername()).add("memberType", "PERSON").build();
String groupMembershipBodyCreate = groupMembershipBody.toString();
JsonObject groupMembershipGroupBody = Json.createObjectBuilder().add("id", subGroupName).add("memberType", "GROUP").build();
String groupMembershipGroupBodyCreate = groupMembershipGroupBody.toString();

//MembershipCreation:
//-ve
restClient.authenticateUser(userModel).withCoreAPI().usingGroups().createGroupMembership("GROUP_"+groupName, groupMembershipBodyCreate);
restClient.assertStatusCodeIs(HttpStatus.FORBIDDEN);
//+ve
restClient.authenticateUser(adminUser).withCoreAPI().usingGroups().createGroupMembership("GROUP_"+groupName, groupMembershipBodyCreate);
restClient.assertStatusCodeIs(HttpStatus.CREATED);
restClient.authenticateUser(adminUser).withCoreAPI().usingGroups().createGroupMembership("GROUP_"+groupName, groupMembershipGroupBodyCreate);
restClient.assertStatusCodeIs(HttpStatus.CREATED);

//ListPersonMembership
restClient.authenticateUser(userModel).withCoreAPI().usingUser(userModel).listGroupMemberships()
.assertThat().entriesListContains("id", "GROUP_"+groupName);
.assertThat().entriesListContains("id", "GROUP_"+groupName)
.and().entriesListContains("id", "GROUP_"+subGroupName);
restClient.assertStatusCodeIs(HttpStatus.OK);

//CheckListDetails
restClient.withCoreAPI().usingParams("include=zones").usingGroups().getGroupDetail("GROUP_"+groupName)
.assertThat().field("id").is("GROUP_"+groupName)
.and().field("hasSubgroups").is(true);
restClient.assertStatusCodeIs(HttpStatus.OK);

//DeleteGroupMembership
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
import org.alfresco.repo.security.authority.UnknownAuthorityException;
import org.alfresco.rest.antlr.WhereClauseParser;
import org.alfresco.rest.api.Groups;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.People;
import org.alfresco.rest.api.model.Group;
import org.alfresco.rest.api.model.GroupMember;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
Expand All @@ -68,6 +70,7 @@
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker;
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalkerOrSupported;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PermissionService;
Expand Down Expand Up @@ -109,6 +112,7 @@ public class GroupsImpl implements Groups
private AuthorityDAO authorityDAO;

protected People people;
protected Nodes nodes;

public AuthorityService getAuthorityService()
{
Expand All @@ -130,6 +134,10 @@ public void setPeople(People people)
this.people = people;
}

public void setNodes(Nodes nodes) {
this.nodes = nodes;
}

public Group create(Group group, Parameters parameters)
{
validateGroup(group, false);
Expand All @@ -151,6 +159,17 @@ public Group create(Group group, Parameters parameters)
authorityService.addAuthority(group.getParentIds(), authority);
}

if (group.getDescription() != null && !group.getDescription().isEmpty())
{
NodeRef groupNodeRef = authorityService.getAuthorityNodeRef(authorityDisplayName);
Node groupNode = nodes.getNode(groupNodeRef.getId());
Map<String, Object> props = groupNode.getProperties();
if (props != null)
{
props.put("cm:description", group.getDescription());
}
}

return getGroup(authority, parameters);
}

Expand All @@ -168,6 +187,17 @@ public Group update(String groupId, Group group, Parameters parameters)
handleAuthorityException(ae);
}

if (group.getDescription() != null && !group.getDescription().isEmpty())
{
NodeRef groupNodeRef = authorityService.getAuthorityNodeRef(group.getDisplayName());
Node groupNode = nodes.getNode(groupNodeRef.getId());
Map<String, Object> props = groupNode.getProperties();
if (props != null)
{
props.put("cm:description", group.getDescription());
}
}

return getGroup(groupId, parameters);
}

Expand Down Expand Up @@ -584,6 +614,17 @@ private Group getGroup(AuthorityInfo authorityInfo, List<String> includeParam, S
group.setDisplayName(authorityDisplayName);

group.setIsRoot(isRootAuthority(rootAuthorities, authorityInfo.getAuthorityName()));
group.setHasSubgroups(!authorityService.getContainedAuthorities(AuthorityType.GROUP, authorityInfo.getAuthorityName(), true).isEmpty());

NodeRef groupNodeRef = authorityService.getAuthorityNodeRef(authorityDisplayName);
Node groupNode = nodes.getNode(groupNodeRef.getId());
Map<String, Object> props = groupNode.getProperties();
String description = "";
if (props != null)
{
description = props.get("cm:description") != null ? (String) props.get("cm:description") : "";
}
group.setDescription(description);

// Optionally include
if (includeParam != null)
Expand Down Expand Up @@ -1014,6 +1055,10 @@ private void validateGroup(Group group, boolean isUpdate)
{
throw new InvalidArgumentException("Group update does not support field: zones");
}
if (group.wasSet(Group.HAS_SUBGROUPS))
{
throw new InvalidArgumentException("Group update does not support field: hasSubgroups");
}
}
}

Expand Down
23 changes: 22 additions & 1 deletion remote-api/src/main/java/org/alfresco/rest/api/model/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ public class Group implements Comparable<Group>

protected String id; // group id (aka authority name)
protected String displayName;
protected String description;
protected Boolean isRoot;
protected Boolean hasSubgroups;
protected Set<String> parentIds;
protected Set<String> zones;

private Map<String, Boolean> setFields = new HashMap<>(7);

public static final String ID = "id";
public static final String DISPLAY_NAME = "displayName";
public static final String DESCRIPTION = "description";
public static final String IS_ROOT = "isRoot";
public static final String HAS_SUBGROUPS = "hasSubgroups";
public static final String PARENT_IDS = "parentIds";
public static final String ZONES = "zones";

Expand Down Expand Up @@ -81,6 +85,14 @@ public void setDisplayName(String displayName)
setFields.put(DISPLAY_NAME, true);
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Boolean getIsRoot()
{
return isRoot;
Expand All @@ -92,6 +104,14 @@ public void setIsRoot(Boolean isRoot)
setFields.put(IS_ROOT, true);
}

public Boolean getHasSubgroups() {
return hasSubgroups;
}

public void setHasSubgroups(Boolean hasSubgroups) {
this.hasSubgroups = hasSubgroups;
}

public Set<String> getParentIds()
{
return parentIds;
Expand Down Expand Up @@ -154,7 +174,8 @@ public int hashCode()
@Override
public String toString()
{
return "Group [id=" + id + ", displayName=" + displayName + ", isRoot=" + isRoot + "]";
return "Group [id=" + id + ", displayName=" + displayName + ", description=" + description
+ ", isRoot=" + isRoot + ", hasSubgroups=" + hasSubgroups + "]";
}

public boolean wasSet(String fieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,7 @@
<property name="authorityService" ref="AuthorityService" />
<property name="authorityDAO" ref="authorityDAO" />
<property name="people" ref="people"/>
<property name="nodes" ref="nodes"/>
</bean>

<bean id="Groups" class="org.springframework.aop.framework.ProxyFactoryBean">
Expand Down
Loading

0 comments on commit 0c31dbb

Please sign in to comment.