View Javadoc
1   package io.oasp.module.security.common.api.accesscontrol;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Objects;
6   
7   import javax.xml.bind.annotation.XmlAttribute;
8   import javax.xml.bind.annotation.XmlElement;
9   import javax.xml.bind.annotation.XmlElementWrapper;
10  import javax.xml.bind.annotation.XmlIDREF;
11  import javax.xml.bind.annotation.XmlRootElement;
12  import javax.xml.bind.annotation.XmlSchemaType;
13  
14  /**
15   * A {@link AccessControlGroup} represents a collection of {@link AccessControlPermission permissions}. A security
16   * administrator assigns a {@link java.security.Principal user} to a {@link AccessControlGroup group} to grant him the
17   * {@link AccessControlPermission permissions} of that {@link AccessControlGroup group}.<br/>
18   * Please note that a <em>role</em> is a special form of a {@link AccessControlGroup group} that also represents a
19   * strategic function. Therefore not every {@link AccessControlGroup group} is a role. Often a user can only have one
20   * role or can only act under one role at a time. Unfortunately these terms are often mixed up what is causing
21   * confusion.
22   *
23   * @author hohwille
24   */
25  @XmlRootElement(name = "group")
26  public class AccessControlGroup extends AccessControl { // implements java.security.acl.Group {
27  
28    /** UID for serialization. */
29    private static final long serialVersionUID = 1L;
30  
31    /** @see #getInherits() */
32    @XmlIDREF
33    @XmlElementWrapper(name = "inherits")
34    @XmlElement(name = "group-ref")
35    private List<AccessControlGroup> inherits;
36  
37    /** @see #getPermissions() */
38    @XmlElementWrapper(name = "permissions")
39    @XmlElement(name = "permission")
40    private List<AccessControlPermission> permissions;
41  
42    /** @see #getType() */
43    @XmlAttribute(name = "type", required = false)
44    @XmlSchemaType(name = "string")
45    private String type;
46  
47    /**
48     * The constructor.
49     */
50    public AccessControlGroup() {
51  
52      super();
53    }
54  
55    /**
56     * The constructor.
57     *
58     * @param id the {@link #getId() ID}.
59     */
60    public AccessControlGroup(String id) {
61  
62      super(id);
63    }
64  
65    /**
66     * @return the type of this group. E.g. "role", "department", "use-case-group", etc. You can use this for your own
67     *         purpose.
68     */
69    public String getType() {
70  
71      if (this.type == null) {
72        return "";
73      }
74      return this.type;
75    }
76  
77    /**
78     * @param type the type to set
79     */
80    public void setType(String type) {
81  
82      this.type = type;
83    }
84  
85    /**
86     * @return inherits
87     */
88    public List<AccessControlGroup> getInherits() {
89  
90      if (this.inherits == null) {
91        this.inherits = new ArrayList<>();
92      }
93      return this.inherits;
94    }
95  
96    /**
97     * @param inherits the inherits to set
98     */
99    public void setInherits(List<AccessControlGroup> inherits) {
100 
101     this.inherits = inherits;
102   }
103 
104   /**
105    * @return the {@link List} of {@link AccessControlPermission}s.
106    */
107   public List<AccessControlPermission> getPermissions() {
108 
109     if (this.permissions == null) {
110       this.permissions = new ArrayList<>();
111     }
112     return this.permissions;
113   }
114 
115   /**
116    * @param permissions the new {@link #getPermissions() permissions}.
117    */
118   public void setPermissions(List<AccessControlPermission> permissions) {
119 
120     this.permissions = permissions;
121   }
122 
123   @Override
124   public int hashCode() {
125 
126     final int prime = 31;
127     int result = super.hashCode();
128     // prevent infinity loops or other sick effects
129     // result = prime * result + ((this.inherits == null) ? 0 : this.inherits.hashCode());
130     result = prime * result + ((this.permissions == null) ? 0 : this.permissions.hashCode());
131     result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
132     return result;
133   }
134 
135   @Override
136   public boolean equals(Object obj) {
137 
138     if (this == obj) {
139       return true;
140     }
141     if (!super.equals(obj)) {
142       return false;
143     }
144     if (getClass() != obj.getClass()) {
145       return false;
146     }
147     AccessControlGroup other = (AccessControlGroup) obj;
148     // prevent infinity loops or other sick effects...
149     // if (!Objects.equal(this.inherits, other.inherits)) {
150     // return false;
151     // }
152     if (!Objects.equals(this.permissions, other.permissions)) {
153       return false;
154     }
155     if (!Objects.equals(this.type, other.type)) {
156       return false;
157     }
158     return true;
159   }
160 
161 }