Results 1 to 2 of 2

Thread: programmaticly check is principial role against RoleHierarchyVoter voter

  1. #1

    Default programmaticly check is principial role against RoleHierarchyVoter voter

    i'm using RoleHierarchyVoter and would like to programmaticly check if an user role has rights to any give role after they log in.

    i'm working on some user managerment stuff and would like to prevent a user setting his or somebody else's role greater than their own.

  2. #2

    Lightbulb my solution

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •