NIHVIVO-3523 Re-write Permission as an abstract class rather than an interface. Make it Comparable. Remove the unused methods for getLocalName() and getNamespace()
This commit is contained in:
parent
45e8d0c654
commit
9d55acb2f3
3 changed files with 52 additions and 72 deletions
|
@ -8,38 +8,9 @@ import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAct
|
|||
* This is what the PermissionRegistry hands out if you ask for a Permission
|
||||
* that it doesn't know about. Nothing is authorized by this Permission.
|
||||
*/
|
||||
public class BrokenPermission implements Permission {
|
||||
private final String uri;
|
||||
private final String localName;
|
||||
private final String namespace;
|
||||
|
||||
public class BrokenPermission extends Permission {
|
||||
public BrokenPermission(String uri) {
|
||||
this.uri = uri;
|
||||
|
||||
int namespaceBreak = uri.lastIndexOf("#");
|
||||
if (namespaceBreak == -1) {
|
||||
namespaceBreak = uri.lastIndexOf("/");
|
||||
}
|
||||
|
||||
int localNameStart = namespaceBreak + 1;
|
||||
|
||||
this.namespace = uri.substring(0, localNameStart);
|
||||
this.localName = uri.substring(localNameStart);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalName() {
|
||||
return localName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
super(uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -8,47 +8,64 @@ import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAct
|
|||
* Interface that describes a unit of authorization, or permission to perform
|
||||
* requested actions.
|
||||
*/
|
||||
public interface Permission {
|
||||
public abstract class Permission implements Comparable<Permission> {
|
||||
protected final String uri;
|
||||
|
||||
protected Permission(String uri) {
|
||||
if (uri == null) {
|
||||
throw new NullPointerException("uri may not be null.");
|
||||
}
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URI that identifies this Permission object.
|
||||
*/
|
||||
String getUri();
|
||||
|
||||
/**
|
||||
* Convenience method to get the localName portion of the URI.
|
||||
*/
|
||||
String getLocalName();
|
||||
|
||||
/**
|
||||
* Convenience method to get the namespace portion of the URI.
|
||||
*/
|
||||
String getNamespace();
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a user with this Permission authorized to perform this
|
||||
* RequestedAction?
|
||||
*/
|
||||
boolean isAuthorized(RequestedAction whatToAuth);
|
||||
public abstract boolean isAuthorized(RequestedAction whatToAuth);
|
||||
|
||||
@Override
|
||||
public int compareTo(Permission that) {
|
||||
return this.uri.compareTo(that.uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (!obj.getClass().equals(this.getClass())) {
|
||||
return false;
|
||||
}
|
||||
Permission that = (Permission) obj;
|
||||
return this.uri.equals(that.uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return uri.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + "['" + uri + "']";
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of Permission that authorizes nothing.
|
||||
* A concrete Permission instance that authorizes nothing.
|
||||
*/
|
||||
static Permission NOT_AUTHORIZED = new Permission() {
|
||||
|
||||
@Override
|
||||
public String getUri() {
|
||||
return "java:" + Permission.class.getName() + "#NOT_AUTHORIZED";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalName() {
|
||||
return "NOT_AUTHORIZED";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return "java:" + Permission.class.getName();
|
||||
}
|
||||
static Permission NOT_AUTHORIZED = new Permission("java:"
|
||||
+ Permission.class.getName() + "#NOT_AUTHORIZED") {
|
||||
|
||||
@Override
|
||||
public boolean isAuthorized(RequestedAction whatToAuth) {
|
||||
|
|
|
@ -18,7 +18,7 @@ import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAct
|
|||
* A class of simple permissions. Each instance holds a RequestedAction, and
|
||||
* will only authorize that RequestedAction (or one with the same URI).
|
||||
*/
|
||||
public class SimplePermission implements Permission {
|
||||
public class SimplePermission extends Permission {
|
||||
private static final Log log = LogFactory.getLog(SimplePermission.class);
|
||||
|
||||
private static final String NAMESPACE = "java:"
|
||||
|
@ -88,18 +88,17 @@ public class SimplePermission implements Permission {
|
|||
}
|
||||
|
||||
private final String localName;
|
||||
private final String uri;
|
||||
public final RequestedAction ACTION;
|
||||
public final Actions ACTIONS;
|
||||
|
||||
public SimplePermission(String localName) {
|
||||
super(NAMESPACE + localName);
|
||||
|
||||
if (localName == null) {
|
||||
throw new NullPointerException("name may not be null.");
|
||||
}
|
||||
|
||||
this.localName = localName;
|
||||
this.uri = NAMESPACE + localName;
|
||||
|
||||
this.ACTION = new SimpleRequestedAction(localName);
|
||||
this.ACTIONS = new Actions(this.ACTION);
|
||||
|
||||
|
@ -110,21 +109,14 @@ public class SimplePermission implements Permission {
|
|||
allInstances.put(uri, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalName() {
|
||||
return this.localName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUri() {
|
||||
return NAMESPACE + this.localName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAuthorized(RequestedAction whatToAuth) {
|
||||
if (whatToAuth != null) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue