Permissions { public static long getPermissions(@NotNull String jcrActions, @NotNull TreeLocation location, boolean isAccessControlContent) { Set<String> actions = Sets.newHashSet(Text.explode(jcrActions, ',', false)); long permissions = NO_PERMISSION; if (actions.remove(Session.ACTION_READ)) { if (isAccessControlContent) { permissions |= READ_ACCESS_CONTROL; } else if (!location.exists()) { permissions |= READ; } else if (location.getProperty() != null) { permissions |= READ_PROPERTY; } else { permissions |= READ_NODE; } } if (!actions.isEmpty()) { if (isAccessControlContent) { if (actions.removeAll(WRITE_ACTIONS)) { permissions |= MODIFY_ACCESS_CONTROL; } } else { if (actions.remove(Session.ACTION_SET_PROPERTY)) { if (location.getProperty() == null) { permissions |= ADD_PROPERTY; } else { permissions |= MODIFY_PROPERTY; } } if (actions.remove(Session.ACTION_REMOVE)) { if (!location.exists()) { permissions |= REMOVE; } else if (location.getProperty() != null) { permissions |= REMOVE_PROPERTY; } else { permissions |= REMOVE_NODE; } } } } if (!actions.isEmpty()) { for (Map.Entry<String, Long> actionEntry : ACTIONS_MAP.entrySet()) { if (actions.remove(actionEntry.getKey())) { permissions |= actionEntry.getValue(); } } permissions |= getPermissions(actions); } if (!actions.isEmpty()) { throw new IllegalArgumentException("Unknown actions: " + actions); } return permissions; } private  Permissions(); static Set<String> getNames(long permissions); static String getString(long permissions); static boolean isRepositoryPermission(long permission); static boolean isAggregate(long permission); static Iterable<Long> aggregates(final long permissions); static boolean includes(long permissions, long permissionsToTest); static boolean respectParentPermissions(long permissions); static long diff(long permissions, long otherPermissions); static long getPermissions(@NotNull String jcrActions,
                                      @NotNull TreeLocation location,
                                      boolean isAccessControlContent); static long getPermissions(@Nullable String permissionNames); static long getPermission(@Nullable String path, long defaultPermission); static final long NO_PERMISSION; static final long READ_NODE; static final long READ_PROPERTY; static final long ADD_PROPERTY; static final long MODIFY_PROPERTY; static final long REMOVE_PROPERTY; static final long ADD_NODE; static final long REMOVE_NODE; static final long READ_ACCESS_CONTROL; static final long MODIFY_ACCESS_CONTROL; static final long NODE_TYPE_MANAGEMENT; static final long VERSION_MANAGEMENT; static final long LOCK_MANAGEMENT; static final long LIFECYCLE_MANAGEMENT; static final long RETENTION_MANAGEMENT; static final long MODIFY_CHILD_NODE_COLLECTION; static final long NODE_TYPE_DEFINITION_MANAGEMENT; static final long NAMESPACE_MANAGEMENT; static final long WORKSPACE_MANAGEMENT; static final long PRIVILEGE_MANAGEMENT; static final long USER_MANAGEMENT; static final long INDEX_DEFINITION_MANAGEMENT; static final long READ; static final long REMOVE; static final long SET_PROPERTY; static final long WRITE; static final long ALL; static final Map<Long, String> PERMISSION_NAMES; }
