private Stream<UserGroup> streamAllContainingGroups() { Set<UserGroup> groups = getContainingGroups(); return Stream.concat(groups.stream(), groups.stream().flatMap(group -> group.getAllContainingGroups().stream())); }
@Nonnull @Override public Set<GroupPrincipal> getAllGroupPrincipals() { return streamAllContainingGroups() .map(group -> group.getRootPrincial()) .collect(Collectors.toSet()); }
@Nonnull public Stream<User> streamAllUsers() { return streamAllGroups().flatMap(g -> StreamSupport.stream(g.getUsers().spliterator(), false)); }
@Override public GroupPrincipal getPrincipal() { Set<Principal> members = StreamSupport.stream(getGroups().spliterator(), false) .map(g -> g.getPrincipal()) .collect(Collectors.toSet()); return new GroupPrincipal(getSystemName(), members); }
@Nonnull public Stream<UserGroup> streamAllGroups() { return StreamSupport.stream(getGroups().spliterator(), false).flatMap(g -> g.streamAllGroups()); }
@Override public void deleteUser(@Nonnull final User user) { this.actionsProvider.getAllowedActions(AccessController.SERVICES).ifPresent(allowed -> allowed.disableAll(user.getPrincipal())); this.membershipProvider.findAll().forEach(membership -> membership.removeMember(user.getPrincipal())); delete(user); }
/** * Creates a new user with the specified name. * * @param username the name of the user * @param ensure {@code true} to return the user if it already exists, or {@code false} to throw an exception * @return the user * @throws UserAlreadyExistsException if the user already exists and {@code ensure} is {@code false} * @throws MetadataRepositoryException if the user could not be created */ @Nonnull private User createUser(@Nonnull final String username, final boolean ensure) { final Session session = getSession(); final String safeUserName = encodeUserName(username); final String userPath = UsersPaths.userPath(username).toString(); try { final Node usersNode = session.getRootNode().getNode(UsersPaths.USERS.toString()); if (session.getRootNode().hasNode(userPath)) { if (ensure) { return JcrUtil.getJcrObject(usersNode, safeUserName, JcrUser.class); } else { throw new UserAlreadyExistsException(username); } } else { return JcrUtil.getOrCreateNode(usersNode, safeUserName, JcrUser.NODE_TYPE, JcrUser.class); } } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed attempting to create a new user with name: " + username, e); } }
/** * Creates a new group with the specified name. * * @param groupName the name of the group * @param ensure {@code true} to return the group if it already exists, or {@code false} to throw an exception * @return the group * @throws GroupAlreadyExistsException if the group already exists and {@code ensure} is {@code false} * @throws MetadataRepositoryException if the group could not be created */ @Nonnull private UserGroup createGroup(@Nonnull final String groupName, final boolean ensure) { final Session session = getSession(); final String safeGroupName = encodeGroupName(groupName); final String groupPath = UsersPaths.groupPath(safeGroupName).toString(); try { final Node groupsNode = session.getRootNode().getNode(UsersPaths.GROUPS.toString()); if (session.getRootNode().hasNode(groupPath)) { if (ensure) { return JcrUtil.getJcrObject(groupsNode, safeGroupName, JcrUserGroup.class); } else { throw new GroupAlreadyExistsException(groupName); } } else { return JcrUtil.getOrCreateNode(groupsNode, safeGroupName, JcrUserGroup.NODE_TYPE, JcrUserGroup.class); } } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed attempting to create a new group with name: " + groupName, e); } }
@Override public void deleteGroup(@Nonnull final UserGroup group) { this.actionsProvider.getAllowedActions(AccessController.SERVICES).ifPresent(allowed -> allowed.disableAll(group.getPrincipal())); this.membershipProvider.findAll().forEach(membership -> membership.removeMember(group.getPrincipal())); delete(group); }