Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ACLEntry |
|
| 2.3636363636363638;2,364 |
1 | 0 | /** |
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 | 0 | Entity result = null; |
30 | ||
31 | 0 | entityName = entityName.toLowerCase(); |
32 | ||
33 | 0 | if (entityName.equals(Entity.VALIDATOR.getName().toLowerCase())) |
34 | 0 | result = Entity.VALIDATOR; |
35 | 0 | else if (entityName.equals(Entity.PROVIDER.getName().toLowerCase())) |
36 | 0 | result = Entity.PROVIDER; |
37 | 0 | else if (entityName.equals(Entity.APPLICATION.getName().toLowerCase())) |
38 | 0 | result = Entity.APPLICATION; |
39 | 0 | else if (entityName.equals(Entity.SERVICE.getName().toLowerCase())) |
40 | 0 | result = Entity.SERVICE; |
41 | 0 | else if (entityName.equals(Entity.STAGE.getName().toLowerCase())) |
42 | 0 | result = Entity.STAGE; |
43 | ||
44 | 0 | 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 | 0 | String result = null; |
57 | ||
58 | 0 | if (entity.equals(Entity.VALIDATOR)) |
59 | 0 | result = Entity.VALIDATOR.getName(); |
60 | 0 | else if (entity.equals(Entity.PROVIDER)) |
61 | 0 | result = Entity.PROVIDER.getName(); |
62 | 0 | else if (entity.equals(Entity.APPLICATION)) |
63 | 0 | result = Entity.APPLICATION.getName(); |
64 | 0 | else if (entity.equals(Entity.SERVICE)) |
65 | 0 | result = Entity.SERVICE.getName(); |
66 | 0 | else if (entity.equals(Entity.STAGE)) |
67 | 0 | result = Entity.STAGE.getName(); |
68 | else | |
69 | 0 | result = ""; |
70 | ||
71 | 0 | 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 | 0 | public ACLEntry() |
97 | { | |
98 | 0 | this.userID = null; |
99 | 0 | this.groupID = null; |
100 | 0 | this.entityType = null; |
101 | 0 | this.entityID = null; |
102 | 0 | this.publicAccess = false; |
103 | 0 | this.isEnabled = true; |
104 | 0 | this.isDefault = false; |
105 | 0 | } |
106 | ||
107 | /** | |
108 | * @see java.lang.Object#equals(java.lang.Object) | |
109 | */ | |
110 | @Override | |
111 | public boolean equals(Object obj) | |
112 | { | |
113 | 0 | ACLEntry entry = (ACLEntry) obj; |
114 | 0 | boolean same = true; |
115 | ||
116 | // Validate ownership... | |
117 | 0 | if (isUserACL()) |
118 | 0 | same = same && this.getUserID().equals(entry.getUserID()); |
119 | 0 | else if (isGroupACL()) |
120 | 0 | same = same && this.getGroupID().equals(entry.getGroupID()); |
121 | // else, they can still be equal ('same' is T) | |
122 | ||
123 | // Validate object | |
124 | 0 | if (this.entityID != null && entry.entityID != null) |
125 | 0 | same = same && this.getEntityID().equals(entry.getEntityID()); |
126 | ||
127 | 0 | if (this.entityType != null && entry.entityType != null) |
128 | 0 | same = same && this.getEntityType().equals(entry.getEntityType()); |
129 | ||
130 | 0 | same = same && this.isDefault() == entry.isDefault(); |
131 | 0 | same = same && this.isEnabled() == entry.isEnabled(); |
132 | 0 | same = same && this.isPublicAccess() == entry.isPublicAccess(); |
133 | ||
134 | 0 | 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 | 0 | return entityID; |
145 | } | |
146 | ||
147 | /** | |
148 | * Returns the entity type. | |
149 | * | |
150 | * @return the entityType | |
151 | */ | |
152 | public Entity getEntityType() | |
153 | { | |
154 | 0 | return entityType; |
155 | } | |
156 | ||
157 | /** | |
158 | * Returns the group ID. | |
159 | * | |
160 | * @return the groupID | |
161 | */ | |
162 | public String getGroupID() | |
163 | { | |
164 | 0 | return groupID; |
165 | } | |
166 | ||
167 | /** | |
168 | * Returns the user ID. | |
169 | * | |
170 | * @return the userID | |
171 | */ | |
172 | public String getUserID() | |
173 | { | |
174 | 0 | return userID; |
175 | } | |
176 | ||
177 | /** | |
178 | * @see java.lang.Object#hashCode() | |
179 | */ | |
180 | @Override | |
181 | public int hashCode() | |
182 | { | |
183 | ||
184 | 0 | final int ODD_PRIME_FACTOR = 37; |
185 | 0 | final int ODD_PRIME_TRUE = 1231; |
186 | 0 | final int ODD_PRIME_FALSE = 1237; |
187 | ||
188 | 0 | int result = 17; |
189 | ||
190 | 0 | if (this.isUserACL()) |
191 | 0 | result = ODD_PRIME_FACTOR * result + this.userID.hashCode(); |
192 | 0 | else if (this.isGroupACL()) |
193 | 0 | result = ODD_PRIME_FACTOR * result + this.groupID.hashCode(); |
194 | ||
195 | 0 | result = ODD_PRIME_FACTOR * result + (publicAccess ? ODD_PRIME_TRUE : ODD_PRIME_FALSE); |
196 | 0 | result = ODD_PRIME_FACTOR * result + (this.entityID == null ? 0 : this.entityID.hashCode()); |
197 | 0 | result = ODD_PRIME_FACTOR * result + (this.entityType == null ? 0 : this.entityType.hashCode()); |
198 | 0 | result = ODD_PRIME_FACTOR * result + (isDefault ? ODD_PRIME_TRUE : ODD_PRIME_FALSE); |
199 | 0 | result = ODD_PRIME_FACTOR * result + (isEnabled ? ODD_PRIME_TRUE : ODD_PRIME_FALSE); |
200 | ||
201 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | this.isDefault = isDefault; |
263 | 0 | } |
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 | 0 | this.isEnabled = isEnabled; |
274 | 0 | } |
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 | 0 | this.entityID = entityID; |
285 | 0 | } |
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 | 0 | this.entityType = entityType; |
296 | 0 | } |
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 | 0 | this.groupID = groupID; |
307 | 0 | this.userID = null; |
308 | 0 | this.publicAccess = false; |
309 | 0 | } |
310 | ||
311 | /** | |
312 | * Sets the ACL entry as public. | |
313 | */ | |
314 | public void setPublicAccess() | |
315 | { | |
316 | 0 | this.publicAccess = true; |
317 | 0 | this.userID = null; |
318 | 0 | this.groupID = null; |
319 | 0 | } |
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 | 0 | this.userID = userID; |
330 | 0 | this.groupID = null; |
331 | 0 | this.publicAccess = false; |
332 | 0 | } |
333 | ||
334 | /** | |
335 | * @see java.lang.Object#toString() | |
336 | */ | |
337 | @Override | |
338 | public String toString() | |
339 | { | |
340 | 0 | ObjectFormatter formatter = new ObjectFormatter(); |
341 | 0 | formatter.addItemIfNotNull("User ID", userID); |
342 | 0 | formatter.addItemIfNotNull("Group ID", groupID); |
343 | 0 | formatter.addItemIfNotNull("Public Access", publicAccess); |
344 | 0 | formatter.addItemIfNotNull("Entity Type", entityType); |
345 | 0 | formatter.addItemIfNotNull("Entity ID", entityID); |
346 | 0 | formatter.addItemIfNotNull("Default", isDefault); |
347 | 0 | formatter.addItemIfNotNull("Enabled", isEnabled); |
348 | ||
349 | 0 | return formatter.getFormatedObject(); |
350 | } | |
351 | ||
352 | } |