Created
October 18, 2011 20:03
-
-
Save runeflobakk/1296543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecurityLevelVoter implements AccessDecisionVoter { | |
private String securityLevelPrefix = "SECURITY_LEVEL_"; | |
public int vote(Authentication authentication, | |
Object object, | |
Collection<ConfigAttribute> attributes) { | |
int result = ACCESS_ABSTAIN; | |
Object principal = authentication.getPrincipal(); | |
for (ConfigAttribute attribute : attributes) { | |
if (this.supports(attribute)) { | |
result = ACCESS_DENIED; | |
if (principal instanceof MyPrincipal) { | |
int requiredSecurityLevel = getRequiredSecurityLevel(attribute); | |
int securityLevel = getUserSecurityLevel((MyPrincipal) principal); | |
if (securityLevel >= requiredSecurityLevel) { | |
return ACCESS_GRANTED; | |
} | |
} | |
} | |
} | |
return result; | |
} | |
private int getRequiredSecurityLevel(ConfigAttribute attribute) { | |
String requiredSecurityLevel = | |
removeStart(attribute.getAttribute(), securityLevelPrefix); | |
return toInt(requiredSecurityLevel, Integer.MAX_VALUE); | |
} | |
private int getUserSecurityLevel(MyPrincipal principal) { | |
return toInt(principal.getSecurityLevel(), Integer.MIN_VALUE); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment