]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/osgi/useradmin/LdifAuthorization.java
1 package org
.argeo
.osgi
.useradmin
;
3 import java
.util
.Arrays
;
4 import java
.util
.Collections
;
5 import java
.util
.Dictionary
;
8 import org
.argeo
.util
.naming
.LdapAttrs
;
9 import org
.osgi
.service
.useradmin
.Authorization
;
10 import org
.osgi
.service
.useradmin
.Role
;
11 import org
.osgi
.service
.useradmin
.User
;
13 /** Basic authorization. */
14 class LdifAuthorization
implements Authorization
{
15 private final String name
;
16 private final String displayName
;
17 private final List
<String
> allRoles
;
19 public LdifAuthorization(User user
, List
<Role
> allRoles
) {
22 this.displayName
= "anonymous";
24 this.name
= user
.getName();
25 this.displayName
= extractDisplayName(user
);
28 String
[] roles
= new String
[allRoles
.size()];
29 for (int i
= 0; i
< allRoles
.size(); i
++) {
30 roles
[i
] = allRoles
.get(i
).getName();
32 this.allRoles
= Collections
.unmodifiableList(Arrays
.asList(roles
));
36 public String
getName() {
41 public boolean hasRole(String name
) {
42 return allRoles
.contains(name
);
46 public String
[] getRoles() {
47 return allRoles
.toArray(new String
[allRoles
.size()]);
51 public int hashCode() {
53 return super.hashCode();
54 return name
.hashCode();
58 public boolean equals(Object obj
) {
59 if (!(obj
instanceof Authorization
))
61 Authorization that
= (Authorization
) obj
;
63 return that
.getName() == null;
64 return name
.equals(that
.getName());
68 public String
toString() {
72 final static String
extractDisplayName(User user
) {
73 Dictionary
<String
, Object
> props
= user
.getProperties();
74 Object displayName
= props
.get(LdapAttrs
.displayName
);
75 if (displayName
== null)
76 displayName
= props
.get(LdapAttrs
.cn
);
77 if (displayName
== null)
78 displayName
= props
.get(LdapAttrs
.uid
);
79 if (displayName
== null)
80 displayName
= user
.getName();
81 if (displayName
== null)
82 throw new UserDirectoryException("Cannot set display name for " + user
);
83 return displayName
.toString();