1 package org
.argeo
.osgi
.useradmin
;
3 import static org
.argeo
.naming
.LdapAttrs
.objectClass
;
4 import static org
.argeo
.naming
.LdapObjs
.inetOrgPerson
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
10 import java
.io
.OutputStream
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Dictionary
;
13 import java
.util
.HashSet
;
14 import java
.util
.Hashtable
;
15 import java
.util
.List
;
17 import java
.util
.SortedMap
;
18 import java
.util
.TreeMap
;
20 import javax
.naming
.NamingEnumeration
;
21 import javax
.naming
.directory
.Attributes
;
22 import javax
.naming
.ldap
.LdapName
;
23 import javax
.transaction
.TransactionManager
;
25 import org
.argeo
.naming
.LdifParser
;
26 import org
.argeo
.naming
.LdifWriter
;
27 import org
.osgi
.framework
.Filter
;
28 import org
.osgi
.service
.useradmin
.Role
;
29 import org
.osgi
.service
.useradmin
.User
;
32 * A user admin based on a LDIF files. Requires a {@link TransactionManager} and
33 * an open transaction for write access.
35 public class LdifUserAdmin
extends AbstractUserDirectory
{
36 private SortedMap
<LdapName
, DirectoryUser
> users
= new TreeMap
<LdapName
, DirectoryUser
>();
37 private SortedMap
<LdapName
, DirectoryGroup
> groups
= new TreeMap
<LdapName
, DirectoryGroup
>();
39 public LdifUserAdmin(String uri
, String baseDn
) {
40 this(fromUri(uri
, baseDn
));
43 public LdifUserAdmin(Dictionary
<String
, ?
> properties
) {
47 public LdifUserAdmin(InputStream in
) {
48 super(new Hashtable
<String
, Object
>());
53 protected AbstractUserDirectory
scope(User user
) {
54 Dictionary
<String
, Object
> properties
= cloneProperties();
55 properties
.put(UserAdminConf
.readOnly
.name(), "true");
56 return new LdifUserAdmin(properties
);
59 private static Dictionary
<String
, Object
> fromUri(String uri
, String baseDn
) {
60 Hashtable
<String
, Object
> res
= new Hashtable
<String
, Object
>();
61 res
.put(UserAdminConf
.uri
.name(), uri
);
62 res
.put(UserAdminConf
.baseDn
.name(), baseDn
);
68 if (getUri().getScheme().equals("file")) {
69 File file
= new File(getUri());
73 load(getUri().toURL().openStream());
74 } catch (Exception e
) {
75 throw new UserDirectoryException("Cannot open URL " + getUri(), e
);
81 throw new UserDirectoryException("Cannot save LDIF user admin: no URI is set");
83 throw new UserDirectoryException("Cannot save LDIF user admin: " + getUri() + " is read-only");
84 try (FileOutputStream out
= new FileOutputStream(new File(getUri()))) {
86 } catch (IOException e
) {
87 throw new UserDirectoryException("Cannot save user admin to " + getUri(), e
);
91 public void save(OutputStream out
) throws IOException
{
93 LdifWriter ldifWriter
= new LdifWriter(out
);
94 for (LdapName name
: groups
.keySet())
95 ldifWriter
.writeEntry(name
, groups
.get(name
).getAttributes());
96 for (LdapName name
: users
.keySet())
97 ldifWriter
.writeEntry(name
, users
.get(name
).getAttributes());
103 protected void load(InputStream in
) {
108 LdifParser ldifParser
= new LdifParser();
109 SortedMap
<LdapName
, Attributes
> allEntries
= ldifParser
.read(in
);
110 for (LdapName key
: allEntries
.keySet()) {
111 Attributes attributes
= allEntries
.get(key
);
112 // check for inconsistency
113 Set
<String
> lowerCase
= new HashSet
<String
>();
114 NamingEnumeration
<String
> ids
= attributes
.getIDs();
115 while (ids
.hasMoreElements()) {
116 String id
= ids
.nextElement().toLowerCase();
117 if (lowerCase
.contains(id
))
118 throw new UserDirectoryException(key
+ " has duplicate id " + id
);
122 // analyse object classes
123 NamingEnumeration
<?
> objectClasses
= attributes
.get(objectClass
.name()).getAll();
124 // System.out.println(key);
125 objectClasses
: while (objectClasses
.hasMore()) {
126 String objectClass
= objectClasses
.next().toString();
127 // System.out.println(" " + objectClass);
128 if (objectClass
.equals(inetOrgPerson
.name())) {
129 users
.put(key
, new LdifUser(this, key
, attributes
));
131 } else if (objectClass
.equals(getGroupObjectClass())) {
132 groups
.put(key
, new LdifGroup(this, key
, attributes
));
137 } catch (Exception e
) {
138 throw new UserDirectoryException("Cannot load user admin service from LDIF", e
);
142 public void destroy() {
143 if (users
== null || groups
== null)
144 throw new UserDirectoryException("User directory " + getBaseDn() + " is already destroyed");
151 protected DirectoryUser
daoGetRole(LdapName key
) {
152 if (groups
.containsKey(key
))
153 return groups
.get(key
);
154 if (users
.containsKey(key
))
155 return users
.get(key
);
159 protected Boolean
daoHasRole(LdapName dn
) {
160 return users
.containsKey(dn
) || groups
.containsKey(dn
);
163 @SuppressWarnings("unchecked")
164 protected List
<DirectoryUser
> doGetRoles(Filter f
) {
165 ArrayList
<DirectoryUser
> res
= new ArrayList
<DirectoryUser
>();
167 res
.addAll(users
.values());
168 res
.addAll(groups
.values());
170 for (DirectoryUser user
: users
.values()) {
171 // System.out.println("\n" + user.getName());
172 // Dictionary<String, Object> props = user.getProperties();
173 // for (Enumeration<String> keys = props.keys(); keys
174 // .hasMoreElements();) {
175 // String key = keys.nextElement();
176 // System.out.println(" " + key + "=" + props.get(key));
178 if (f
.match(user
.getProperties()))
181 for (DirectoryUser group
: groups
.values())
182 if (f
.match(group
.getProperties()))
189 protected List
<LdapName
> getDirectGroups(LdapName dn
) {
190 List
<LdapName
> directGroups
= new ArrayList
<LdapName
>();
191 for (LdapName name
: groups
.keySet()) {
192 DirectoryGroup group
= groups
.get(name
);
193 if (group
.getMemberNames().contains(dn
))
194 directGroups
.add(group
.getDn());
200 protected void prepare(UserDirectoryWorkingCopy wc
) {
202 for (LdapName dn
: wc
.getDeletedUsers().keySet()) {
203 if (users
.containsKey(dn
))
205 else if (groups
.containsKey(dn
))
208 throw new UserDirectoryException("User to delete not found " + dn
);
211 for (LdapName dn
: wc
.getNewUsers().keySet()) {
212 DirectoryUser user
= wc
.getNewUsers().get(dn
);
213 if (users
.containsKey(dn
) || groups
.containsKey(dn
))
214 throw new UserDirectoryException("User to create found " + dn
);
215 else if (Role
.USER
== user
.getType())
217 else if (Role
.GROUP
== user
.getType())
218 groups
.put(dn
, (DirectoryGroup
) user
);
220 throw new UserDirectoryException("Unsupported role type " + user
.getType() + " for new user " + dn
);
223 for (LdapName dn
: wc
.getModifiedUsers().keySet()) {
224 Attributes modifiedAttrs
= wc
.getModifiedUsers().get(dn
);
226 if (users
.containsKey(dn
))
227 user
= users
.get(dn
);
228 else if (groups
.containsKey(dn
))
229 user
= groups
.get(dn
);
231 throw new UserDirectoryException("User to modify no found " + dn
);
232 user
.publishAttributes(modifiedAttrs
);
237 protected void commit(UserDirectoryWorkingCopy wc
) {
242 protected void rollback(UserDirectoryWorkingCopy wc
) {