}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) {
throw new AuthorizationException("Cannot authorize with no principals.");
}
String username = principals.getPrimaryPrincipal().toString();
Set<String> roles = new HashSet<String>();
Set<String> realmNames = new HashSet<String>(principals.getRealmNames());
// if the user belongs to this realm, we are most likely using this realm stand alone, or for testing
if (!realmNames.contains(this.getName())) {
// make sure the realm is enabled
Collection<Realm> configureadRealms = this.securitySystem.getSecurityManager().getRealms();
boolean foundRealm = false;
for (Realm realm : configureadRealms) {
if (realmNames.contains(realm.getName())) {
foundRealm = true;
break;
}
}
if (!foundRealm) {
// user is from a realm that is NOT enabled
throw new AuthorizationException("User for principals: " + principals.getPrimaryPrincipal()
+ " belongs to a disabled realm(s): " + principals.getRealmNames() + ".");
}
}
// clean up the realm names for processing (replace the Xml*Realm with default)
cleanUpRealmList(realmNames);
if (RoleMappingUserManager.class.isInstance(userManager)) {
for (String realmName : realmNames) {
try {
for (RoleIdentifier roleIdentifier : ((RoleMappingUserManager) userManager).getUsersRoles(username,
realmName)) {
roles.add(roleIdentifier.getRoleId());
}
}
catch (UserNotFoundException e) {
if (this.logger.isTraceEnabled()) {
this.logger.trace("Failed to find role mappings for user: " + username + " realm: "
+ realmName);
}
}
}
}
else if (realmNames.contains("default")) {
try {
for (RoleIdentifier roleIdentifier : userManager.getUser(username).getRoles()) {
roles.add(roleIdentifier.getRoleId());
}
}
catch (UserNotFoundException e) {
throw new AuthorizationException("User for principals: " + principals.getPrimaryPrincipal()
+ " could not be found.", e);
}
}
else
// user not managed by this Realm
{
throw new AuthorizationException("User for principals: " + principals.getPrimaryPrincipal()
+ " not manged by XML realm.");
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);