1 package org
.argeo
.osgi
.useradmin
;
3 import static org
.argeo
.osgi
.useradmin
.LdifName
.inetOrgPerson
;
4 import static org
.argeo
.osgi
.useradmin
.LdifName
.objectClass
;
5 import static org
.argeo
.osgi
.useradmin
.LdifName
.organizationalPerson
;
6 import static org
.argeo
.osgi
.useradmin
.LdifName
.person
;
7 import static org
.argeo
.osgi
.useradmin
.LdifName
.top
;
11 import java
.net
.URISyntaxException
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.Dictionary
;
15 import java
.util
.Enumeration
;
16 import java
.util
.Hashtable
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
20 import javax
.naming
.InvalidNameException
;
21 import javax
.naming
.directory
.Attributes
;
22 import javax
.naming
.directory
.BasicAttribute
;
23 import javax
.naming
.directory
.BasicAttributes
;
24 import javax
.naming
.ldap
.LdapName
;
25 import javax
.naming
.ldap
.Rdn
;
26 import javax
.transaction
.SystemException
;
27 import javax
.transaction
.Transaction
;
28 import javax
.transaction
.TransactionManager
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.osgi
.framework
.Filter
;
33 import org
.osgi
.framework
.FrameworkUtil
;
34 import org
.osgi
.framework
.InvalidSyntaxException
;
35 import org
.osgi
.service
.useradmin
.Authorization
;
36 import org
.osgi
.service
.useradmin
.Role
;
37 import org
.osgi
.service
.useradmin
.User
;
38 import org
.osgi
.service
.useradmin
.UserAdmin
;
40 /** Base class for a {@link UserDirectory}. */
41 abstract class AbstractUserDirectory
implements UserAdmin
, UserDirectory
{
42 private final static Log log
= LogFactory
43 .getLog(AbstractUserDirectory
.class);
45 private final Hashtable
<String
, Object
> properties
;
46 private final String baseDn
;
47 private final String userObjectClass
;
48 private final String groupObjectClass
;
50 private final boolean readOnly
;
51 private final URI uri
;
53 private UserAdmin externalRoles
;
54 private List
<String
> indexedUserProperties
= Arrays
.asList(new String
[] {
55 LdifName
.uid
.name(), LdifName
.mail
.name(), LdifName
.cn
.name() });
57 private String memberAttributeId
= "member";
58 private List
<String
> credentialAttributeIds
= Arrays
59 .asList(new String
[] { LdifName
.userpassword
.name() });
61 private TransactionManager transactionManager
;
62 // private TransactionSynchronizationRegistry transactionRegistry;
63 // private Xid editingTransactionXid = null;
64 private WcXaResource xaResource
= new WcXaResource(this);
66 AbstractUserDirectory(Dictionary
<String
, ?
> props
) {
67 properties
= new Hashtable
<String
, Object
>();
68 for (Enumeration
<String
> keys
= props
.keys(); keys
.hasMoreElements();) {
69 String key
= keys
.nextElement();
70 properties
.put(key
, props
.get(key
));
73 String uriStr
= UserAdminConf
.uri
.getValue(properties
);
78 uri
= new URI(uriStr
);
79 } catch (URISyntaxException e
) {
80 throw new UserDirectoryException("Badly formatted URI "
84 baseDn
= UserAdminConf
.baseDn
.getValue(properties
).toString();
85 String readOnlyStr
= UserAdminConf
.readOnly
.getValue(properties
);
86 if (readOnlyStr
== null) {
87 readOnly
= readOnlyDefault(uri
);
88 properties
.put(UserAdminConf
.readOnly
.property(),
89 Boolean
.toString(readOnly
));
91 readOnly
= new Boolean(readOnlyStr
);
93 userObjectClass
= UserAdminConf
.userObjectClass
.getValue(properties
);
94 groupObjectClass
= UserAdminConf
.groupObjectClass
.getValue(properties
);
97 /** Returns the groups this user is a direct member of. */
98 protected abstract List
<LdapName
> getDirectGroups(LdapName dn
);
100 protected abstract Boolean
daoHasRole(LdapName dn
);
102 protected abstract DirectoryUser
daoGetRole(LdapName key
);
104 protected abstract List
<DirectoryUser
> doGetRoles(Filter f
);
110 public void destroy() {
114 boolean isEditing() {
115 // if (editingTransactionXid == null)
117 // return workingCopy.get() != null;
118 return xaResource
.wc() != null;
121 protected UserDirectoryWorkingCopy
getWorkingCopy() {
122 // UserDirectoryWorkingCopy wc = workingCopy.get();
123 UserDirectoryWorkingCopy wc
= xaResource
.wc();
126 // if (wc.getXid() == null) {
127 // workingCopy.set(null);
134 Transaction transaction
;
136 transaction
= transactionManager
.getTransaction();
137 } catch (SystemException e
) {
138 throw new UserDirectoryException("Cannot get transaction", e
);
140 if (transaction
== null)
141 throw new UserDirectoryException(
142 "A transaction needs to be active in order to edit");
143 if (xaResource
.wc() == null) {
144 // UserDirectoryWorkingCopy wc = new UserDirectoryWorkingCopy(this);
146 transaction
.enlistResource(xaResource
);
147 // editingTransactionXid = wc.getXid();
148 // workingCopy.set(wc);
149 } catch (Exception e
) {
150 throw new UserDirectoryException("Cannot enlist " + xaResource
,
154 // UserDirectoryWorkingCopy wc = xaResource.wc();
156 // throw new UserDirectoryException("Transaction "
157 // + editingTransactionXid + " already editing");
159 // (!editingTransactionXid.equals(workingCopy.get().getXid()))
160 // throw new UserDirectoryException("Working copy Xid "
161 // + workingCopy.get().getXid() + " inconsistent with"
162 // + editingTransactionXid);
166 List
<Role
> getAllRoles(DirectoryUser user
) {
167 List
<Role
> allRoles
= new ArrayList
<Role
>();
169 collectRoles(user
, allRoles
);
172 collectAnonymousRoles(allRoles
);
176 private void collectRoles(DirectoryUser user
, List
<Role
> allRoles
) {
177 for (LdapName groupDn
: getDirectGroups(user
.getDn())) {
178 // TODO check for loops
179 DirectoryUser group
= doGetRole(groupDn
);
181 collectRoles(group
, allRoles
);
185 private void collectAnonymousRoles(List
<Role
> allRoles
) {
186 // TODO gather anonymous roles
191 public Role
getRole(String name
) {
192 return doGetRole(toDn(name
));
195 protected DirectoryUser
doGetRole(LdapName dn
) {
196 UserDirectoryWorkingCopy wc
= getWorkingCopy();
197 DirectoryUser user
= daoGetRole(dn
);
199 if (user
== null && wc
.getNewUsers().containsKey(dn
))
200 user
= wc
.getNewUsers().get(dn
);
201 else if (wc
.getDeletedUsers().containsKey(dn
))
207 @SuppressWarnings("unchecked")
209 public Role
[] getRoles(String filter
) throws InvalidSyntaxException
{
210 UserDirectoryWorkingCopy wc
= getWorkingCopy();
211 Filter f
= filter
!= null ? FrameworkUtil
.createFilter(filter
) : null;
212 List
<DirectoryUser
> res
= doGetRoles(f
);
214 for (Iterator
<DirectoryUser
> it
= res
.iterator(); it
.hasNext();) {
215 DirectoryUser user
= it
.next();
216 LdapName dn
= user
.getDn();
217 if (wc
.getDeletedUsers().containsKey(dn
))
220 for (DirectoryUser user
: wc
.getNewUsers().values()) {
221 if (f
== null || f
.match(user
.getProperties()))
224 // no need to check modified users,
225 // since doGetRoles was already based on the modified attributes
227 return res
.toArray(new Role
[res
.size()]);
231 public User
getUser(String key
, String value
) {
232 // TODO check value null or empty
233 List
<DirectoryUser
> collectedUsers
= new ArrayList
<DirectoryUser
>(
234 getIndexedUserProperties().size());
236 doGetUser(key
, value
, collectedUsers
);
239 DirectoryUser user
= null;
241 user
= (DirectoryUser
) getRole(value
);
243 collectedUsers
.add(user
);
244 } catch (Exception e
) {
248 for (String attr
: getIndexedUserProperties())
249 doGetUser(attr
, value
, collectedUsers
);
251 if (collectedUsers
.size() == 1)
252 return collectedUsers
.get(0);
253 else if (collectedUsers
.size() > 1)
254 log
.warn(collectedUsers
.size() + " users for "
255 + (key
!= null ? key
+ "=" : "") + value
);
259 protected void doGetUser(String key
, String value
,
260 List
<DirectoryUser
> collectedUsers
) {
262 Filter f
= FrameworkUtil
.createFilter("(&(" + objectClass
+ "="
263 + getUserObjectClass() + ")(" + key
+ "=" + value
+ "))");
264 List
<DirectoryUser
> users
= doGetRoles(f
);
265 collectedUsers
.addAll(users
);
266 } catch (InvalidSyntaxException e
) {
267 throw new UserDirectoryException("Cannot get user with " + key
273 public Authorization
getAuthorization(User user
) {
274 return new LdifAuthorization((DirectoryUser
) user
,
275 getAllRoles((DirectoryUser
) user
));
279 public Role
createRole(String name
, int type
) {
281 UserDirectoryWorkingCopy wc
= getWorkingCopy();
282 LdapName dn
= toDn(name
);
283 if ((daoHasRole(dn
) && !wc
.getDeletedUsers().containsKey(dn
))
284 || wc
.getNewUsers().containsKey(dn
))
285 throw new UserDirectoryException("Already a role " + name
);
286 BasicAttributes attrs
= new BasicAttributes();
287 attrs
.put("dn", dn
.toString());
288 Rdn nameRdn
= dn
.getRdn(dn
.size() - 1);
289 // TODO deal with multiple attr RDN
290 attrs
.put(nameRdn
.getType(), nameRdn
.getValue());
291 if (wc
.getDeletedUsers().containsKey(dn
)) {
292 wc
.getDeletedUsers().remove(dn
);
293 wc
.getModifiedUsers().put(dn
, attrs
);
295 wc
.getModifiedUsers().put(dn
, attrs
);
296 DirectoryUser newRole
= newRole(dn
, type
, attrs
);
297 wc
.getNewUsers().put(dn
, newRole
);
299 return getRole(name
);
302 protected DirectoryUser
newRole(LdapName dn
, int type
, Attributes attrs
) {
304 BasicAttribute objClass
= new BasicAttribute(objectClass
.name());
305 if (type
== Role
.USER
) {
306 String userObjClass
= getUserObjectClass();
307 objClass
.add(userObjClass
);
308 if (inetOrgPerson
.name().equals(userObjClass
)) {
309 objClass
.add(organizationalPerson
.name());
310 objClass
.add(person
.name());
311 } else if (organizationalPerson
.name().equals(userObjClass
)) {
312 objClass
.add(person
.name());
316 newRole
= new LdifUser(this, dn
, attrs
);
317 } else if (type
== Role
.GROUP
) {
318 objClass
.add(getGroupObjectClass());
321 newRole
= new LdifGroup(this, dn
, attrs
);
323 throw new UserDirectoryException("Unsupported type " + type
);
328 public boolean removeRole(String name
) {
330 UserDirectoryWorkingCopy wc
= getWorkingCopy();
331 LdapName dn
= toDn(name
);
332 boolean actuallyDeleted
;
333 if (daoHasRole(dn
) || wc
.getNewUsers().containsKey(dn
)) {
334 DirectoryUser user
= (DirectoryUser
) getRole(name
);
335 wc
.getDeletedUsers().put(dn
, user
);
336 actuallyDeleted
= true;
337 } else {// just removing from groups (e.g. system roles)
338 actuallyDeleted
= false;
340 for (LdapName groupDn
: getDirectGroups(dn
)) {
341 DirectoryUser group
= doGetRole(groupDn
);
342 group
.getAttributes().get(getMemberAttributeId())
343 .remove(dn
.toString());
345 return actuallyDeleted
;
349 protected void prepare(UserDirectoryWorkingCopy wc
) {
353 protected void commit(UserDirectoryWorkingCopy wc
) {
357 protected void rollback(UserDirectoryWorkingCopy wc
) {
361 // void clearEditingTransactionXid() {
362 // editingTransactionXid = null;
366 protected LdapName
toDn(String name
) {
368 return new LdapName(name
);
369 } catch (InvalidNameException e
) {
370 throw new UserDirectoryException("Badly formatted name", e
);
376 String
getMemberAttributeId() {
377 return memberAttributeId
;
380 List
<String
> getCredentialAttributeIds() {
381 return credentialAttributeIds
;
384 protected URI
getUri() {
388 protected List
<String
> getIndexedUserProperties() {
389 return indexedUserProperties
;
392 protected void setIndexedUserProperties(List
<String
> indexedUserProperties
) {
393 this.indexedUserProperties
= indexedUserProperties
;
396 private static boolean readOnlyDefault(URI uri
) {
399 if (uri
.getScheme().equals("file")) {
400 File file
= new File(uri
);
402 return !file
.canWrite();
404 return !file
.getParentFile().canWrite();
409 public boolean isReadOnly() {
413 UserAdmin
getExternalRoles() {
414 return externalRoles
;
417 public String
getBaseDn() {
421 protected String
getUserObjectClass() {
422 return userObjectClass
;
425 protected String
getGroupObjectClass() {
426 return groupObjectClass
;
429 public Dictionary
<String
, ?
> getProperties() {
433 public void setExternalRoles(UserAdmin externalRoles
) {
434 this.externalRoles
= externalRoles
;
437 public void setTransactionManager(TransactionManager transactionManager
) {
438 this.transactionManager
= transactionManager
;
441 public WcXaResource
getXaResource() {