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