ACS-3366 Add support for isLinkedTo to GET rule sets. (#1427)

* ACS-3364 Add permission handling to linkedToBy.

* ACS-3366 Add support for isLinkedTo to GET rule sets.
This commit is contained in:
Tom Page
2022-09-21 15:18:41 +01:00
committed by GitHub
parent c4d432b136
commit e0d52f98ae
4 changed files with 122 additions and 2 deletions

View File

@@ -447,6 +447,62 @@ public class GetRuleSetsTests extends RestTest
ruleSet.assertThat().field("isInherited").is(false); ruleSet.assertThat().field("isInherited").is(false);
} }
/** Check that a user can see that a rule set is linked to even if they don't have permission to view the linking folder. */
@Test
public void getRuleSetAndIsLinkedToWithoutPermission()
{
STEP("Create a site owned by admin and add user as a contributor");
SiteModel siteModel = dataSite.usingAdmin().createPrivateRandomSite();
dataUser.addUserToSite(user, siteModel, UserRole.SiteContributor);
STEP("Create a folder with a rule set");
FolderModel ruleFolder = dataContent.usingUser(user).usingSite(siteModel).createFolder();
RestRuleModel ruleModel = createRuleModelWithDefaultValues();
coreAPIForUser().usingNode(ruleFolder).usingDefaultRuleSet().createSingleRule(ruleModel);
STEP("Create a private folder linking to the rule set");
FolderModel linkingFolder = dataContent.usingAdmin().usingSite(siteModel).createFolder();
RestRuleSetLinkModel linkModel = new RestRuleSetLinkModel();
linkModel.setId(ruleFolder.getNodeRef());
coreAPIForAdmin().usingNode(linkingFolder).createRuleLink(linkModel);
STEP("Remove the user from the site");
dataUser.removeUserFromSite(user, siteModel);
STEP("Get the rule set and isLinkedTo field");
RestRuleSetModel ruleSet = coreAPIForUser().usingNode(ruleFolder)
.include("isLinkedTo", "linkedToBy", "owningFolder")
.getDefaultRuleSet();
restClient.assertStatusCodeIs(OK);
ruleSet.assertThat().field("isLinkedTo").is(true)
.assertThat().field("linkedToBy").isEmpty();
}
/**
* Check that if a rule set is owned and inherited but not linked to then isLinkedTo returns false.
*/
@Test
public void getRuleSetAndIsLinkedToCanBeFalse()
{
STEP("Create a site, a folder with a rule and a child folder that inherits it");
SiteModel siteModel = dataSite.usingUser(user).createPublicRandomSite();
FolderModel ruleFolder = dataContent.usingUser(user).usingSite(siteModel).createFolder();
RestRuleModel ruleModel = createRuleModelWithDefaultValues();
coreAPIForUser().usingNode(ruleFolder).usingDefaultRuleSet().createSingleRule(ruleModel);
dataContent.usingUser(user).usingResource(ruleFolder).createFolder();
STEP("Get the rule set and isLinkedTo field");
RestRuleSetModel ruleSet = coreAPIForUser().usingNode(ruleFolder)
.include("isLinkedTo")
.getDefaultRuleSet();
restClient.assertStatusCodeIs(OK);
ruleSet.assertThat().field("isLinkedTo").is(false);
}
private RestCoreAPI coreAPIForUser() private RestCoreAPI coreAPIForUser()
{ {
return restClient.authenticateUser(user).withCoreAPI(); return restClient.authenticateUser(user).withCoreAPI();

View File

@@ -48,6 +48,7 @@ public class RuleSetLoader
protected static final String INHERITED_BY = "inheritedBy"; protected static final String INHERITED_BY = "inheritedBy";
protected static final String LINKED_TO_BY = "linkedToBy"; protected static final String LINKED_TO_BY = "linkedToBy";
protected static final String IS_INHERITED = "isInherited"; protected static final String IS_INHERITED = "isInherited";
protected static final String IS_LINKED_TO = "isLinkedTo";
private static final int MAX_INHERITED_BY_SIZE = 100; private static final int MAX_INHERITED_BY_SIZE = 100;
private NodeService nodeService; private NodeService nodeService;
private RuleService ruleService; private RuleService ruleService;
@@ -98,6 +99,10 @@ public class RuleSetLoader
{ {
ruleSet.setIsInherited(loadIsInherited(ruleSetNodeRef)); ruleSet.setIsInherited(loadIsInherited(ruleSetNodeRef));
} }
if (includes.contains(IS_LINKED_TO))
{
ruleSet.setIsLinkedTo(loadIsLinkedTo(ruleSetNodeRef, parentRef));
}
} }
return ruleSet; return ruleSet;
} }
@@ -117,6 +122,23 @@ public class RuleSetLoader
return AuthenticationUtil.runAsSystem(() -> !ruleService.getFoldersInheritingRuleSet(ruleSetNodeRef, 1).isEmpty()); return AuthenticationUtil.runAsSystem(() -> !ruleService.getFoldersInheritingRuleSet(ruleSetNodeRef, 1).isEmpty());
} }
/**
* Check if any parents of the rule set node are not the owning folder.
*
* @param ruleSetNodeRef The rule set node.
* @param parentRef The owning folder.
* @return True if another folder links to the rule set.
*/
private Boolean loadIsLinkedTo(NodeRef ruleSetNodeRef, NodeRef parentRef)
{
return AuthenticationUtil.runAsSystem(() ->
nodeService.getParentAssocs(ruleSetNodeRef)
.stream()
.map(ChildAssociationRef::getParentRef)
.anyMatch(folder -> !folder.equals(parentRef))
);
}
public void setNodeService(NodeService nodeService) public void setNodeService(NodeService nodeService)
{ {
this.nodeService = nodeService; this.nodeService = nodeService;

View File

@@ -44,6 +44,7 @@ public class RuleSet
private List<NodeRef> inheritedBy; private List<NodeRef> inheritedBy;
private List<NodeRef> linkedToBy; private List<NodeRef> linkedToBy;
private Boolean isInherited; private Boolean isInherited;
private Boolean isLinkedTo;
public static RuleSet of(String id) public static RuleSet of(String id)
{ {
@@ -130,6 +131,26 @@ public class RuleSet
return isInherited; return isInherited;
} }
/**
* Set a flag indicating that the rule set is linked to by a folder.
*
* @param isLinkedTo The flag.
*/
public void setIsLinkedTo(Boolean isLinkedTo)
{
this.isLinkedTo = isLinkedTo;
}
/**
* Find if the rule set is linked to by a folder.
*
* @return The value of the flag.
*/
public Boolean getIsLinkedTo()
{
return isLinkedTo;
}
@Override @Override
public String toString() public String toString()
{ {
@@ -141,6 +162,7 @@ public class RuleSet
.add("inheritedBy='" + inheritedBy + "'") .add("inheritedBy='" + inheritedBy + "'")
.add("linkedToBy='" + linkedToBy + "'") .add("linkedToBy='" + linkedToBy + "'")
.add("isInherited='" + isInherited + "'") .add("isInherited='" + isInherited + "'")
.add("isLinkedTo='" + isLinkedTo + "'")
.toString() .toString()
+ '}'; + '}';
} }
@@ -158,13 +180,14 @@ public class RuleSet
&& inclusionType == ruleSet.inclusionType && inclusionType == ruleSet.inclusionType
&& Objects.equals(inheritedBy, ruleSet.inheritedBy) && Objects.equals(inheritedBy, ruleSet.inheritedBy)
&& Objects.equals(linkedToBy, ruleSet.linkedToBy) && Objects.equals(linkedToBy, ruleSet.linkedToBy)
&& Objects.equals(isInherited, ruleSet.isInherited); && Objects.equals(isInherited, ruleSet.isInherited)
&& Objects.equals(isLinkedTo, ruleSet.isLinkedTo);
} }
@Override @Override
public int hashCode() public int hashCode()
{ {
return Objects.hash(id, owningFolder, inclusionType, inheritedBy, linkedToBy, isInherited); return Objects.hash(id, owningFolder, inclusionType, inheritedBy, linkedToBy, isInherited, isLinkedTo);
} }
public static Builder builder() public static Builder builder()
@@ -180,6 +203,7 @@ public class RuleSet
private List<NodeRef> inheritedBy; private List<NodeRef> inheritedBy;
private List<NodeRef> linkedToBy; private List<NodeRef> linkedToBy;
private Boolean isInherited; private Boolean isInherited;
private Boolean isLinkedTo;
public Builder id(String id) public Builder id(String id)
{ {
@@ -217,6 +241,12 @@ public class RuleSet
return this; return this;
} }
public Builder isLinkedTo(Boolean isLinkedTo)
{
this.isLinkedTo = isLinkedTo;
return this;
}
public RuleSet create() public RuleSet create()
{ {
final RuleSet ruleSet = new RuleSet(); final RuleSet ruleSet = new RuleSet();
@@ -226,6 +256,7 @@ public class RuleSet
ruleSet.setInheritedBy(inheritedBy); ruleSet.setInheritedBy(inheritedBy);
ruleSet.setLinkedToBy(linkedToBy); ruleSet.setLinkedToBy(linkedToBy);
ruleSet.setIsInherited(isInherited); ruleSet.setIsInherited(isInherited);
ruleSet.setIsLinkedTo(isLinkedTo);
return ruleSet; return ruleSet;
} }
} }

