/** * Sets the modifiable list of users. This method clears the current list * and adds all entries in the parameter list. * * @param users * A list of users. */ public void setUsers(List<User> users) { synchronized (getUsers()) { if (users != getUsers()) { getUsers().clear(); if (users != null) { getUsers().addAll(users); } } } }
/** * Maps a user defined in a component to a role defined in the application. * * @param user * The source user. * @param application * The parent application. Can't be null. * @param roleName * The target role name. * @throws IllegalArgumentException * If application is null. */ public void map(User user, Application application, String roleName) { map(user, Role.get(application, roleName, null)); }
/** * Finds the set of groups where a given user is a member. * * @param user * The member user. * @param inheritOnly * Indicates if only the ancestors groups that have their * "inheritRoles" property enabled should be added. * @return The set of groups. */ public Set<Group> findGroups(User user, boolean inheritOnly) { Set<Group> result = new HashSet<Group>(); List<Group> stack; // Recursively find user groups for (Group group : getRootGroups()) { stack = new ArrayList<Group>(); addGroups(user, result, group, stack, inheritOnly); } return result; }
MemoryRealm realm = new MemoryRealm(); User homer = new User("chunkylover53", "pwd", "Homer", "Simpson", "homer@simpson.org"); realm.getUsers().add(homer); realm.map(homer, app.getRole("CFO")); realm.map(homer, app.getRole("User")); realm.getUsers().add(marge); realm.map(marge, app.getRole("User")); realm.getUsers().add(bart); realm.map(bart, app.getRole("User")); realm.getUsers().add(lisa); realm.map(lisa, app.getRole("User")); app.getContext().setDefaultEnroler(realm.getEnroler()); app.getContext().setDefaultVerifier(realm.getVerifier());
public void enrole(ClientInfo clientInfo) { User user = findUser(clientInfo.getUser().getIdentifier()); if (user != null) { // Find all the inherited groups of this user Set<Group> userGroups = findGroups(user); // Add roles specific to this user Set<Role> userRoles = findRoles(user); for (Role role : userRoles) { clientInfo.getRoles().add(role); } // Add roles common to group members Set<Role> groupRoles = findRoles(userGroups); for (Role role : groupRoles) { clientInfo.getRoles().add(role); } } } }
/** * Maps a user defined in a component to a role defined in the application. * * @param user * The source user. * @param role * The target role. */ public void map(User user, Role role) { getRoleMappings().add(new RoleMapping(user, role)); }
@Override public int verify(String identifier, char[] secret) { char[] actualSecret = null; User user = findUser(identifier); if (user != null) { actualSecret = user.getSecret(); } return compare(secret, actualSecret) ? RESULT_VALID : RESULT_INVALID; } }
/** * Finds the set of groups where a given user is a member. Note that * inheritable ancestors groups are also returned. * * @param user * The member user. * @return The set of groups. */ public Set<Group> findGroups(User user) { return findGroups(user, true); }
/** * Sets the modifiable list of root groups. This method clears the current * list and adds all entries in the parameter list. * * @param rootGroups * A list of root groups. */ public void setRootGroups(List<Group> rootGroups) { synchronized (getRootGroups()) { if (rootGroups != getRootGroups()) { getRootGroups().clear(); if (rootGroups != null) { getRootGroups().addAll(rootGroups); } } } }
addGroups(user, userGroups, group, stack, inheritOnly);
MemoryRealm realm = new MemoryRealm(); User homer = new User("chunkylover53", "pwd", "Homer", "Simpson", "homer@simpson.org"); realm.getUsers().add(homer); realm.map(homer, app.getRole("CFO")); realm.map(homer, app.getRole("User")); realm.getUsers().add(marge); realm.map(marge, app.getRole("User")); realm.getUsers().add(bart); realm.map(bart, app.getRole("User")); realm.getUsers().add(lisa); realm.map(lisa, app.getRole("User")); app.getContext().setDefaultEnroler(realm.getEnroler()); app.getContext().setDefaultVerifier(realm.getVerifier());
/** * Maps a group defined in a component to a role defined in the application. * * @param group * The source group. * @param role * The target role. */ public void map(Group group, Role role) { getRoleMappings().add(new RoleMapping(group, role)); }
@Override protected User createUser(String identifier, Request request, Response response) { User result = new User(identifier); // Find the reference user User user = findUser(identifier); if (user != null) { // Copy the properties of the reference user result.setEmail(user.getEmail()); result.setFirstName(user.getFirstName()); result.setLastName(user.getLastName()); } return result; }
/** * Unmaps an element (user, group or organization) defined in a component * from a role defined in the application. * * @param group * The source group. * @param role * The target role. */ private void unmap(Object source, Role role) { RoleMapping mapping; for (int i = getRoleMappings().size(); i >= 0; i--) { mapping = getRoleMappings().get(i); if (mapping.getSource().equals(source) && mapping.getTarget().equals(role)) { getRoleMappings().remove(i); } } }
/** * Finds a user in the organization based on its identifier. * * @param userIdentifier * The identifier to match. * @return The matched user or null. */ public User findUser(String userIdentifier) { User result = null; User user; for (int i = 0; (result == null) && (i < getUsers().size()); i++) { user = getUsers().get(i); if (user.getIdentifier().equals(userIdentifier)) { result = user; } } return result; }
/** * Finds the roles mapped to a given user. * * @param user * The user. * @return The roles found. */ public Set<Role> findRoles(User user) { Set<Role> result = new HashSet<Role>(); Object source; for (RoleMapping mapping : getRoleMappings()) { source = mapping.getSource(); if ((user != null) && user.equals(source)) { result.add(mapping.getTarget()); } } return result; }
/** * Finds the roles mapped to given user group. * * @param userGroup * The user group. * @return The roles found. */ public Set<Role> findRoles(Group userGroup) { Set<Role> result = new HashSet<Role>(); Object source; for (RoleMapping mapping : getRoleMappings()) { source = mapping.getSource(); if ((userGroup != null) && userGroup.equals(source)) { result.add(mapping.getTarget()); } } return result; }
/** * Finds the roles mapped to given user groups. * * @param userGroups * The user groups. * @return The roles found. */ public Set<Role> findRoles(Set<Group> userGroups) { Set<Role> result = new HashSet<Role>(); Object source; for (RoleMapping mapping : getRoleMappings()) { source = mapping.getSource(); if ((userGroups != null) && userGroups.contains(source)) { result.add(mapping.getTarget()); } } return result; }
/** * Finds the roles mapped to given user groups. * * @param application * The parent application. Can't be null. * @param userGroups * The user groups. * @return The roles found. * @throws IllegalArgumentException * If application is null. */ public Set<Role> findRoles(Application application, Set<Group> userGroups) { if (application == null) { throw new IllegalArgumentException( "The application argument can't be null"); } Set<Role> result = new HashSet<Role>(); Object source; for (RoleMapping mapping : getRoleMappings()) { source = mapping.getSource(); if ((userGroups != null) && userGroups.contains(source)) { if (mapping.getTarget().getApplication() == application) { result.add(mapping.getTarget()); } } } return result; }
/** * Finds the roles mapped to a given user group. * * @param application * The parent application. Can't be null. * @param userGroup * The user group. * @return The roles found. * @throws IllegalArgumentException * If application is null. */ public Set<Role> findRoles(Application application, Group userGroup) { if (application == null) { throw new IllegalArgumentException( "The application argument can't be null"); } Set<Role> result = new HashSet<Role>(); Object source; for (RoleMapping mapping : getRoleMappings()) { source = mapping.getSource(); if ((userGroup != null) && userGroup.equals(source)) { if (mapping.getTarget().getApplication() == application) { result.add(mapping.getTarget()); } } } return result; }