001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.auth.webac;
007
008import java.net.URI;
009
010import org.apache.shiro.authz.Permission;
011
012/**
013 * A WebAC permission represents a particular mode of access (e.g., acl:read) to a particular resource. Both the mode
014 * and resource are URIs. One WebAC permission implies another if and only if their mode and resource URIs are both
015 * equal to the other's.
016 *
017 * @author peichman
018 */
019public class WebACPermission implements Permission {
020
021    private final URI resource;
022
023    private final URI mode;
024
025    /**
026     * @param mode ACL access mode
027     * @param resource resource to be accessed
028     */
029    public WebACPermission(final URI mode, final URI resource) {
030        this.mode = mode;
031        this.resource = resource;
032    }
033
034    /**
035     * One WebACPermission implies another if they are equal (i.e., have the same mode and resource URIs).
036     *
037     * @param p permission to compare to
038     */
039    @Override
040    public boolean implies(final Permission p) {
041        return equals(p);
042    }
043
044    /**
045     * One WebACPermission equals another if they have the same mode and resource URIs.
046     *
047     * @param o object to compare to
048     */
049    @Override
050    public boolean equals(final Object o) {
051        if (o instanceof WebACPermission) {
052            final WebACPermission perm = (WebACPermission) o;
053            return perm.getResource().equals(resource) && perm.getMode().equals(mode);
054        } else {
055            return false;
056        }
057    }
058
059    @Override
060    public int hashCode() {
061        final int prime = 31;
062        int result = 1;
063        result = prime * result + ((mode == null) ? 0 : mode.hashCode());
064        result = prime * result + ((resource == null) ? 0 : resource.hashCode());
065        return result;
066    }
067
068    /**
069     * @return the mode
070     */
071    private URI getMode() {
072        return mode;
073    }
074
075    /**
076     * @return the resource
077     */
078    private URI getResource() {
079        return resource;
080    }
081
082    @Override
083    public String toString() {
084        return "[" + mode.toString() + " " + resource.toString() + "]";
085    }
086
087}