View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.security.api;
23  
24  import org.apache.commons.lang3.StringUtils;
25  
26  import javax.xml.bind.annotation.XmlAccessType;
27  import javax.xml.bind.annotation.XmlAccessorType;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlType;
30  
31  /**
32   * A tuple of role, action, and whether the combination is to be allowed.
33   */
34  @XmlAccessorType(XmlAccessType.FIELD)
35  @XmlType(name = "ace", namespace = "http://org.opencastproject.security", propOrder = { "action", "allow", "role" })
36  @XmlRootElement(name = "ace", namespace = "http://org.opencastproject.security")
37  public final class AccessControlEntry {
38  
39    /** The role */
40    private String role = null;
41  
42    /** The action */
43    private String action = null;
44  
45    /** Whether this role is allowed to take this action */
46    private boolean allow = false;
47  
48    /**
49     * No-arg constructor needed by JAXB
50     */
51    public AccessControlEntry() {
52    }
53  
54    /**
55     * Constructs an access control entry for a role, action, and allow tuple
56     *
57     * @param role
58     *          the role
59     * @param action
60     *          the action
61     * @param allow
62     *          Whether this role is allowed to take this action
63     */
64    public AccessControlEntry(String role, String action, boolean allow) {
65      this.role = role;
66      this.action = action;
67      this.allow = allow;
68    }
69  
70    /**
71     * @return the role
72     */
73    public String getRole() {
74      return role;
75    }
76  
77    /**
78     * @return the action
79     */
80    public String getAction() {
81      return action;
82    }
83  
84    /**
85     * @return the allow
86     */
87    public boolean isAllow() {
88      return allow;
89    }
90  
91    public boolean isValid() {
92      return StringUtils.isNotBlank(role) && StringUtils.isNotBlank(action);
93    }
94  
95    /**
96     * {@inheritDoc}
97     *
98     * @see java.lang.Object#equals(java.lang.Object)
99     */
100   @Override
101   public boolean equals(Object obj) {
102     if (obj instanceof AccessControlEntry) {
103       AccessControlEntry other = (AccessControlEntry) obj;
104       return this.allow == other.allow && this.role.equals(other.role) && this.action.equals(other.action);
105     } else {
106       return false;
107     }
108   }
109 
110   /**
111    * {@inheritDoc}
112    *
113    * @see java.lang.Object#hashCode()
114    */
115   @Override
116   public int hashCode() {
117     return (role + action + Boolean.toString(allow)).hashCode();
118   }
119 
120   /**
121    * {@inheritDoc}
122    *
123    * @see java.lang.Object#toString()
124    */
125   @Override
126   public String toString() {
127     StringBuilder sb = new StringBuilder(role).append(" is ");
128     if (!allow)
129       sb.append("not ");
130     sb.append("allowed to ");
131     sb.append(action);
132     return sb.toString();
133   }
134 
135 }