]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/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
.osgi
.service
.useradmin
.Authorization
;
9 import org
.osgi
.service
.useradmin
.Role
;
10 import org
.osgi
.service
.useradmin
.User
;
12 /** Basic authorization. */
13 class LdifAuthorization
implements Authorization
{
14 private final String name
;
15 private final String displayName
;
16 private final List
<String
> allRoles
;
18 @SuppressWarnings("unchecked")
19 public LdifAuthorization(User user
, List
<Role
> allRoles
) {
22 this.displayName
= "anonymous";
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 "
36 this.displayName
= displayName
.toString();
39 String
[] roles
= new String
[allRoles
.size()];
40 for (int i
= 0; i
< allRoles
.size(); i
++) {
41 roles
[i
] = allRoles
.get(i
).getName();
43 this.allRoles
= Collections
.unmodifiableList(Arrays
.asList(roles
));
47 public String
getName() {
52 public boolean hasRole(String name
) {
53 return allRoles
.contains(name
);
57 public String
[] getRoles() {
58 return allRoles
.toArray(new String
[allRoles
.size()]);
62 public int hashCode() {
64 return super.hashCode();
65 return name
.hashCode();
69 public boolean equals(Object obj
) {
70 if (!(obj
instanceof Authorization
))
72 Authorization that
= (Authorization
) obj
;
74 return that
.getName() == null;
75 return name
.equals(that
.getName());
79 public String
toString() {