]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.enterprise/src/org/argeo/osgi/useradmin/LdifAuthorization.java
Factorize 'node' URI scheme constant.
[lgpl/argeo-commons.git] / org.argeo.enterprise / src / org / argeo / osgi / useradmin / LdifAuthorization.java
1 package org.argeo.osgi.useradmin;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.Dictionary;
6 import java.util.List;
7
8 import org.argeo.naming.LdapAttrs;
9 import org.osgi.service.useradmin.Authorization;
10 import org.osgi.service.useradmin.Role;
11 import org.osgi.service.useradmin.User;
12
13 /** Basic authorization. */
14 class LdifAuthorization implements Authorization {
15 private final String name;
16 private final String displayName;
17 private final List<String> allRoles;
18
19 @SuppressWarnings("unchecked")
20 public LdifAuthorization(User user, List<Role> allRoles) {
21 if (user == null) {
22 this.name = null;
23 this.displayName = "anonymous";
24 } else {
25 this.name = user.getName();
26 Dictionary<String, Object> props = user.getProperties();
27 Object displayName = props.get(LdapAttrs.displayName);
28 if (displayName == null)
29 displayName = props.get(LdapAttrs.cn);
30 if (displayName == null)
31 displayName = props.get(LdapAttrs.uid);
32 if (displayName == null)
33 displayName = user.getName();
34 if (displayName == null)
35 throw new UserDirectoryException("Cannot set display name for "
36 + user);
37 this.displayName = displayName.toString();
38 }
39 // roles
40 String[] roles = new String[allRoles.size()];
41 for (int i = 0; i < allRoles.size(); i++) {
42 roles[i] = allRoles.get(i).getName();
43 }
44 this.allRoles = Collections.unmodifiableList(Arrays.asList(roles));
45 }
46
47 @Override
48 public String getName() {
49 return name;
50 }
51
52 @Override
53 public boolean hasRole(String name) {
54 return allRoles.contains(name);
55 }
56
57 @Override
58 public String[] getRoles() {
59 return allRoles.toArray(new String[allRoles.size()]);
60 }
61
62 @Override
63 public int hashCode() {
64 if (name == null)
65 return super.hashCode();
66 return name.hashCode();
67 }
68
69 @Override
70 public boolean equals(Object obj) {
71 if (!(obj instanceof Authorization))
72 return false;
73 Authorization that = (Authorization) obj;
74 if (name == null)
75 return that.getName() == null;
76 return name.equals(that.getName());
77 }
78
79 @Override
80 public String toString() {
81 return displayName;
82 }
83 }