/** * Fetches just the Roles associated with the corresponding DefaultUserImpl. * * @param userId the id of the user * @return the DefaultUserImpl's set of Roles or an empty Set. */ public Set<ISecurityRole> getUserRoles(Long userId) { checkArgument(userId != null, "getUserRoles() requires a non-null userId parameter."); Iterator<UserRolePermissionJoinRow> baseResults = isEnabledColumnUsed() ? getEnabledUserRolesAndPermissions(userId) : getUserRolesAndPermissions(userId); DefaultUserImpl u = extractObjectGraphFromJoinResults(baseResults); return u != null ? u.getRoles() : Collections.<ISecurityRole>emptySet(); }
/** * Helper method for JDBI SQL Object. Builds a single DefaultUserImpl with associated * DefaultRoleImpl & Permission sub-graph from tuples each of which was fetched into a * UserRolePermissionJoinRow instance. * * @param i an iterator of the collection of UserRolePermissionJoinRow instances. * @return a new DefaultUserImpl instance with fields and Roles/Permissions set. */ protected DefaultUserImpl extractObjectGraphFromJoinResults( Iterator<UserRolePermissionJoinRow> i) { DefaultUserImpl u = null; Map<String, ISecurityRole> roles = new HashMap<>(); while (i.hasNext()) { UserRolePermissionJoinRow row = i.next(); if (u == null) { u = new DefaultUserImpl(); u.setId(row.getUserId()); u.setUsername(row.getUsername()); u.setPassword(row.getPassword()); } // Could check that the user_id and username (etc.) are the same on all results. String roleName = row.getRoleName(); String permission = row.getPermission(); if (roleName != null) { if (!roles.containsKey(roleName)) { roles.put(roleName, new DefaultRoleImpl(roleName)); } if (permission != null) { roles.get(roleName).getPermissions().add(permission); } else { LOG.warn("Record found with null permission for role '{}'.", roleName); } } else if (permission != null) { LOG.warn( "RoleName is null, but has a permission value of '{}'. How can that be?", permission); } } if (u != null) { u.setRoles(new HashSet<>(roles.values())); } return u; }
public Set<ISecurityRole> getUserRoles(String username) { DefaultUserImpl u = findUser(username); // We'd need to do a 4-way join anyway, so just call findUser() return u != null ? u.getRoles() : Collections.<ISecurityRole>emptySet(); }