AccessControlManagerImpl extends AbstractAccessControlManager implements PolicyOwner { @Override public void setPolicy(@Nullable String absPath, @NotNull AccessControlPolicy policy) throws RepositoryException { String oakPath = getOakPath(absPath); Util.checkValidPolicy(oakPath, policy); if (policy instanceof PrincipalACL) { setPrincipalBasedAcl((PrincipalACL) policy); } else { Tree tree = getTree(oakPath, Permissions.MODIFY_ACCESS_CONTROL, true); setNodeBasedAcl(oakPath, tree, (ACL) policy); } } AccessControlManagerImpl(@NotNull Root root, @NotNull NamePathMapper namePathMapper,
                                    @NotNull SecurityProvider securityProvider); @NotNull @Override AccessControlPolicy[] getPolicies(@Nullable String absPath); @NotNull @Override AccessControlPolicy[] getEffectivePolicies(@Nullable String absPath); @NotNull @Override AccessControlPolicyIterator getApplicablePolicies(@Nullable String absPath); @Override void setPolicy(@Nullable String absPath, @NotNull AccessControlPolicy policy); @Override void removePolicy(@Nullable String absPath, @NotNull AccessControlPolicy policy); @NotNull @Override JackrabbitAccessControlPolicy[] getApplicablePolicies(@NotNull Principal principal); @NotNull @Override JackrabbitAccessControlPolicy[] getPolicies(@NotNull Principal principal); @NotNull @Override AccessControlPolicy[] getEffectivePolicies(@NotNull Set<Principal> principals); @Override boolean defines(String absPath, @NotNull AccessControlPolicy accessControlPolicy);  }
@Test public void testSetPolicy() throws Exception { ACL acl = TestUtility.getApplicablePolicy(acMgr, testPath); acl.addAccessControlEntry(testPrincipal, testPrivileges); acl.addEntry(EveryonePrincipal.getInstance(), testPrivileges, false, TestUtility.getGlobRestriction("*/something", valueFactory)); acMgr.setPolicy(testPath, acl); root.commit(); Root root2 = adminSession.getLatestRoot(); AccessControlPolicy[] policies = getAccessControlManager(root2).getPolicies(testPath); assertPolicies(policies, 1); assertArrayEquals(acl.getAccessControlEntries(), ((ACL) policies[0]).getAccessControlEntries()); }
@Test public void testSetPolicyWritesAcContent() throws Exception { ACL acl = TestUtility.getApplicablePolicy(acMgr, testPath); acl.addAccessControlEntry(testPrincipal, testPrivileges); acl.addEntry(EveryonePrincipal.getInstance(), testPrivileges, false, TestUtility.getGlobRestriction("*/something", valueFactory)); acMgr.setPolicy(testPath, acl); root.commit(); Root root2 = adminSession.getLatestRoot(); Tree tree = root2.getTree(testPath); assertTrue(tree.hasChild(REP_POLICY)); Tree policyTree = tree.getChild(REP_POLICY); assertEquals(NT_REP_ACL, TreeUtil.getPrimaryTypeName(policyTree)); assertEquals(2, policyTree.getChildrenCount(3)); Iterator<Tree> children = policyTree.getChildren().iterator(); Tree ace = children.next(); assertEquals(NT_REP_GRANT_ACE, TreeUtil.getPrimaryTypeName(ace)); assertEquals(testPrincipal.getName(), TreeUtil.getString(ace, REP_PRINCIPAL_NAME)); assertEquals( newHashSet(testPrivileges), newHashSet(privilegesFromNames(TreeUtil.getStrings(ace, REP_PRIVILEGES)))); assertFalse(ace.hasChild(REP_RESTRICTIONS)); Tree ace2 = children.next(); assertEquals(NT_REP_DENY_ACE, TreeUtil.getPrimaryTypeName(ace2)); assertEquals(EveryonePrincipal.NAME, checkNotNull(ace2.getProperty(REP_PRINCIPAL_NAME)).getValue(Type.STRING)); Privilege[] privs = privilegesFromNames(TreeUtil.getNames(ace2, REP_PRIVILEGES)); assertEquals(newHashSet(testPrivileges), newHashSet(privs)); assertTrue(ace2.hasChild(REP_RESTRICTIONS)); Tree restr = ace2.getChild(REP_RESTRICTIONS); assertEquals("*/something", checkNotNull(restr.getProperty(REP_GLOB)).getValue(Type.STRING)); }
@Test public void testSetInvalidPolicy() throws Exception { try { acMgr.setPolicy(testPath, new TestACL(testPath, getRestrictionProvider(), getNamePathMapper())); fail("Setting invalid policy must fail"); } catch (AccessControlException e) { } ACL acl = setupPolicy(testPath); try { acMgr.setPolicy(testPath, new TestACL(testPath, getRestrictionProvider(), getNamePathMapper())); fail("Setting invalid policy must fail"); } catch (AccessControlException e) { } ACL repoAcl = setupPolicy(null); try { acMgr.setPolicy(testPath, repoAcl); fail("Setting invalid policy must fail"); } catch (AccessControlException e) { } try { acMgr.setPolicy(null, acl); fail("Setting invalid policy must fail"); } catch (AccessControlException e) { } }
@Test public void testSetPolicyInvalidPath() { for (String invalid : getInvalidPaths()) { try { AccessControlPolicy acl = createPolicy(invalid); acMgr.setPolicy(invalid, acl); fail("Setting access control policy with invalid path should fail"); } catch (RepositoryException e) { } } }
@Test(expected = PathNotFoundException.class) public void testSetPolicyPropertyPath() throws Exception { String path = "/jcr:primaryType"; AccessControlPolicy acl = createPolicy(path); acMgr.setPolicy(path, acl); }
@Test(expected = PathNotFoundException.class) public void testSetPolicyNonExistingNodePath() throws Exception { String path = "/non/existing"; AccessControlPolicy acl = createPolicy(path); acMgr.setPolicy(path, acl); }
@Test public void testSetPolicyAcContent() throws Exception { for (String acPath : getAcContentPaths()) { try { AccessControlPolicy acl = createPolicy(acPath); acMgr.setPolicy(acPath, acl); fail("Setting access control policy to access control content should fail"); } catch (AccessControlException e) { } } }
@Test(expected = AccessControlException.class) public void testSetPolicyAtDifferentPath() throws Exception { ACL acl = TestUtility.getApplicablePolicy(acMgr, testPath); acMgr.setPolicy("/", acl); }
@Test public void testSetPolicyWithExistingMixins() throws Exception { TreeUtil.addMixin(root.getTree(testPath), JcrConstants.MIX_LOCKABLE, root.getTree(NodeTypeConstants.NODE_TYPES_PATH), null); ACL acl = TestUtility.getApplicablePolicy(acMgr, testPath); assertTrue(acl.addAccessControlEntry(testPrincipal, testPrivileges)); acMgr.setPolicy(testPath, acl); root.commit(); assertEquals(ImmutableSet.of(JcrConstants.MIX_LOCKABLE, MIX_REP_ACCESS_CONTROLLABLE), ImmutableSet.copyOf(TreeUtil.getNames(root.getTree(testPath), JcrConstants.JCR_MIXINTYPES))); }