MoveAwarePermissionValidator extends PermissionValidator { @Override public Validator childNodeDeleted(String name, NodeState before) throws CommitFailedException { if (moveCtx.processDelete(getParentBefore(), name, this)) { return null; } else { return super.childNodeDeleted(name, before); } } 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 testChildNodeDeletedNoMatchingMove() throws Exception { MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), new MoveTracker())); Validator validator = maValidator.childNodeDeleted("name", mock(NodeState.class)); assertNull(validator); verify(maValidator, times(1)).checkPermissions(t.getChild("name"), true, Permissions.REMOVE_NODE); }
@Test public void testChildNodeDeletedNonExistingDestination() throws Exception { MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/src", "/nonExistingDest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), moveTracker)); Validator validator = maValidator.childNodeDeleted("src", mock(NodeState.class)); assertNull(validator); verify(maValidator, times(1)).checkPermissions(t.getChild("src"), true, Permissions.REMOVE_NODE); verify(pp, never()).isGranted(t.getChild("nonExistingDest"), null, Permissions.ADD_NODE|Permissions.NODE_TYPE_MANAGEMENT); }
@Test public void testChildNodeDeletedExistingDestination() throws Exception { MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/src", "/dest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), moveTracker)); Validator validator = maValidator.childNodeDeleted("src", mock(NodeState.class)); assertNull(validator); verify(maValidator, times(1)).checkPermissions(t.getChild("src"), true, Permissions.REMOVE_NODE); verify(pp, times(1)).isGranted(t.getChild("dest"), null, Permissions.ADD_NODE|Permissions.NODE_TYPE_MANAGEMENT); }
@Test public void testChildNodeDeletedNullParent() throws Exception { MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/src", "/dest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(adminSession.getAuthInfo().getPrincipals(), moveTracker)); when(maValidator.getParentBefore()).thenReturn(null); Validator validator = maValidator.childNodeDeleted("src", mock(NodeState.class)); assertNull(validator); verify(maValidator, times(1)).checkPermissions(t.getChild("src"), true, Permissions.REMOVE_NODE); verify(pp, never()).isGranted(t.getChild("dest"), null, Permissions.ADD_NODE|Permissions.NODE_TYPE_MANAGEMENT); }
@Test(expected = CommitFailedException.class) public void testChildNodeDeletedMissingPermissionAtDestination() throws Exception { grant(PathUtils.ROOT_PATH, EveryonePrincipal.getInstance(), PrivilegeConstants.JCR_REMOVE_CHILD_NODES, PrivilegeConstants.JCR_REMOVE_NODE); MoveTracker moveTracker = new MoveTracker(); moveTracker.addMove("/src", "/dest"); MoveAwarePermissionValidator maValidator = spy(createRootValidator(ImmutableSet.of(EveryonePrincipal.getInstance()), moveTracker)); try { maValidator.childNodeDeleted("src", mock(NodeState.class)); } catch (CommitFailedException e){ verify(maValidator, times(1)).checkPermissions(t.getChild("src"), true, Permissions.REMOVE_NODE); verify(pp, times(1)).isGranted(t.getChild("dest"), null, Permissions.ADD_NODE|Permissions.NODE_TYPE_MANAGEMENT); assertTrue(e.isAccessViolation()); assertEquals(0, e.getCode()); throw e; } }