View File

@@ -28,6 +28,7 @@ package org.alfresco.rest.api.impl.rules;
import static org.alfresco.rest.api.impl.rules.RuleSetLoader.INCLUSION_TYPE; import static org.alfresco.rest.api.impl.rules.RuleSetLoader.INCLUSION_TYPE;
import static org.alfresco.rest.api.impl.rules.RuleSetLoader.INHERITED_BY; import static org.alfresco.rest.api.impl.rules.RuleSetLoader.INHERITED_BY;
import static org.alfresco.rest.api.impl.rules.RuleSetLoader.IS_INHERITED; import static org.alfresco.rest.api.impl.rules.RuleSetLoader.IS_INHERITED;
import static org.alfresco.rest.api.impl.rules.RuleSetLoader.IS_LINKED_TO;
import static org.alfresco.rest.api.impl.rules.RuleSetLoader.LINKED_TO_BY; import static org.alfresco.rest.api.impl.rules.RuleSetLoader.LINKED_TO_BY;
import static org.alfresco.rest.api.impl.rules.RuleSetLoader.OWNING_FOLDER; import static org.alfresco.rest.api.impl.rules.RuleSetLoader.OWNING_FOLDER;
import static org.alfresco.rest.api.model.rules.InclusionType.INHERITED; import static org.alfresco.rest.api.model.rules.InclusionType.INHERITED;
@@ -170,4 +171,14 @@ public class RuleSetLoaderTest extends TestCase
RuleSet expected = RuleSet.builder().id(RULE_SET_ID).isInherited(true).create(); RuleSet expected = RuleSet.builder().id(RULE_SET_ID).isInherited(true).create();
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testLoadRuleSet_isLinkedTo()
{
// Call the method under test.
RuleSet actual = ruleSetLoader.loadRuleSet(RULE_SET_NODE, FOLDER_NODE, List.of(IS_LINKED_TO));
RuleSet expected = RuleSet.builder().id(RULE_SET_ID).isLinkedTo(true).create();
assertEquals(expected, actual);
}
} }