1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package pt.digitalis.dif.controller.security.objects; |
6 | |
|
7 | |
import java.util.ArrayList; |
8 | |
import java.util.HashMap; |
9 | |
import java.util.List; |
10 | |
import java.util.Map; |
11 | |
import java.util.Set; |
12 | |
|
13 | |
import pt.digitalis.dif.controller.security.managers.IAuthorizationManager; |
14 | |
import pt.digitalis.dif.controller.security.managers.IIdentityManager; |
15 | |
import pt.digitalis.dif.controller.security.managers.IIdentityManagerPrivate; |
16 | |
import pt.digitalis.dif.dem.Entity; |
17 | |
import pt.digitalis.dif.dem.interfaces.IStage; |
18 | |
import pt.digitalis.dif.exception.InternalFrameworkException; |
19 | |
import pt.digitalis.dif.exception.security.AuthorizationManagerException; |
20 | |
import pt.digitalis.dif.exception.security.IdentityManagerException; |
21 | |
import pt.digitalis.dif.ioc.DIFIoCRegistry; |
22 | |
import pt.digitalis.dif.utils.ObjectFormatter; |
23 | |
import pt.digitalis.utils.common.collections.CaseInsensitiveHashMap; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class DIFUserImpl implements IDIFClonableUser, Cloneable { |
33 | |
|
34 | |
|
35 | 0 | static private IAuthorizationManager authorizationManager = DIFIoCRegistry.getRegistry().getImplementation( |
36 | 0 | IAuthorizationManager.class); |
37 | |
|
38 | |
|
39 | 0 | static private IIdentityManager identityManager = DIFIoCRegistry.getRegistry().getImplementation( |
40 | 0 | IIdentityManager.class); |
41 | |
|
42 | |
|
43 | 0 | private CaseInsensitiveHashMap<Object> attributes = new CaseInsensitiveHashMap<Object>(); |
44 | |
|
45 | |
|
46 | 0 | private List<String> attributesToRemove = new ArrayList<String>(); |
47 | |
|
48 | |
|
49 | |
private String email; |
50 | |
|
51 | |
|
52 | |
private boolean enabled; |
53 | |
|
54 | |
|
55 | 0 | private Map<String, IDIFGroup> groupsCache = null; |
56 | |
|
57 | |
|
58 | |
private String id; |
59 | |
|
60 | |
|
61 | |
private boolean isDefault; |
62 | |
|
63 | |
|
64 | |
private String name; |
65 | |
|
66 | |
|
67 | |
private String nick; |
68 | |
|
69 | |
|
70 | |
private String password; |
71 | |
|
72 | |
|
73 | |
private String profileID; |
74 | |
|
75 | |
|
76 | 0 | private final Map<String, IDIFGroup> temporaryGroups = new HashMap<String, IDIFGroup>(); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 0 | public DIFUserImpl() |
82 | |
{ |
83 | 0 | this.cleanCache(); |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 0 | public DIFUserImpl(IDIFUser user, String password) throws IdentityManagerException |
97 | |
{ |
98 | 0 | setProps(user); |
99 | |
|
100 | 0 | this.password = password; |
101 | 0 | this.cleanCache(); |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void addTempGroup(String groupId) |
108 | |
{ |
109 | |
try |
110 | |
{ |
111 | 0 | IDIFGroup group = identityManager.getGroup(groupId); |
112 | 0 | if (group != null) |
113 | |
{ |
114 | 0 | temporaryGroups.put(group.getID(), group); |
115 | |
} |
116 | |
} |
117 | 0 | catch (IdentityManagerException e) |
118 | |
{ |
119 | |
|
120 | |
} |
121 | 0 | } |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public boolean canAccess(Entity resourceType, String resourceId) |
128 | |
{ |
129 | |
try |
130 | |
{ |
131 | 0 | return authorizationManager.hasAccessUser(this, resourceType, resourceId) |
132 | 0 | || authorizationManager.hasAccessPublic(resourceType, resourceId); |
133 | |
} |
134 | 0 | catch (AuthorizationManagerException authorizationManagerException) |
135 | |
{ |
136 | 0 | return false; |
137 | |
} |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public boolean canAccess(IStage stage) |
144 | |
{ |
145 | 0 | if (stage == null) |
146 | 0 | return false; |
147 | |
|
148 | 0 | return this.canAccess(Entity.STAGE, stage.getID()); |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public void cleanCache() |
155 | |
{ |
156 | 0 | groupsCache = null; |
157 | 0 | } |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
@Override |
163 | |
protected Object clone() throws CloneNotSupportedException |
164 | |
{ |
165 | |
try |
166 | |
{ |
167 | 0 | DIFUserImpl clone = new DIFUserImpl(this, this.password); |
168 | |
|
169 | 0 | return clone; |
170 | |
} |
171 | 0 | catch (IdentityManagerException identityManagerException) |
172 | |
{ |
173 | 0 | throw new RuntimeException( |
174 | 0 | "Could not clone object because the identity manager raised an excpetion an vital data could not be fetched!", |
175 | 0 | identityManagerException); |
176 | |
} |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public IDIFClonableUser cloneUser() |
183 | |
{ |
184 | |
try |
185 | |
{ |
186 | 0 | return (IDIFClonableUser) this.clone(); |
187 | |
|
188 | |
} |
189 | 0 | catch (CloneNotSupportedException e) |
190 | |
{ |
191 | 0 | return null; |
192 | |
} |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public boolean containsAttribute(String id) |
199 | |
{ |
200 | 0 | return attributes.containsKey(id); |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public boolean containsParameter(String id) |
207 | |
{ |
208 | 0 | return DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class) |
209 | 0 | .containsUserParameter(this.getID(), id); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
@Override |
216 | |
public boolean equals(Object obj) |
217 | |
{ |
218 | 0 | if (this == obj) |
219 | 0 | return true; |
220 | 0 | if (obj == null) |
221 | 0 | return false; |
222 | 0 | if (getClass() != obj.getClass()) |
223 | 0 | return false; |
224 | 0 | DIFUserImpl other = (DIFUserImpl) obj; |
225 | 0 | if (id == null) |
226 | |
{ |
227 | 0 | if (other.id != null) |
228 | 0 | return false; |
229 | |
} |
230 | 0 | else if (!id.equals(other.id)) |
231 | 0 | return false; |
232 | 0 | return true; |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public Object getAttribute(String id) |
239 | |
{ |
240 | 0 | return attributes.get(id); |
241 | |
} |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public CaseInsensitiveHashMap<Object> getAttributes() |
247 | |
{ |
248 | 0 | return attributes; |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
public List<String> getAttributesToRemove() |
255 | |
{ |
256 | 0 | return attributesToRemove; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
public String getEmail() |
263 | |
{ |
264 | 0 | return email; |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
public Set<String> getGroupIDs() throws IdentityManagerException |
271 | |
{ |
272 | 0 | return getGroups().keySet(); |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
public Map<String, IDIFGroup> getGroups() throws IdentityManagerException |
279 | |
{ |
280 | 0 | if (groupsCache == null) |
281 | |
{ |
282 | 0 | groupsCache = identityManager.getUserGroups(id); |
283 | 0 | groupsCache.putAll(this.temporaryGroups); |
284 | |
} |
285 | |
|
286 | 0 | return groupsCache; |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
public String getID() |
293 | |
{ |
294 | 0 | return id; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
public String getName() |
301 | |
{ |
302 | 0 | return name; |
303 | |
} |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
public String getNick() |
309 | |
{ |
310 | 0 | return nick; |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public Object getParameter(String id) |
317 | |
{ |
318 | 0 | return DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class) |
319 | 0 | .getUserParameter(this.getID(), id); |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
public Map<String, Object> getParameters() |
326 | |
{ |
327 | 0 | return DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class).getUserParameters(this.getID()); |
328 | |
} |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
public String getPassword() |
334 | |
{ |
335 | 0 | return password; |
336 | |
} |
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
public IDIFGroup getProfile() throws IdentityManagerException |
342 | |
{ |
343 | 0 | if (profileID == null) |
344 | 0 | return null; |
345 | |
|
346 | 0 | return getGroups().get(profileID); |
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public String getProfileID() |
353 | |
{ |
354 | 0 | return this.profileID; |
355 | |
} |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
@Override |
361 | |
public int hashCode() |
362 | |
{ |
363 | 0 | final int prime = 31; |
364 | 0 | int result = 1; |
365 | 0 | result = prime * result + ((id == null) ? 0 : id.hashCode()); |
366 | 0 | return result; |
367 | |
} |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
public void initializeAttributes(Map<String, Object> attrs) |
373 | |
{ |
374 | 0 | attributes = new CaseInsensitiveHashMap<Object>(); |
375 | 0 | attributes.putAll(attrs); |
376 | 0 | } |
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
public boolean isDefault() |
382 | |
{ |
383 | 0 | return this.isDefault; |
384 | |
} |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
public boolean isEnabled() |
390 | |
{ |
391 | 0 | return enabled; |
392 | |
} |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
public void refresh() |
398 | |
{ |
399 | |
IDIFUser updatedUser; |
400 | |
try |
401 | |
{ |
402 | 0 | updatedUser = identityManager.getUser(id); |
403 | 0 | if (updatedUser != null) |
404 | |
{ |
405 | 0 | setProps(updatedUser); |
406 | |
} |
407 | |
} |
408 | 0 | catch (IdentityManagerException e) |
409 | |
{ |
410 | |
|
411 | |
} |
412 | |
|
413 | 0 | cleanCache(); |
414 | 0 | } |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
public void removeAttribute(String id) |
420 | |
{ |
421 | 0 | attributes.remove(id); |
422 | 0 | attributesToRemove.add(id); |
423 | 0 | } |
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
public void removeParameter(String id) |
429 | |
{ |
430 | 0 | DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class).removeUserParameter(this.getID(), id); |
431 | 0 | } |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
public void removeTempGroup(String groupId) |
437 | |
{ |
438 | 0 | temporaryGroups.remove(groupId); |
439 | 0 | } |
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
public void setAttribute(String id, Object attribute) throws InternalFrameworkException |
445 | |
{ |
446 | |
|
447 | |
|
448 | 0 | Object formerAttributeValue = this.attributes.get(id); |
449 | |
|
450 | |
try |
451 | |
{ |
452 | |
|
453 | 0 | this.attributes.put(id, attribute); |
454 | |
|
455 | 0 | ((IIdentityManagerPrivate) DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class)) |
456 | 0 | .persistUserAttribute(this.getID(), id, attribute); |
457 | |
} |
458 | 0 | catch (IdentityManagerException identityManagerException) |
459 | |
{ |
460 | |
|
461 | 0 | this.attributes.put(id, formerAttributeValue); |
462 | |
|
463 | 0 | throw new InternalFrameworkException("Could not update the attribute value on the Identity Manager!", |
464 | 0 | identityManagerException, null); |
465 | |
} |
466 | 0 | } |
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
public void setAttributes(Map<String, Object> attributes) throws InternalFrameworkException |
472 | |
{ |
473 | |
|
474 | 0 | CaseInsensitiveHashMap<Object> formerAttributeValues = this.attributes; |
475 | |
|
476 | |
try |
477 | |
{ |
478 | |
|
479 | 0 | this.attributes = new CaseInsensitiveHashMap<Object>(); |
480 | |
|
481 | 0 | if (attributes != null) |
482 | 0 | this.attributes.putAll(attributes); |
483 | |
|
484 | |
|
485 | 0 | ((IIdentityManagerPrivate) DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class)) |
486 | 0 | .persistUserAttributes(this.getID(), attributes); |
487 | |
} |
488 | 0 | catch (IdentityManagerException identityManagerException) |
489 | |
{ |
490 | |
|
491 | 0 | this.attributes = formerAttributeValues; |
492 | |
|
493 | 0 | throw new InternalFrameworkException("Could not update the attribute values on the Identity Manager!", |
494 | 0 | identityManagerException, null); |
495 | |
} |
496 | 0 | } |
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
public void setDefault(boolean isDefault) |
502 | |
{ |
503 | 0 | this.isDefault = isDefault; |
504 | 0 | } |
505 | |
|
506 | |
|
507 | |
|
508 | |
|
509 | |
public void setEmail(String email) |
510 | |
{ |
511 | 0 | this.email = email; |
512 | 0 | } |
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
public void setEnabled(boolean enabled) |
518 | |
{ |
519 | 0 | this.enabled = enabled; |
520 | 0 | } |
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
public void setID(String id) |
526 | |
{ |
527 | 0 | this.id = id; |
528 | 0 | } |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
public void setName(String name) |
534 | |
{ |
535 | 0 | this.name = name; |
536 | 0 | } |
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
public void setNick(String nick) |
542 | |
{ |
543 | 0 | this.nick = nick; |
544 | 0 | } |
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
public void setParameter(String id, Object parameter) |
550 | |
{ |
551 | 0 | DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class) |
552 | 0 | .setUserParameter(this.getID(), id, parameter); |
553 | 0 | } |
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
public void setParameters(Map<String, Object> parameters) |
559 | |
{ |
560 | 0 | DIFIoCRegistry.getRegistry().getImplementation(IIdentityManager.class) |
561 | 0 | .setUserParameters(this.getID(), parameters); |
562 | 0 | } |
563 | |
|
564 | |
|
565 | |
|
566 | |
|
567 | |
public void setPassword(String password) |
568 | |
{ |
569 | 0 | this.password = password; |
570 | 0 | } |
571 | |
|
572 | |
|
573 | |
|
574 | |
|
575 | |
public void setProfileID(String profileGroupID) |
576 | |
{ |
577 | 0 | this.profileID = profileGroupID; |
578 | |
|
579 | 0 | cleanCache(); |
580 | 0 | } |
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
|
587 | |
|
588 | |
|
589 | |
private void setProps(IDIFUser user) throws IdentityManagerException |
590 | |
{ |
591 | |
|
592 | 0 | this.id = user.getID(); |
593 | 0 | this.nick = user.getNick(); |
594 | 0 | this.name = user.getName(); |
595 | 0 | this.email = user.getEmail(); |
596 | 0 | this.enabled = user.isEnabled(); |
597 | 0 | this.attributes = user.getAttributes(); |
598 | 0 | this.profileID = user.getProfileID(); |
599 | 0 | } |
600 | |
|
601 | |
|
602 | |
|
603 | |
|
604 | |
@Override |
605 | |
public String toString() |
606 | |
{ |
607 | 0 | ObjectFormatter formatter = new ObjectFormatter(); |
608 | |
|
609 | 0 | formatter.addItem("ID", getID()); |
610 | 0 | formatter.addItemIfNotNull("Name", getName()); |
611 | 0 | formatter.addItemIfNotNull("Password", getPassword()); |
612 | 0 | formatter.addItemIfNotNull("Nick", getNick()); |
613 | 0 | formatter.addItemIfNotNull("Email", getEmail()); |
614 | 0 | formatter.addItem("Enabled", isEnabled()); |
615 | 0 | formatter.addItemIfNotNull("Attributes", attributes); |
616 | 0 | formatter.addItemIfNotNull("Parameters", this.getParameters()); |
617 | |
|
618 | |
try |
619 | |
{ |
620 | 0 | IDIFGroup profile = getProfile(); |
621 | 0 | formatter.addItemIfNotNull("Profile", profile); |
622 | |
} |
623 | 0 | catch (IdentityManagerException identityManagerException) |
624 | |
{ |
625 | 0 | throw new RuntimeException("Could not access the user's profile on the identity manager!", |
626 | 0 | identityManagerException); |
627 | |
} |
628 | |
|
629 | |
try |
630 | |
{ |
631 | 0 | Map<String, IDIFGroup> groups = getGroups(); |
632 | 0 | formatter.addItemIfNotNull("Groups", groups); |
633 | |
} |
634 | 0 | catch (IdentityManagerException identityManagerException) |
635 | |
{ |
636 | 0 | throw new RuntimeException("Could not access the user's groups on the identity manager!", |
637 | 0 | identityManagerException); |
638 | |
} |
639 | |
|
640 | 0 | return formatter.getFormatedObject(); |
641 | |
} |
642 | |
} |