PrivilegeBits implements PrivilegeConstants { public static long calculatePermissions(@NotNull PrivilegeBits bits, @NotNull PrivilegeBits parentBits, boolean isAllow) { long privs = bits.d.longValue(); long parentPrivs = parentBits.d.longValue(); long perm = Permissions.NO_PERMISSION; if ((privs & READ) == READ) { perm |= Permissions.READ; } else { if ((privs & READ_NODES) == READ_NODES) { perm |= Permissions.READ_NODE; } else if (((privs & READ_PROPERTIES) == READ_PROPERTIES)) { perm |= Permissions.READ_PROPERTY; } } if ((privs & MODIFY_PROPERTIES) == MODIFY_PROPERTIES) { perm |= Permissions.SET_PROPERTY; } else { if ((privs & ADD_PROPERTIES) == ADD_PROPERTIES) { perm |= Permissions.ADD_PROPERTY; } if ((privs & ALTER_PROPERTIES) == ALTER_PROPERTIES) { perm |= Permissions.MODIFY_PROPERTY; } if ((privs & REMOVE_PROPERTIES) == REMOVE_PROPERTIES) { perm |= Permissions.REMOVE_PROPERTY; } } if ((parentPrivs & ADD_CHILD_NODES) == ADD_CHILD_NODES) { perm |= Permissions.ADD_NODE; } if (isAllow) { if ((parentPrivs & REMOVE_CHILD_NODES) == REMOVE_CHILD_NODES && (privs & REMOVE_NODE) == REMOVE_NODE) { perm |= Permissions.REMOVE_NODE; } } else { if ((parentPrivs & REMOVE_CHILD_NODES) == REMOVE_CHILD_NODES || (privs & REMOVE_NODE) == REMOVE_NODE) { perm |= Permissions.REMOVE_NODE; } } if ((privs & ADD_CHILD_NODES) == ADD_CHILD_NODES && (privs & REMOVE_CHILD_NODES) == REMOVE_CHILD_NODES) { perm |= Permissions.MODIFY_CHILD_NODE_COLLECTION; } if ((privs & READ_AC) == READ_AC) { perm |= Permissions.READ_ACCESS_CONTROL; } if ((privs & MODIFY_AC) == MODIFY_AC) { perm |= Permissions.MODIFY_ACCESS_CONTROL; } if ((privs & LIFECYCLE_MNGMT) == LIFECYCLE_MNGMT) { perm |= Permissions.LIFECYCLE_MANAGEMENT; } if ((privs & LOCK_MNGMT) == LOCK_MNGMT) { perm |= Permissions.LOCK_MANAGEMENT; } if ((privs & NODE_TYPE_MNGMT) == NODE_TYPE_MNGMT) { perm |= Permissions.NODE_TYPE_MANAGEMENT; } if ((privs & RETENTION_MNGMT) == RETENTION_MNGMT) { perm |= Permissions.RETENTION_MANAGEMENT; } if ((privs & VERSION_MNGMT) == VERSION_MNGMT) { perm |= Permissions.VERSION_MANAGEMENT; } if ((privs & WORKSPACE_MNGMT) == WORKSPACE_MNGMT) { perm |= Permissions.WORKSPACE_MANAGEMENT; } if ((privs & NODE_TYPE_DEF_MNGMT) == NODE_TYPE_DEF_MNGMT) { perm |= Permissions.NODE_TYPE_DEFINITION_MANAGEMENT; } if ((privs & NAMESPACE_MNGMT) == NAMESPACE_MNGMT) { perm |= Permissions.NAMESPACE_MANAGEMENT; } if ((privs & PRIVILEGE_MNGMT) == PRIVILEGE_MNGMT) { perm |= Permissions.PRIVILEGE_MANAGEMENT; } if ((privs & USER_MNGMT) == USER_MNGMT) { perm |= Permissions.USER_MANAGEMENT; } if ((privs & INDEX_DEFINITION_MNGMT) == INDEX_DEFINITION_MNGMT) { perm |= Permissions.INDEX_DEFINITION_MANAGEMENT; } return perm; } private  PrivilegeBits(Data d); static PrivilegeBits getInstance(); @NotNull static PrivilegeBits getInstance(@NotNull PrivilegeBits... base); @NotNull static PrivilegeBits getInstance(@Nullable PropertyState property); @NotNull static PrivilegeBits getInstance(@Nullable Tree tree); static long calculatePermissions(@NotNull PrivilegeBits bits,
                                            @NotNull PrivilegeBits parentBits,
                                            boolean isAllow); boolean isEmpty(); @NotNull PrivilegeBits unmodifiable(); @NotNull PrivilegeBits modifiable(); boolean includes(@NotNull PrivilegeBits otherBits); boolean isBuiltin(); @NotNull PrivilegeBits add(@NotNull PrivilegeBits other); @NotNull PrivilegeBits diff(@NotNull PrivilegeBits other); @NotNull PrivilegeBits addDifference(@NotNull PrivilegeBits a, @NotNull PrivilegeBits b); @NotNull PrivilegeBits retain(@NotNull PrivilegeBits other); @NotNull PropertyState asPropertyState(@NotNull String name); @NotNull PrivilegeBits nextBits(); void writeTo(@NotNull Tree tree); @Override int hashCode(); @Override boolean equals(Object o); @Override String toString(); static final PrivilegeBits EMPTY; static final Map<String, PrivilegeBits> BUILT_IN; static final PrivilegeBits NEXT_AFTER_BUILT_INS; }
