1 package org
.argeo
.osgi
.useradmin
;
3 import java
.io
.InputStream
;
4 import java
.util
.SortedMap
;
5 import java
.util
.TreeMap
;
7 import javax
.naming
.InvalidNameException
;
8 import javax
.naming
.NamingEnumeration
;
9 import javax
.naming
.directory
.Attributes
;
10 import javax
.naming
.ldap
.LdapName
;
12 import org
.osgi
.framework
.InvalidSyntaxException
;
13 import org
.osgi
.service
.useradmin
.Authorization
;
14 import org
.osgi
.service
.useradmin
.Role
;
15 import org
.osgi
.service
.useradmin
.User
;
16 import org
.osgi
.service
.useradmin
.UserAdmin
;
18 public class LdifUserAdmin
implements UserAdmin
{
19 SortedMap
<LdapName
, LdifUser
> users
= new TreeMap
<LdapName
, LdifUser
>();
20 SortedMap
<LdapName
, LdifGroup
> groups
= new TreeMap
<LdapName
, LdifGroup
>();
22 public LdifUserAdmin(InputStream in
) {
24 LdifParser ldifParser
= new LdifParser();
25 SortedMap
<LdapName
, Attributes
> allEntries
= ldifParser
.read(in
);
26 for (LdapName key
: allEntries
.keySet()) {
27 Attributes attributes
= allEntries
.get(key
);
28 NamingEnumeration objectClasses
= attributes
.get("objectClass")
30 objectClasses
: while (objectClasses
.hasMore()) {
31 String objectClass
= objectClasses
.next().toString();
32 if (objectClass
.equals("inetOrgPerson")) {
33 users
.put(key
, new LdifUser(key
, attributes
));
35 } else if (objectClass
.equals("groupOfNames")) {
36 groups
.put(key
, new LdifGroup(key
, attributes
));
43 for (LdifGroup group
: groups
.values()) {
44 group
.loadMembers(this);
46 } catch (Exception e
) {
47 throw new ArgeoUserAdminException(
48 "Cannot initialise user admin service from LDIF", e
);
53 public Role
getRole(String name
) {
56 key
= new LdapName(name
);
57 } catch (InvalidNameException e
) {
58 // TODO implements default base DN
59 throw new IllegalArgumentException("Badly formatted role name: "
63 if (groups
.containsKey(key
))
64 return groups
.get(key
);
65 if (users
.containsKey(key
))
66 return users
.get(key
);
71 public Authorization
getAuthorization(User user
) {
72 return new LdifAuthorization((LdifUser
) user
);
76 public Role
createRole(String name
, int type
) {
77 throw new UnsupportedOperationException();
81 public boolean removeRole(String name
) {
82 throw new UnsupportedOperationException();
86 public Role
[] getRoles(String filter
) throws InvalidSyntaxException
{
87 throw new UnsupportedOperationException();
91 public User
getUser(String key
, String value
) {
92 throw new UnsupportedOperationException();