MoveAwarePermissionValidator extends PermissionValidator { @Override public Validator childNodeAdded(String name, NodeState after) throws CommitFailedException { if (moveCtx.processAdd(getParentAfter(), name, this)) { return null; } else { return super.childNodeAdded(name, after); } } MoveAwarePermissionValidator(@NotNull NodeState rootBefore,
                                 @NotNull NodeState rootAfter,
                                 @NotNull PermissionProvider permissionProvider,
                                 @NotNull PermissionValidatorProvider provider,
                                 @NotNull MoveTracker moveTracker); private  MoveAwarePermissionValidator(@Nullable Tree parentBefore,
                                         @Nullable Tree parentAfter,
                                         @NotNull TreePermission parentPermission,
                                         @NotNull PermissionValidator parentValidator); @Override Validator childNodeAdded(String name, NodeState after); @Override Validator childNodeDeleted(String name, NodeState before);  }
@Test public void testChildNodeAddedNoMatchingMove() throws Exception { MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), new MoveTracker())); Validator validator = maValidator.childNodeAdded("name", mock(NodeState.class)); assertTrue(validator instanceof VisibleValidator); verify(maValidator, times(1)).checkPermissions(t.getChild("name"), false, Permissions.ADD_NODE); }
@Test public void testChildNodeAddedNonExistingSrc() throws Exception { MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/srcNonExisting", "/dest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), moveTracker)); Validator validator = maValidator.childNodeAdded("dest", mock(NodeState.class)); assertTrue(validator instanceof VisibleValidator); verify(maValidator, times(1)).checkPermissions(t.getChild("dest"), false, Permissions.ADD_NODE); verify(pp, never()).isGranted(t.getChild("src"), null, Permissions.REMOVE_NODE); }
@Test public void testChildNodeAddedExistingSrc() throws Exception { MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/src", "/dest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), moveTracker)); Validator validator = maValidator.childNodeAdded("dest", mock(NodeState.class)); assertNull(validator); verify(maValidator, times(1)).checkPermissions(t.getChild("dest"), false, Permissions.ADD_NODE|Permissions.NODE_TYPE_MANAGEMENT); verify(pp, times(1)).isGranted(t.getChild("src"), null, Permissions.REMOVE_NODE); }
@Test public void testChildNodeAddedNullPraent() throws Exception { MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/src", "/dest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), moveTracker)); when(maValidator.getParentAfter()).thenReturn(null); Validator validator = maValidator.childNodeAdded("dest", mock(NodeState.class)); assertTrue(validator instanceof VisibleValidator); verify(maValidator, times(1)).checkPermissions(t.getChild("dest"), false, Permissions.ADD_NODE); verify(pp, never()).isGranted(t.getChild("src"), null, Permissions.REMOVE_NODE); }
@Test(expected = CommitFailedException.class) public void testChildNodeAddedMissingPermissionAtSrc() throws Exception { grant("/", EveryonePrincipal.getInstance(), PrivilegeConstants.JCR_ADD_CHILD_NODES, PrivilegeConstants.JCR_NODE_TYPE_MANAGEMENT); MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/src", "/dest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(ImmutableSet.of(EveryonePrincipal.getInstance()), moveTracker)); try { maValidator.childNodeAdded("dest", mock(NodeState.class)); } catch (CommitFailedException e){ verify(maValidator, times(1)).checkPermissions(t.getChild("dest"), false, Permissions.ADD_NODE|Permissions.NODE_TYPE_MANAGEMENT); verify(pp, times(1)).isGranted(t.getChild("src"), null, Permissions.REMOVE_NODE); assertTrue(e.isAccessViolation()); assertEquals(0, e.getCode()); throw e; } }