@Test public void testCalculatePermissionsModifyPropertyAggregated() { PrivilegeBitsProvider provider = new PrivilegeBitsProvider(mock(Root.class)); PrivilegeBits add_change = provider.getBits(REP_ADD_PROPERTIES, REP_ALTER_PROPERTIES); long permissions = (Permissions.ADD_PROPERTY | Permissions.MODIFY_PROPERTY); assertEquals(permissions, PrivilegeBits.calculatePermissions(add_change, PrivilegeBits.EMPTY, true)); assertEquals(permissions, PrivilegeBits.calculatePermissions(add_change, add_change, true)); PrivilegeBits add_rm = provider.getBits(REP_ADD_PROPERTIES, REP_REMOVE_PROPERTIES); permissions = (Permissions.ADD_PROPERTY | Permissions.REMOVE_PROPERTY); assertEquals(permissions, PrivilegeBits.calculatePermissions(add_rm, PrivilegeBits.EMPTY, true)); assertEquals(permissions, PrivilegeBits.calculatePermissions(add_rm, add_rm, true)); PrivilegeBits ch_rm = provider.getBits(REP_ALTER_PROPERTIES, REP_REMOVE_PROPERTIES); permissions = (Permissions.MODIFY_PROPERTY | Permissions.REMOVE_PROPERTY); assertEquals(permissions, PrivilegeBits.calculatePermissions(ch_rm, PrivilegeBits.EMPTY, true)); assertEquals(permissions, PrivilegeBits.calculatePermissions(ch_rm, add_rm, true)); }
@Test public void testCalculatePermissionsParentAwareAllow() { PrivilegeBitsProvider provider = new PrivilegeBitsProvider(mock(Root.class)); PrivilegeBits addChild = provider.getBits(JCR_ADD_CHILD_NODES); assertNotEquals(Permissions.ADD_NODE, PrivilegeBits.calculatePermissions(addChild, PrivilegeBits.EMPTY, true)); assertEquals(Permissions.ADD_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, addChild, true)); PrivilegeBits removeChild = provider.getBits(JCR_REMOVE_CHILD_NODES); assertNotEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(removeChild, PrivilegeBits.EMPTY, true)); assertNotEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, removeChild, true)); PrivilegeBits removeNode = provider.getBits(JCR_REMOVE_NODE); assertNotEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(removeNode, PrivilegeBits.EMPTY, true)); assertNotEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, removeNode, true)); PrivilegeBits remove = provider.getBits(JCR_REMOVE_CHILD_NODES, JCR_REMOVE_NODE); assertNotEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(remove, PrivilegeBits.EMPTY, true)); assertNotEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, remove, true)); assertEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(remove, remove, true)); }
@Test public void testCalculatePermissionsParentAwareDeny() { PrivilegeBitsProvider provider = new PrivilegeBitsProvider(mock(Root.class)); PrivilegeBits addChild = provider.getBits(JCR_ADD_CHILD_NODES); assertNotEquals(Permissions.ADD_NODE, PrivilegeBits.calculatePermissions(addChild, PrivilegeBits.EMPTY, false)); assertEquals(Permissions.ADD_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, addChild, false)); PrivilegeBits removeChild = provider.getBits(JCR_REMOVE_CHILD_NODES); assertEquals(Permissions.NO_PERMISSION, PrivilegeBits.calculatePermissions(removeChild, PrivilegeBits.EMPTY, false)); assertEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, removeChild, false)); PrivilegeBits removeNode = provider.getBits(JCR_REMOVE_NODE); assertEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(removeNode, PrivilegeBits.EMPTY, false)); assertNotEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, removeNode, false)); PrivilegeBits remove = provider.getBits(JCR_REMOVE_CHILD_NODES, JCR_REMOVE_NODE); assertEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(remove, PrivilegeBits.EMPTY, false)); assertEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(PrivilegeBits.EMPTY, remove, false)); assertEquals(Permissions.REMOVE_NODE, PrivilegeBits.calculatePermissions(remove, remove, false)); }