001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.auth.webac;
019
020import java.net.URI;
021
022import org.apache.shiro.authz.Permission;
023
024/**
025 * A WebAC permission represents a particular mode of access (e.g., acl:read) to a particular resource. Both the mode
026 * and resource are URIs. One WebAC permission implies another if and only if their mode and resource URIs are both
027 * equal to the other's.
028 *
029 * @author peichman
030 */
031public class WebACPermission implements Permission {
032
033    private final URI resource;
034
035    private final URI mode;
036
037    /**
038     * @param mode ACL access mode
039     * @param resource resource to be accessed
040     */
041    public WebACPermission(final URI mode, final URI resource) {
042        this.mode = mode;
043        this.resource = resource;
044    }
045
046    /**
047     * One WebACPermission implies another if they are equal (i.e., have the same mode and resource URIs).
048     *
049     * @param p permission to compare to
050     */
051    @Override
052    public boolean implies(final Permission p) {
053        return equals(p);
054    }
055
056    /**
057     * One WebACPermission equals another if they have the same mode and resource URIs.
058     *
059     * @param o object to compare to
060     */
061    @Override
062    public boolean equals(final Object o) {
063        if (o instanceof WebACPermission) {
064            final WebACPermission perm = (WebACPermission) o;
065            return perm.getResource().equals(resource) && perm.getMode().equals(mode);
066        } else {
067            return false;
068        }
069    }
070
071    @Override
072    public int hashCode() {
073        final int prime = 31;
074        int result = 1;
075        result = prime * result + ((mode == null) ? 0 : mode.hashCode());
076        result = prime * result + ((resource == null) ? 0 : resource.hashCode());
077        return result;
078    }
079
080    /**
081     * @return the mode
082     */
083    private URI getMode() {
084        return mode;
085    }
086
087    /**
088     * @return the resource
089     */
090    private URI getResource() {
091        return resource;
092    }
093
094    @Override
095    public String toString() {
096        return "[" + mode.toString() + " " + resource.toString() + "]";
097    }
098
099}