View Javadoc

1   /**
2    * 2007, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   package pt.digitalis.dif.controller.security.objects;
6   
7   import pt.digitalis.dif.dem.Entity;
8   import pt.digitalis.dif.utils.ObjectFormatter;
9   
10  /**
11   * Represents a grant on a resource for a given entity (user or group).
12   * 
13   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
14   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a><br/>
15   * @created Dec 10, 2007
16   */
17  public class ACLEntry {
18  
19      /**
20       * Converts an entity name to the appropriate Entity.
21       * 
22       * @param entityName
23       *            the entity name
24       * @return the entity
25       */
26      static public Entity getEntityTypeFromName(String entityName)
27      {
28  
29          Entity result = null;
30  
31          entityName = entityName.toLowerCase();
32  
33          if (entityName.equals(Entity.VALIDATOR.getName().toLowerCase()))
34              result = Entity.VALIDATOR;
35          else if (entityName.equals(Entity.PROVIDER.getName().toLowerCase()))
36              result = Entity.PROVIDER;
37          else if (entityName.equals(Entity.APPLICATION.getName().toLowerCase()))
38              result = Entity.APPLICATION;
39          else if (entityName.equals(Entity.SERVICE.getName().toLowerCase()))
40              result = Entity.SERVICE;
41          else if (entityName.equals(Entity.STAGE.getName().toLowerCase()))
42              result = Entity.STAGE;
43  
44          return result;
45      }
46  
47      /**
48       * Converts an Entity to it's type name.
49       * 
50       * @param entity
51       *            the entity
52       * @return the entity type name
53       */
54      static public String getTypeNameFromEntity(Entity entity)
55      {
56          String result = null;
57  
58          if (entity.equals(Entity.VALIDATOR))
59              result = Entity.VALIDATOR.getName();
60          else if (entity.equals(Entity.PROVIDER))
61              result = Entity.PROVIDER.getName();
62          else if (entity.equals(Entity.APPLICATION))
63              result = Entity.APPLICATION.getName();
64          else if (entity.equals(Entity.SERVICE))
65              result = Entity.SERVICE.getName();
66          else if (entity.equals(Entity.STAGE))
67              result = Entity.STAGE.getName();
68          else
69              result = "";
70  
71          return result.toLowerCase();
72      }
73  
74      /** The id of the resource (entity). */
75      private String entityID;
76  
77      /** The type of resource to grant access to. */
78      private Entity entityType;
79  
80      /** If it's a group privilege holds the group ID. */
81      private String groupID;
82  
83      /** Is this ACL entry default? */
84      private boolean isDefault;
85  
86      /** Is this ACL entry enabled? */
87      private boolean isEnabled;
88  
89      /** Is the accessed id public? */
90      private boolean publicAccess;
91  
92      /** If it's a user privilege holds the user ID. */
93      private String userID;
94  
95      /** Default ctor. */
96      public ACLEntry()
97      {
98          this.userID = null;
99          this.groupID = null;
100         this.entityType = null;
101         this.entityID = null;
102         this.publicAccess = false;
103         this.isEnabled = true;
104         this.isDefault = false;
105     }
106 
107     /**
108      * @see java.lang.Object#equals(java.lang.Object)
109      */
110     @Override
111     public boolean equals(Object obj)
112     {
113         ACLEntry entry = (ACLEntry) obj;
114         boolean same = true;
115 
116         // Validate ownership...
117         if (isUserACL())
118             same = same && this.getUserID().equals(entry.getUserID());
119         else if (isGroupACL())
120             same = same && this.getGroupID().equals(entry.getGroupID());
121         // else, they can still be equal ('same' is T)
122 
123         // Validate object
124         if (this.entityID != null && entry.entityID != null)
125             same = same && this.getEntityID().equals(entry.getEntityID());
126 
127         if (this.entityType != null && entry.entityType != null)
128             same = same && this.getEntityType().equals(entry.getEntityType());
129 
130         same = same && this.isDefault() == entry.isDefault();
131         same = same && this.isEnabled() == entry.isEnabled();
132         same = same && this.isPublicAccess() == entry.isPublicAccess();
133 
134         return same;
135     }
136 
137     /**
138      * Returns the ID of the entity associated with this entity.
139      * 
140      * @return the entityID
141      */
142     public String getEntityID()
143     {
144         return entityID;
145     }
146 
147     /**
148      * Returns the entity type.
149      * 
150      * @return the entityType
151      */
152     public Entity getEntityType()
153     {
154         return entityType;
155     }
156 
157     /**
158      * Returns the group ID.
159      * 
160      * @return the groupID
161      */
162     public String getGroupID()
163     {
164         return groupID;
165     }
166 
167     /**
168      * Returns the user ID.
169      * 
170      * @return the userID
171      */
172     public String getUserID()
173     {
174         return userID;
175     }
176 
177     /**
178      * @see java.lang.Object#hashCode()
179      */
180     @Override
181     public int hashCode()
182     {
183 
184         final int ODD_PRIME_FACTOR = 37;
185         final int ODD_PRIME_TRUE = 1231;
186         final int ODD_PRIME_FALSE = 1237;
187 
188         int result = 17;
189 
190         if (this.isUserACL())
191             result = ODD_PRIME_FACTOR * result + this.userID.hashCode();
192         else if (this.isGroupACL())
193             result = ODD_PRIME_FACTOR * result + this.groupID.hashCode();
194 
195         result = ODD_PRIME_FACTOR * result + (publicAccess ? ODD_PRIME_TRUE : ODD_PRIME_FALSE);
196         result = ODD_PRIME_FACTOR * result + (this.entityID == null ? 0 : this.entityID.hashCode());
197         result = ODD_PRIME_FACTOR * result + (this.entityType == null ? 0 : this.entityType.hashCode());
198         result = ODD_PRIME_FACTOR * result + (isDefault ? ODD_PRIME_TRUE : ODD_PRIME_FALSE);
199         result = ODD_PRIME_FACTOR * result + (isEnabled ? ODD_PRIME_TRUE : ODD_PRIME_FALSE);
200 
201         return result;
202     }
203 
204     /**
205      * Checks if this is a default entry.
206      * 
207      * @return T if this entry is default, F otherwise
208      */
209     public boolean isDefault()
210     {
211         return this.isDefault;
212     }
213 
214     /**
215      * Checks if this entry is enabled.
216      * 
217      * @return T if this entry is enabled, F otherwise
218      */
219     public boolean isEnabled()
220     {
221         return this.isEnabled;
222     }
223 
224     /**
225      * Checks if it is a group entry.
226      * 
227      * @return T if it is a group grant
228      */
229     public boolean isGroupACL()
230     {
231         return (this.groupID != null);
232     }
233 
234     /**
235      * Checks if the entry has public access.
236      * 
237      * @return T if it's a public access entry, F otherwise
238      */
239     public boolean isPublicAccess()
240     {
241         return publicAccess;
242     }
243 
244     /**
245      * Checks if it is a user entry.
246      * 
247      * @return T if it is a user grant
248      */
249     public boolean isUserACL()
250     {
251         return (this.userID != null);
252     }
253 
254     /**
255      * Sets a new value for 'isDefault' field
256      * 
257      * @param isDefault
258      *            the new default value
259      */
260     public void setDefault(boolean isDefault)
261     {
262         this.isDefault = isDefault;
263     }
264 
265     /**
266      * Sets a new value for 'isEnabled' field
267      * 
268      * @param isEnabled
269      *            the new 'isEnabled' value
270      */
271     public void setEnabled(boolean isEnabled)
272     {
273         this.isEnabled = isEnabled;
274     }
275 
276     /**
277      * Sets the entity ID.
278      * 
279      * @param entityID
280      *            the entityID to set
281      */
282     public void setEntityID(String entityID)
283     {
284         this.entityID = entityID;
285     }
286 
287     /**
288      * Sets the entity type.
289      * 
290      * @param entityType
291      *            the entityType to set
292      */
293     public void setEntityType(Entity entityType)
294     {
295         this.entityType = entityType;
296     }
297 
298     /**
299      * Sets the group ID.
300      * 
301      * @param groupID
302      *            the groupID to set
303      */
304     public void setGroupID(String groupID)
305     {
306         this.groupID = groupID;
307         this.userID = null;
308         this.publicAccess = false;
309     }
310 
311     /**
312      * Sets the ACL entry as public.
313      */
314     public void setPublicAccess()
315     {
316         this.publicAccess = true;
317         this.userID = null;
318         this.groupID = null;
319     }
320 
321     /**
322      * Sets the user ID.
323      * 
324      * @param userID
325      *            the userID to set
326      */
327     public void setUserID(String userID)
328     {
329         this.userID = userID;
330         this.groupID = null;
331         this.publicAccess = false;
332     }
333 
334     /**
335      * @see java.lang.Object#toString()
336      */
337     @Override
338     public String toString()
339     {
340         ObjectFormatter formatter = new ObjectFormatter();
341         formatter.addItemIfNotNull("User ID", userID);
342         formatter.addItemIfNotNull("Group ID", groupID);
343         formatter.addItemIfNotNull("Public Access", publicAccess);
344         formatter.addItemIfNotNull("Entity Type", entityType);
345         formatter.addItemIfNotNull("Entity ID", entityID);
346         formatter.addItemIfNotNull("Default", isDefault);
347         formatter.addItemIfNotNull("Enabled", isEnabled);
348 
349         return formatter.getFormatedObject();
350     }
351 
352 }