@Test public void testGetPermissionsFromActions() { TreeLocation tl = TreeLocation.create(existingTree); Map<String, Long> map = ImmutableMap.of( Session.ACTION_READ, Permissions.READ_NODE, Session.ACTION_READ + "," + Session.ACTION_REMOVE, Permissions.READ_NODE|Permissions.REMOVE_NODE ); for (Map.Entry<String, Long> entry : map.entrySet()) { assertEquals(entry.getValue().longValue(), Permissions.getPermissions(entry.getKey(), tl, false)); } }
@Test public void testGetPermissionsFromInvalidActions() { TreeLocation tl = TreeLocation.create(existingTree); List<String> l = ImmutableList.of( Session.ACTION_READ + ",invalid", "invalid", "invalid," + Session.ACTION_REMOVE ); for (String invalid : l) { try { Permissions.getPermissions(invalid, tl, false); fail(); } catch (IllegalArgumentException e) { } } }
@Test public void testGetPermissionsFromJackrabbitActions() { TreeLocation tl = TreeLocation.create(existingTree); Map<String, Long> map = new HashMap<>(); map.put(Session.ACTION_ADD_NODE, Permissions.ADD_NODE); map.put(JackrabbitSession.ACTION_ADD_PROPERTY, Permissions.ADD_PROPERTY); map.put(JackrabbitSession.ACTION_MODIFY_PROPERTY, Permissions.MODIFY_PROPERTY); map.put(JackrabbitSession.ACTION_REMOVE_PROPERTY, Permissions.REMOVE_PROPERTY); map.put(JackrabbitSession.ACTION_REMOVE_NODE, Permissions.REMOVE_NODE); map.put(JackrabbitSession.ACTION_NODE_TYPE_MANAGEMENT, Permissions.NODE_TYPE_MANAGEMENT); map.put(JackrabbitSession.ACTION_LOCKING, Permissions.LOCK_MANAGEMENT); map.put(JackrabbitSession.ACTION_VERSIONING, Permissions.VERSION_MANAGEMENT); map.put(JackrabbitSession.ACTION_READ_ACCESS_CONTROL, Permissions.READ_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_MODIFY_ACCESS_CONTROL, Permissions.MODIFY_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_USER_MANAGEMENT, Permissions.USER_MANAGEMENT); for (Map.Entry<String, Long> entry : map.entrySet()) { assertEquals(entry.getValue().longValue(), Permissions.getPermissions(entry.getKey(), tl, false)); } }
@Test public void testGetPermissionsOnAccessControlledNode() { TreeLocation tl = createNonExistingTreeLocation(PathUtils.ROOT_PATH + AccessControlConstants.REP_POLICY); Map<String, Long> map = new HashMap<>(); map.put(Session.ACTION_READ, Permissions.READ_ACCESS_CONTROL); map.put(Session.ACTION_ADD_NODE, Permissions.MODIFY_ACCESS_CONTROL); map.put(Session.ACTION_REMOVE, Permissions.MODIFY_ACCESS_CONTROL); map.put(Session.ACTION_SET_PROPERTY, Permissions.MODIFY_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_ADD_PROPERTY, Permissions.MODIFY_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_MODIFY_PROPERTY, Permissions.MODIFY_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_REMOVE_PROPERTY, Permissions.MODIFY_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_REMOVE_NODE, Permissions.MODIFY_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_READ_ACCESS_CONTROL, Permissions.READ_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_MODIFY_ACCESS_CONTROL, Permissions.MODIFY_ACCESS_CONTROL); map.put(JackrabbitSession.ACTION_LOCKING, Permissions.LOCK_MANAGEMENT); map.put(JackrabbitSession.ACTION_VERSIONING, Permissions.VERSION_MANAGEMENT); map.put(JackrabbitSession.ACTION_USER_MANAGEMENT, Permissions.USER_MANAGEMENT); for (Map.Entry<String, Long> entry : map.entrySet()) { assertEquals(entry.getKey(), entry.getValue().longValue(), Permissions.getPermissions(entry.getKey(), tl, true)); } }
@Test public void testActionRead() { TreeLocation treeLocation = TreeLocation.create(existingTree); assertNull(treeLocation.getProperty()); assertEquals(Permissions.READ_NODE, Permissions.getPermissions(Session.ACTION_READ, treeLocation, false)); assertEquals(Permissions.READ_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_READ, treeLocation, true)); TreeLocation nonExistingTree = createNonExistingTreeLocation("/nonExisting"); assertNull(nonExistingTree.getProperty()); assertEquals(Permissions.READ, Permissions.getPermissions(Session.ACTION_READ, nonExistingTree, false)); assertEquals(Permissions.READ_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_READ, nonExistingTree, true)); TreeLocation nonExistingProp = createNonExistingTreeLocation("/nonExisting").getChild("nonExisting"); assertNull(nonExistingProp.getProperty()); assertEquals(Permissions.READ, Permissions.getPermissions(Session.ACTION_READ, nonExistingProp, false)); assertEquals(Permissions.READ_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_READ, nonExistingProp, true)); TreeLocation existingProp = treeLocation.getChild(JcrConstants.JCR_PRIMARYTYPE); assertNotNull(existingProp.getProperty()); assertEquals(Permissions.READ_PROPERTY, Permissions.getPermissions(Session.ACTION_READ, existingProp, false)); assertEquals(Permissions.READ_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_READ, existingProp, true)); }
@Test public void testActionSetProperty() { TreeLocation treeLocation = TreeLocation.create(existingTree); assertNull(treeLocation.getProperty()); assertEquals(Permissions.ADD_PROPERTY, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, treeLocation, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, treeLocation, true)); TreeLocation nonExistingTree = createNonExistingTreeLocation("/nonExisting"); assertNull(nonExistingTree.getProperty()); assertEquals(Permissions.ADD_PROPERTY, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, nonExistingTree, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, nonExistingTree, true)); TreeLocation nonExistingProp = createNonExistingTreeLocation("/nonExisting").getChild("nonExisting"); assertNull(nonExistingProp.getProperty()); assertEquals(Permissions.ADD_PROPERTY, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, nonExistingProp, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, nonExistingProp, true)); TreeLocation existingProp = treeLocation.getChild(JcrConstants.JCR_PRIMARYTYPE); assertNotNull(existingProp.getProperty()); assertEquals(Permissions.MODIFY_PROPERTY, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, existingProp, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, existingProp, true)); }
@Test public void testActionRemove() { TreeLocation treeLocation = TreeLocation.create(existingTree); assertNull(treeLocation.getProperty()); assertEquals(Permissions.REMOVE_NODE, Permissions.getPermissions(Session.ACTION_REMOVE, treeLocation, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_REMOVE, treeLocation, true)); TreeLocation nonExistingTree = createNonExistingTreeLocation("/nonExisting"); assertNull(nonExistingTree.getProperty()); assertEquals(Permissions.REMOVE, Permissions.getPermissions(Session.ACTION_REMOVE, nonExistingTree, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_REMOVE, nonExistingTree, true)); TreeLocation nonExistingProp = createNonExistingTreeLocation("/nonExisting").getChild("nonExisting"); assertNull(nonExistingProp.getProperty()); assertEquals(Permissions.REMOVE, Permissions.getPermissions(Session.ACTION_REMOVE, nonExistingProp, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_REMOVE, nonExistingProp, true)); TreeLocation existingProp = treeLocation.getChild(JcrConstants.JCR_PRIMARYTYPE); assertNotNull(existingProp.getProperty()); assertEquals(Permissions.REMOVE_PROPERTY, Permissions.getPermissions(Session.ACTION_REMOVE, existingProp, false)); assertEquals(Permissions.MODIFY_ACCESS_CONTROL, Permissions.getPermissions(Session.ACTION_SET_PROPERTY, existingProp, true)); }
@Test public void testGetPermissionsNullString() { assertEquals(Permissions.NO_PERMISSION, Permissions.getPermissions(null)); }
@Test public void testGetPermissionsEmptyString() { assertEquals(Permissions.NO_PERMISSION, Permissions.getPermissions("")); }
@Test public void testGetPermissionsUnknownName() { assertEquals(Permissions.NO_PERMISSION, Permissions.getPermissions("unknown")); assertEquals(Permissions.NO_PERMISSION, Permissions.getPermissions("unknown,permission,strings")); }
@Test public void testGetPermissionsSingleName() { for (Map.Entry<Long, String> entry : Permissions.PERMISSION_NAMES.entrySet()) { assertEquals(entry.getKey().longValue(), Permissions.getPermissions(entry.getValue())); } }
@Test public void testGetPermissionsMultipleNames() { for (Map.Entry<Long, Set<Long>> entry : TEST.entrySet()) { Set<String> names = new HashSet<>(); for (long p : entry.getValue()) { names.add(Permissions.PERMISSION_NAMES.get(p)); } String s = Joiner.on(',').join(names); assertEquals(entry.getKey().longValue(), Permissions.getPermissions(s)); } }
@Test public void testGetPermissionsMultipleNamesWithMissing() { String str = Permissions.PERMISSION_NAMES.get(Permissions.READ) + ", ,," + Permissions.PERMISSION_NAMES.get(Permissions.READ); assertEquals(Permissions.READ, Permissions.getPermissions(str)); }