if (site == null) {
logger.error("Site context not available during user lookup");
throw new UsernameNotFoundException("No site context available");
}
User user = loadUser(name, site);
if (user == null) {
throw new UsernameNotFoundException(name);
} else {
// By default, add the anonymous role so the user is able to access
// publicly available resources
user.addPublicCredentials(SystemRole.GUEST);
// Collect the set of roles (granted authorities) for this users
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
for (Object o : user.getPublicCredentials(Role.class)) {
Role masterRole = (Role) o;
for (Role r : masterRole.getClosure()) {
authorities.add(new SimpleGrantedAuthority(r.getContext() + ":" + r.getIdentifier()));
// Every role may or may not be a system role or - in case of non-
// system roles, may or may not be including one or more of those
// roles. Let's ask for a translation and then add those roles
// to the set of granted authorities
Role[] systemEquivalents = getSystemRoles(r);
for (Role systemRole : systemEquivalents) {
authorities.add(new SimpleGrantedAuthority(systemRole.getContext() + ":" + systemRole.getIdentifier()));
user.addPublicCredentials(systemRole);
}
}
}
// Make sure there is no ambiguous information with regards to passwords
Set<Object> passwords = user.getPrivateCredentials(Password.class);
if (passwords.size() > 1) {
logger.warn("User '{}@{}' has more than one password'", name, site.getIdentifier());
throw new DataRetrievalFailureException("User '" + user + "' has more than one password");
} else if (passwords.size() == 0) {
logger.warn("User '{}@{}' has no password", name, site.getIdentifier());