ACS-5506 Add description and hasSubgroups to groups API

This commit is contained in:
MichalKinas
2023-07-03 13:45:05 +02:00
parent 60e42b4090
commit d9fe82a2d2
7 changed files with 197 additions and 48 deletions

View File

@@ -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;
@@ -75,6 +79,22 @@ public class RestGroupsModel extends TestModel implements IRestModel<RestGroupsM
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;

View File

@@ -34,7 +34,8 @@ public class GroupsTests extends RestTest
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:
@@ -45,7 +46,9 @@ public class GroupsTests extends RestTest
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:
@@ -55,11 +58,12 @@ public class GroupsTests extends RestTest
.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);
@@ -68,7 +72,8 @@ public class GroupsTests extends RestTest
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:
@@ -86,15 +91,23 @@ public class GroupsTests extends RestTest
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);
@@ -102,10 +115,19 @@ public class GroupsTests extends RestTest
//+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

View File

@@ -51,9 +51,11 @@ import org.alfresco.repo.security.authority.AuthorityInfo;
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;
@@ -68,6 +70,7 @@ import org.alfresco.rest.framework.resource.parameters.where.Query;
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;
@@ -109,6 +112,7 @@ public class GroupsImpl implements Groups
private AuthorityDAO authorityDAO;
protected People people;
protected Nodes nodes;
public AuthorityService getAuthorityService()
{
@@ -130,6 +134,10 @@ public class GroupsImpl implements Groups
this.people = people;
}
public void setNodes(Nodes nodes) {
this.nodes = nodes;
}
public Group create(Group group, Parameters parameters)
{
validateGroup(group, false);
@@ -151,6 +159,17 @@ public class GroupsImpl implements Groups
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);
}
@@ -168,6 +187,17 @@ public class GroupsImpl implements Groups
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);
}
@@ -584,6 +614,17 @@ public class GroupsImpl implements Groups
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)
@@ -1014,6 +1055,10 @@ public class GroupsImpl implements Groups
{
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");
}
}
}

View File

@@ -42,7 +42,9 @@ 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;
@@ -50,7 +52,9 @@ public class Group implements Comparable<Group>
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";
@@ -81,6 +85,14 @@ public class Group implements Comparable<Group>
setFields.put(DISPLAY_NAME, true);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getIsRoot()
{
return isRoot;
@@ -92,6 +104,14 @@ public class Group implements Comparable<Group>
setFields.put(IS_ROOT, true);
}
public Boolean getHasSubgroups() {
return hasSubgroups;
}
public void setHasSubgroups(Boolean hasSubgroups) {
this.hasSubgroups = hasSubgroups;
}
public Set<String> getParentIds()
{
return parentIds;
@@ -154,7 +174,8 @@ public class Group implements Comparable<Group>
@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)

View File

@@ -1693,6 +1693,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">

View File

