here's the method i came up with;
Code:
public void changeUserRole(User user, UserRole role) throws UserManagerException {
boolean hasAuth = false;
GrantedAuthority[] grantedAuthorities = _roleHierrarchy.getReachableGrantedAuthorities(CurrentUserManagerImpl.getUserFromSecurityContext().getAuthorities());
for(GrantedAuthority ga : grantedAuthorities) {
if(ga.toString().equalsIgnoreCase(role.toString())) hasAuth = true;
}
try {
if(hasAuth) {
_userDao.setRole(user, role);
} else {
String adminUser = CurrentUserManagerImpl.getUserFromSecurityContext().getUsername();
LOG.error(adminUser + " doesn't have rights to set: " + user.getUsername() + " to role: " + role.toString());
throw new UserManagerException(adminUser + " doesn't have rights to set: " + user.getUsername() + " to role: " + role.toString());
}
} catch (UserDaoExpection userDaoExpection) {
throw new UserManagerException(userDaoExpection.getMessage());
}
}
i'm injecting the roleHierrarchy bean from my security context config.