| 1 |
|
package org.trails.security; |
| 2 |
|
|
| 3 |
|
import org.acegisecurity.GrantedAuthority; |
| 4 |
|
import org.apache.commons.logging.Log; |
| 5 |
|
import org.apache.commons.logging.LogFactory; |
| 6 |
|
import org.trails.descriptor.IClassDescriptor; |
| 7 |
|
|
| 8 |
|
public abstract class SecurityRestriction |
| 9 |
|
{ |
| 10 |
|
|
| 11 |
3 |
private static final Log LOG = LogFactory.getLog(SecurityRestriction.class); |
| 12 |
|
|
| 13 |
|
public SecurityRestriction() |
| 14 |
|
{ |
| 15 |
141 |
super(); |
| 16 |
|
|
| 17 |
141 |
} |
| 18 |
|
|
| 19 |
|
private String requiredRole[]; |
| 20 |
|
|
| 21 |
|
private RestrictionType restrictionType; |
| 22 |
|
|
| 23 |
|
public String[] getRequiredRole() |
| 24 |
|
{ |
| 25 |
6 |
return requiredRole; |
| 26 |
|
} |
| 27 |
|
|
| 28 |
|
public void setRequiredRole(String[] requiredRole) |
| 29 |
|
{ |
| 30 |
141 |
if (requiredRole == null) this.requiredRole = new String[]{}; |
| 31 |
141 |
else this.requiredRole = requiredRole; |
| 32 |
141 |
} |
| 33 |
|
|
| 34 |
|
public RestrictionType getRestrictionType() |
| 35 |
|
{ |
| 36 |
123 |
return restrictionType; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
public void setRestrictionType(RestrictionType restrictionType) |
| 40 |
|
{ |
| 41 |
150 |
this.restrictionType = restrictionType; |
| 42 |
150 |
} |
| 43 |
|
|
| 44 |
|
protected boolean hasRequiredRole(GrantedAuthority[] authorities) |
| 45 |
|
{ |
| 46 |
249 |
for (GrantedAuthority authority : authorities) |
| 47 |
132 |
for (String role : requiredRole) if (role.equals(authority.getAuthority()) ) return true; |
| 48 |
117 |
return false; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
protected abstract void applyRestriction(IClassDescriptor classDescriptor); |
| 52 |
|
|
| 53 |
|
public void restrict(GrantedAuthority[] authorities, IClassDescriptor classDescriptor) |
| 54 |
|
{ |
| 55 |
147 |
if (!hasRequiredRole(authorities)) |
| 56 |
|
{ |
| 57 |
117 |
applyRestriction(classDescriptor); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
147 |
} |
| 61 |
|
} |