@@ -229,6 +229,7 @@ public class GroupsTest extends BaseWebScriptTest
assertEquals("shortName wrong", TEST_ROOTGROUP, rootGroup.getString("shortName"));
assertEquals("displayName wrong", TEST_ROOTGROUP_DISPLAY_NAME, rootGroup.getString("displayName"));
assertEquals("authorityType wrong", "GROUP", rootGroup.getString("authorityType"));
assertEquals("hasSubgroups wrong", true, rootGroup.getString("hasSubgroups"));
gotRootGroup = true;
}
if(rootGroup.getString("shortName").equals(EMAIL_GROUP))
@@ -270,6 +271,7 @@ public class GroupsTest extends BaseWebScriptTest
assertEquals("shortName wrong", TEST_ROOTGROUP, rootGroup.getString("shortName"));
assertEquals("displayName wrong", TEST_ROOTGROUP_DISPLAY_NAME, rootGroup.getString("displayName"));
assertEquals("authorityType wrong", "GROUP", rootGroup.getString("authorityType"));
assertEquals("hasSubgroups wrong", true, rootGroup.getString("hasSubgroups"));
}
}
}
@@ -293,6 +295,7 @@ public class GroupsTest extends BaseWebScriptTest
assertEquals("shortName wrong", TEST_ROOTGROUP, rootGroup.getString("shortName"));
assertEquals("displayName wrong", TEST_ROOTGROUP_DISPLAY_NAME, rootGroup.getString("displayName"));
assertEquals("authorityType wrong", "GROUP", rootGroup.getString("authorityType"));
assertEquals("hasSubgroups wrong", true, rootGroup.getString("hasSubgroups"));
}
}
}
@@ -383,11 +386,13 @@ public class GroupsTest extends BaseWebScriptTest
{
JSONObject newGroupJSON = new JSONObject();
newGroupJSON.put("displayName", myDisplayName);
newGroupJSON.put("description", "testDesc");
Response response = sendRequest(new PostRequest(URL_ROOTGROUPS + "/" + myGroupName, newGroupJSON.toString(), "application/json"), Status.STATUS_CREATED);
JSONObject top = new JSONObject(response.getContentAsString());
JSONObject rootGroup = top.getJSONObject("data");
assertEquals("shortName wrong", myGroupName, rootGroup.getString("shortName"));
assertEquals("displayName wrong", myDisplayName, rootGroup.getString("displayName"));
assertEquals("description wrong", "testDesc", rootGroup.getString("description"));
}
/**
@@ -503,6 +508,17 @@ public class GroupsTest extends BaseWebScriptTest
assertEquals("authorityType wrong", "GROUP", subGroup.getString("authorityType"));
}
/**
* Check if myGroup has subgroups
*/
{
Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + myRootGroup), Status.STATUS_OK);
JSONObject top = new JSONObject(response.getContentAsString());
logger.debug(response.getContentAsString());
JSONObject myGroup = top.getJSONObject("data");
assertTrue(myGroup.getBoolean("hasSubgroups"));
}
/**
* Now link in an existing user
*/
@@ -556,6 +572,17 @@ public class GroupsTest extends BaseWebScriptTest
//assertTrue("group B not removed", data.length() == 0);
}
/**
* Check if myGroup has no subgroups
*/
{
Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + myRootGroup), Status.STATUS_OK);
JSONObject top = new JSONObject(response.getContentAsString());
logger.debug(response.getContentAsString());
JSONObject myGroup = top.getJSONObject("data");
assertFalse(myGroup.getBoolean("hasSubgroups"));
}
/**
* Create a new group (BUFFY)
*/
@@ -613,18 +640,21 @@ public class GroupsTest extends BaseWebScriptTest
{
String myGroupName = "GT_UG";
String myDisplayName = "GT_UGDisplay";
String description = "GT_UGDesc";
String myNewDisplayName = "GT_UGDisplayNew";
String newDescription = "GT_UGDescNew";
this.authenticationComponent.setSystemUserAsCurrentUser();
try
{
/**
* Create a root group
* Create a root group with descrription
*/
{
JSONObject newGroupJSON = new JSONObject();
newGroupJSON.put("displayName", myDisplayName);
newGroupJSON.put("description", description);
sendRequest(new PostRequest(URL_ROOTGROUPS + "/" + myGroupName, newGroupJSON.toString(), "application/json"), Status.STATUS_CREATED);
}
@@ -634,13 +664,14 @@ public class GroupsTest extends BaseWebScriptTest
{
JSONObject newGroupJSON = new JSONObject();
newGroupJSON.put("displayName", myNewDisplayName);
newGroupJSON.put("description", newDescription);
Response response = sendRequest(new PutRequest(URL_GROUPS + "/" + myGroupName, newGroupJSON.toString(), "application/json"), Status.STATUS_OK);
JSONObject top = new JSONObject(response.getContentAsString());
logger.debug(response.getContentAsString());
JSONObject data = top.getJSONObject("data");
assertTrue(data.length() > 0);
assertEquals("displayName wrong", myNewDisplayName, data.getString("displayName"));
assertEquals("description wrong", newDescription, data.getString("description"));
}
/**
@@ -653,7 +684,7 @@ public class GroupsTest extends BaseWebScriptTest
JSONObject data = top.getJSONObject("data");
assertTrue(data.length() > 0);
assertEquals("displayName wrong", myNewDisplayName, data.getString("displayName"));
assertEquals("description wrong", newDescription, data.getString("description"));
}
/**

View File

@@ -43,11 +43,9 @@ import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.util.GUID;
import org.alfresco.util.testing.category.LuceneTests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mock;
import jakarta.servlet.http.HttpServletResponse;
@@ -663,7 +661,9 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
assertNotNull(group);
assertNotNull(group.getId());
assertNotNull(group.getDisplayName());
assertNotNull(group.getDescription());
assertNotNull(group.getIsRoot());
assertNotNull(group.getHasSubgroups());
if (!ignoreOptionallyIncluded)
{
@@ -1421,13 +1421,16 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
otherParams.put("include", org.alfresco.rest.api.Groups.PARAM_INCLUDE_PARENT_IDS);
Group group = generateGroup();
group.setDescription("testDesc");
Group createdGroup01 = groupsProxy.createGroup(group, null, HttpServletResponse.SC_CREATED);
assertNotNull(createdGroup01);
assertNotNull(createdGroup01.getId());
assertEquals(createdGroup01.getDescription(), "testDesc");
assertTrue(createdGroup01.getIsRoot());
assertNull(createdGroup01.getParentIds());
assertFalse(createdGroup01.getHasSubgroups());
Set<String> subGroup01Parents = new HashSet<>();
subGroup01Parents.add(createdGroup01.getId());
@@ -1441,6 +1444,8 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
assertFalse(createdSubGroup01.getIsRoot());
assertNotNull(createdSubGroup01.getParentIds());
assertEquals(subGroup01Parents, createdSubGroup01.getParentIds());
assertTrue(createdGroup01.getHasSubgroups());
assertFalse(createdSubGroup01.getHasSubgroups());
}
// Group id is missing.
@@ -1623,6 +1628,7 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
subGroupParents.add(group.getId());
Group generatedSubGroup = generateGroup();
generatedSubGroup.setDescription("initialDesc");
generatedSubGroup.setParentIds(subGroupParents);
Group subGroup = groupsProxy.createGroup(generatedSubGroup, otherParams, HttpServletResponse.SC_CREATED);
@@ -1645,9 +1651,11 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
String displayName = "newDisplayName";
String description = "newDesc";
Group mySubGroup = new Group();
mySubGroup.setDisplayName(displayName);
mySubGroup.setDescription(description);
Group updateGroup = groupsProxy.updateGroup(subGroup.getId(), mySubGroup, otherParams, HttpServletResponse.SC_OK);
@@ -1657,8 +1665,9 @@ public class GroupsTest extends AbstractSingleNetworkSiteTest
assertFalse(updateGroup.getIsRoot());
assertNotNull(updateGroup.getParentIds());
// Check that only display name changed.
// Check that only display name and description changed.
assertEquals(displayName, updateGroup.getDisplayName());
assertEquals(description, updateGroup.getDescription());
// Check that nothing else changed.
assertEquals(subGroup.getId(), updateGroup.getId());