]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java
Add Authorization, with chained groups
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / LdifUserAdmin.java
1 package org.argeo.osgi.useradmin;
2
3 import java.io.InputStream;
4 import java.util.SortedMap;
5 import java.util.TreeMap;
6
7 import javax.naming.InvalidNameException;
8 import javax.naming.NamingEnumeration;
9 import javax.naming.directory.Attributes;
10 import javax.naming.ldap.LdapName;
11
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;
17
18 public class LdifUserAdmin implements UserAdmin {
19 SortedMap<LdapName, LdifUser> users = new TreeMap<LdapName, LdifUser>();
20 SortedMap<LdapName, LdifGroup> groups = new TreeMap<LdapName, LdifGroup>();
21
22 public LdifUserAdmin(InputStream in) {
23 try {
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")
29 .getAll();
30 objectClasses: while (objectClasses.hasMore()) {
31 String objectClass = objectClasses.next().toString();
32 if (objectClass.equals("inetOrgPerson")) {
33 users.put(key, new LdifUser(key, attributes));
34 break objectClasses;
35 } else if (objectClass.equals("groupOfNames")) {
36 groups.put(key, new LdifGroup(key, attributes));
37 break objectClasses;
38 }
39 }
40 }
41
42 // optimise
43 for (LdifGroup group : groups.values()) {
44 group.loadMembers(this);
45 }
46 } catch (Exception e) {
47 throw new ArgeoUserAdminException(
48 "Cannot initialise user admin service from LDIF", e);
49 }
50 }
51
52 @Override
53 public Role getRole(String name) {
54 LdapName key;
55 try {
56 key = new LdapName(name);
57 } catch (InvalidNameException e) {
58 // TODO implements default base DN
59 throw new IllegalArgumentException("Badly formatted role name: "
60 + name, e);
61 }
62
63 if (groups.containsKey(key))
64 return groups.get(key);
65 if (users.containsKey(key))
66 return users.get(key);
67 return null;
68 }
69
70 @Override
71 public Authorization getAuthorization(User user) {
72 return new LdifAuthorization((LdifUser) user);
73 }
74
75 @Override
76 public Role createRole(String name, int type) {
77 throw new UnsupportedOperationException();
78 }
79
80 @Override
81 public boolean removeRole(String name) {
82 throw new UnsupportedOperationException();
83 }
84
85 @Override
86 public Role[] getRoles(String filter) throws InvalidSyntaxException {
87 throw new UnsupportedOperationException();
88 }
89
90 @Override
91 public User getUser(String key, String value) {
92 throw new UnsupportedOperationException();
93 }
94
95 }