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