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
;
29 import javax
.transaction
.xa
.Xid
;
31 import org
.osgi
.framework
.Filter
;
32 import org
.osgi
.framework
.FrameworkUtil
;
33 import org
.osgi
.framework
.InvalidSyntaxException
;
34 import org
.osgi
.service
.useradmin
.Authorization
;
35 import org
.osgi
.service
.useradmin
.Role
;
36 import org
.osgi
.service
.useradmin
.User
;
37 import org
.osgi
.service
.useradmin
.UserAdmin
;
39 /** Base class for a {@link UserDirectory}. */
40 abstract class AbstractUserDirectory
implements UserAdmin
, UserDirectory
{
41 private final Hashtable
<String
, Object
> properties
;
42 private final String baseDn
;
43 private final String userObjectClass
;
44 private final String groupObjectClass
;
46 private final boolean readOnly
;
47 private final URI uri
;
49 private UserAdmin externalRoles
;
50 private List
<String
> indexedUserProperties
= Arrays
.asList(new String
[] {
51 LdifName
.uid
.name(), LdifName
.mail
.name(), LdifName
.cn
.name() });
53 private String memberAttributeId
= "member";
54 private List
<String
> credentialAttributeIds
= Arrays
55 .asList(new String
[] { LdifName
.userpassword
.name() });
57 private TransactionManager transactionManager
;
58 private ThreadLocal
<UserDirectoryWorkingCopy
> workingCopy
= new ThreadLocal
<UserDirectoryWorkingCopy
>();
59 private Xid editingTransactionXid
= null;
61 AbstractUserDirectory(Dictionary
<String
, ?
> props
) {
62 properties
= new Hashtable
<String
, Object
>();
63 for (Enumeration
<String
> keys
= props
.keys(); keys
.hasMoreElements();) {
64 String key
= keys
.nextElement();
65 properties
.put(key
, props
.get(key
));
68 String uriStr
= UserAdminConf
.uri
.getValue(properties
);
73 uri
= new URI(uriStr
);
74 } catch (URISyntaxException e
) {
75 throw new UserDirectoryException("Badly formatted URI "
79 baseDn
= UserAdminConf
.baseDn
.getValue(properties
).toString();
80 String readOnlyStr
= UserAdminConf
.readOnly
.getValue(properties
);
81 if (readOnlyStr
== null) {
82 readOnly
= readOnlyDefault(uri
);
83 properties
.put(UserAdminConf
.readOnly
.property(),
84 Boolean
.toString(readOnly
));
86 readOnly
= new Boolean(readOnlyStr
);
88 userObjectClass
= UserAdminConf
.userObjectClass
.getValue(properties
);
89 groupObjectClass
= UserAdminConf
.groupObjectClass
.getValue(properties
);
92 /** Returns the groups this user is a direct member of. */
93 protected abstract List
<LdapName
> getDirectGroups(LdapName dn
);
95 protected abstract Boolean
daoHasRole(LdapName dn
);
97 protected abstract DirectoryUser
daoGetRole(LdapName key
);
99 protected abstract List
<DirectoryUser
> doGetRoles(Filter f
);
105 public void destroy() {
109 boolean isEditing() {
110 if (editingTransactionXid
== null)
112 return workingCopy
.get() != null;
115 protected UserDirectoryWorkingCopy
getWorkingCopy() {
116 UserDirectoryWorkingCopy wc
= workingCopy
.get();
119 if (wc
.getXid() == null) {
120 workingCopy
.set(null);
127 Transaction transaction
;
129 transaction
= transactionManager
.getTransaction();
130 } catch (SystemException e
) {
131 throw new UserDirectoryException("Cannot get transaction", e
);
133 if (transaction
== null)
134 throw new UserDirectoryException(
135 "A transaction needs to be active in order to edit");
136 if (editingTransactionXid
== null) {
137 UserDirectoryWorkingCopy wc
= new UserDirectoryWorkingCopy(this);
139 transaction
.enlistResource(wc
);
140 editingTransactionXid
= wc
.getXid();
142 } catch (Exception e
) {
143 throw new UserDirectoryException("Cannot enlist " + wc
, e
);
146 if (workingCopy
.get() == null)
147 throw new UserDirectoryException("Transaction "
148 + editingTransactionXid
+ " already editing");
149 else if (!editingTransactionXid
.equals(workingCopy
.get().getXid()))
150 throw new UserDirectoryException("Working copy Xid "
151 + workingCopy
.get().getXid() + " inconsistent with"
152 + editingTransactionXid
);
156 List
<Role
> getAllRoles(DirectoryUser user
) {
157 List
<Role
> allRoles
= new ArrayList
<Role
>();
159 collectRoles(user
, allRoles
);
162 collectAnonymousRoles(allRoles
);
166 private void collectRoles(DirectoryUser user
, List
<Role
> allRoles
) {
167 for (LdapName groupDn
: getDirectGroups(user
.getDn())) {
168 // TODO check for loops
169 DirectoryUser group
= doGetRole(groupDn
);
171 collectRoles(group
, allRoles
);
175 private void collectAnonymousRoles(List
<Role
> allRoles
) {
176 // TODO gather anonymous roles
181 public Role
getRole(String name
) {
182 return doGetRole(toDn(name
));
185 protected DirectoryUser
doGetRole(LdapName dn
) {
186 UserDirectoryWorkingCopy wc
= getWorkingCopy();
187 DirectoryUser user
= daoGetRole(dn
);
189 if (user
== null && wc
.getNewUsers().containsKey(dn
))
190 user
= wc
.getNewUsers().get(dn
);
191 else if (wc
.getDeletedUsers().containsKey(dn
))
197 @SuppressWarnings("unchecked")
199 public Role
[] getRoles(String filter
) throws InvalidSyntaxException
{
200 UserDirectoryWorkingCopy wc
= getWorkingCopy();
201 Filter f
= filter
!= null ? FrameworkUtil
.createFilter(filter
) : null;
202 List
<DirectoryUser
> res
= doGetRoles(f
);
204 for (Iterator
<DirectoryUser
> it
= res
.iterator(); it
.hasNext();) {
205 DirectoryUser user
= it
.next();
206 LdapName dn
= user
.getDn();
207 if (wc
.getDeletedUsers().containsKey(dn
))
210 for (DirectoryUser user
: wc
.getNewUsers().values()) {
211 if (f
== null || f
.match(user
.getProperties()))
214 // no need to check modified users,
215 // since doGetRoles was already based on the modified attributes
217 return res
.toArray(new Role
[res
.size()]);
221 public User
getUser(String key
, String value
) {
222 // TODO check value null or empty
223 List
<DirectoryUser
> collectedUsers
= new ArrayList
<DirectoryUser
>(
224 getIndexedUserProperties().size());
226 doGetUser(key
, value
, collectedUsers
);
229 DirectoryUser user
= null;
231 user
= (DirectoryUser
) getRole(value
);
233 collectedUsers
.add(user
);
234 } catch (Exception e
) {
238 for (String attr
: getIndexedUserProperties())
239 doGetUser(attr
, value
, collectedUsers
);
241 if (collectedUsers
.size() == 1)
242 return collectedUsers
.get(0);
246 protected void doGetUser(String key
, String value
,
247 List
<DirectoryUser
> collectedUsers
) {
249 Filter f
= FrameworkUtil
.createFilter("(&(" + objectClass
+ "="
250 + getUserObjectClass() + ")(" + key
+ "=" + value
+ "))");
251 List
<DirectoryUser
> users
= doGetRoles(f
);
252 collectedUsers
.addAll(users
);
253 } catch (InvalidSyntaxException e
) {
254 throw new UserDirectoryException("Cannot get user with " + key
260 public Authorization
getAuthorization(User user
) {
261 return new LdifAuthorization((DirectoryUser
) user
,
262 getAllRoles((DirectoryUser
) user
));
266 public Role
createRole(String name
, int type
) {
268 UserDirectoryWorkingCopy wc
= getWorkingCopy();
269 LdapName dn
= toDn(name
);
270 if ((daoHasRole(dn
) && !wc
.getDeletedUsers().containsKey(dn
))
271 || wc
.getNewUsers().containsKey(dn
))
272 throw new UserDirectoryException("Already a role " + name
);
273 BasicAttributes attrs
= new BasicAttributes();
274 attrs
.put("dn", dn
.toString());
275 Rdn nameRdn
= dn
.getRdn(dn
.size() - 1);
276 // TODO deal with multiple attr RDN
277 attrs
.put(nameRdn
.getType(), nameRdn
.getValue());
278 if (wc
.getDeletedUsers().containsKey(dn
)) {
279 wc
.getDeletedUsers().remove(dn
);
280 wc
.getModifiedUsers().put(dn
, attrs
);
282 wc
.getModifiedUsers().put(dn
, attrs
);
283 DirectoryUser newRole
= newRole(dn
, type
, attrs
);
284 wc
.getNewUsers().put(dn
, newRole
);
286 return getRole(name
);
289 protected DirectoryUser
newRole(LdapName dn
, int type
, Attributes attrs
) {
291 BasicAttribute objClass
= new BasicAttribute(objectClass
.name());
292 if (type
== Role
.USER
) {
293 String userObjClass
= getUserObjectClass();
294 objClass
.add(userObjClass
);
295 if (inetOrgPerson
.name().equals(userObjClass
)) {
296 objClass
.add(organizationalPerson
.name());
297 objClass
.add(person
.name());
298 } else if (organizationalPerson
.name().equals(userObjClass
)) {
299 objClass
.add(person
.name());
303 newRole
= new LdifUser(this, dn
, attrs
);
304 } else if (type
== Role
.GROUP
) {
305 objClass
.add(getGroupObjectClass());
308 newRole
= new LdifGroup(this, dn
, attrs
);
310 throw new UserDirectoryException("Unsupported type " + type
);
315 public boolean removeRole(String name
) {
317 UserDirectoryWorkingCopy wc
= getWorkingCopy();
318 LdapName dn
= toDn(name
);
319 boolean actuallyDeleted
;
320 if (daoHasRole(dn
) || wc
.getNewUsers().containsKey(dn
)) {
321 DirectoryUser user
= (DirectoryUser
) getRole(name
);
322 wc
.getDeletedUsers().put(dn
, user
);
323 actuallyDeleted
= true;
324 } else {// just removing from groups (e.g. system roles)
325 actuallyDeleted
= false;
327 for (LdapName groupDn
: getDirectGroups(dn
)) {
328 DirectoryUser group
= doGetRole(groupDn
);
329 group
.getAttributes().get(getMemberAttributeId())
330 .remove(dn
.toString());
332 return actuallyDeleted
;
336 protected void prepare(UserDirectoryWorkingCopy wc
) {
340 protected void commit(UserDirectoryWorkingCopy wc
) {
344 protected void rollback(UserDirectoryWorkingCopy wc
) {
348 void clearEditingTransactionXid() {
349 editingTransactionXid
= null;
353 protected LdapName
toDn(String name
) {
355 return new LdapName(name
);
356 } catch (InvalidNameException e
) {
357 throw new UserDirectoryException("Badly formatted name", e
);
363 String
getMemberAttributeId() {
364 return memberAttributeId
;
367 List
<String
> getCredentialAttributeIds() {
368 return credentialAttributeIds
;
371 protected URI
getUri() {
375 protected List
<String
> getIndexedUserProperties() {
376 return indexedUserProperties
;
379 protected void setIndexedUserProperties(List
<String
> indexedUserProperties
) {
380 this.indexedUserProperties
= indexedUserProperties
;
383 private static boolean readOnlyDefault(URI uri
) {
386 if (uri
.getScheme().equals("file")) {
387 File file
= new File(uri
);
389 return !file
.canWrite();
391 return !file
.getParentFile().canWrite();
396 public boolean isReadOnly() {
400 UserAdmin
getExternalRoles() {
401 return externalRoles
;
404 public String
getBaseDn() {
408 protected String
getUserObjectClass() {
409 return userObjectClass
;
412 protected String
getGroupObjectClass() {
413 return groupObjectClass
;
416 public Dictionary
<String
, ?
> getProperties() {
420 public void setExternalRoles(UserAdmin externalRoles
) {
421 this.externalRoles
= externalRoles
;
424 public void setTransactionManager(TransactionManager transactionManager
) {
425 this.transactionManager
= transactionManager
;