package org.argeo.osgi.useradmin;
+import static org.argeo.osgi.useradmin.LdifName.inetOrgPerson;
+import static org.argeo.osgi.useradmin.LdifName.objectClass;
+import static org.argeo.osgi.useradmin.LdifName.organizationalPerson;
+import static org.argeo.osgi.useradmin.LdifName.person;
+import static org.argeo.osgi.useradmin.LdifName.top;
+
+import java.io.File;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Enumeration;
import java.util.HashMap;
+import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
-import javax.transaction.TransactionSynchronizationRegistry;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.useradmin.Authorization;
-import org.osgi.service.useradmin.Group;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;
import org.osgi.service.useradmin.UserAdmin;
-public abstract class AbstractUserDirectory implements UserAdmin {
+/** Base class for a {@link UserDirectory}. */
+abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
private final static Log log = LogFactory
.getLog(AbstractUserDirectory.class);
- private boolean isReadOnly;
- private URI uri;
+
+ private final Hashtable<String, Object> properties;
+ private final String baseDn;
+ private final String userObjectClass;
+ private final String groupObjectClass;
+
+ private final boolean readOnly;
+ private final URI uri;
private UserAdmin externalRoles;
private List<String> indexedUserProperties = Arrays.asList(new String[] {
- "uid", "mail", "cn" });
+ LdifName.uid.name(), LdifName.mail.name(), LdifName.cn.name() });
private String memberAttributeId = "member";
private List<String> credentialAttributeIds = Arrays
- .asList(new String[] { "userpassword" });
-
- // private TransactionSynchronizationRegistry syncRegistry;
- // private Object editingTransactionKey = null;
+ .asList(new String[] { LdifName.userpassword.name() });
private TransactionManager transactionManager;
private ThreadLocal<WorkingCopy> workingCopy = new ThreadLocal<AbstractUserDirectory.WorkingCopy>();
private Xid editingTransactionXid = null;
- public AbstractUserDirectory() {
- }
+ AbstractUserDirectory(Dictionary<String, ?> props) {
+ properties = new Hashtable<String, Object>();
+ for (Enumeration<String> keys = props.keys(); keys.hasMoreElements();) {
+ String key = keys.nextElement();
+ properties.put(key, props.get(key));
+ }
- public AbstractUserDirectory(URI uri, boolean isReadOnly) {
- this.uri = uri;
- this.isReadOnly = isReadOnly;
+ String uriStr = UserAdminConf.uri.getValue(properties);
+ if (uriStr == null)
+ uri = null;
+ else
+ try {
+ uri = new URI(uriStr);
+ } catch (URISyntaxException e) {
+ throw new UserDirectoryException("Badly formatted URI "
+ + uriStr, e);
+ }
+
+ baseDn = UserAdminConf.baseDn.getValue(properties).toString();
+ String readOnlyStr = UserAdminConf.readOnly.getValue(properties);
+ if (readOnlyStr == null) {
+ readOnly = readOnlyDefault(uri);
+ properties.put(UserAdminConf.readOnly.property(),
+ Boolean.toString(readOnly));
+ } else
+ readOnly = new Boolean(readOnlyStr);
+
+ userObjectClass = UserAdminConf.userObjectClass.getValue(properties);
+ groupObjectClass = UserAdminConf.groupObjectClass.getValue(properties);
}
- /** Returns the {@link Group}s this user is a direct member of. */
- protected abstract List<? extends DirectoryGroup> getDirectGroups(User user);
+ /** Returns the groups this user is a direct member of. */
+ protected abstract List<LdapName> getDirectGroups(LdapName dn);
protected abstract Boolean daoHasRole(LdapName dn);
protected abstract List<DirectoryUser> doGetRoles(Filter f);
- protected abstract void doGetUser(String key, String value,
- List<DirectoryUser> collectedUsers);
-
public void init() {
}
if (editingTransactionXid == null)
return false;
return workingCopy.get() != null;
- // Object currentTrKey = syncRegistry.getTransactionKey();
- // if (currentTrKey == null)
- // return false;
- // return editingTransactionKey.equals(currentTrKey);
}
protected WorkingCopy getWorkingCopy() {
}
}
- List<Role> getAllRoles(User user) {
+ List<Role> getAllRoles(DirectoryUser user) {
List<Role> allRoles = new ArrayList<Role>();
if (user != null) {
collectRoles(user, allRoles);
return allRoles;
}
- private void collectRoles(User user, List<Role> allRoles) {
- for (Group group : getDirectGroups(user)) {
+ private void collectRoles(DirectoryUser user, List<Role> allRoles) {
+ for (LdapName groupDn : getDirectGroups(user.getDn())) {
// TODO check for loops
+ DirectoryUser group = doGetRole(groupDn);
allRoles.add(group);
collectRoles(group, allRoles);
}
// USER ADMIN
@Override
public Role getRole(String name) {
- LdapName key = toDn(name);
+ return doGetRole(toDn(name));
+ }
+
+ protected DirectoryUser doGetRole(LdapName dn) {
WorkingCopy wc = getWorkingCopy();
- DirectoryUser user = daoGetRole(key);
+ DirectoryUser user = daoGetRole(dn);
if (wc != null) {
- if (user == null && wc.getNewUsers().containsKey(key))
- user = wc.getNewUsers().get(key);
- else if (wc.getDeletedUsers().containsKey(key))
+ if (user == null && wc.getNewUsers().containsKey(dn))
+ user = wc.getNewUsers().get(dn);
+ else if (wc.getDeletedUsers().containsKey(dn))
user = null;
}
return user;
}
+ @SuppressWarnings("unchecked")
@Override
public Role[] getRoles(String filter) throws InvalidSyntaxException {
WorkingCopy wc = getWorkingCopy();
return null;
}
+ protected void doGetUser(String key, String value,
+ List<DirectoryUser> collectedUsers) {
+ try {
+ Filter f = FrameworkUtil.createFilter("(&(" + objectClass + "="
+ + getUserObjectClass() + ")(" + key + "=" + value + "))");
+ List<DirectoryUser> users = doGetRoles(f);
+ collectedUsers.addAll(users);
+ } catch (InvalidSyntaxException e) {
+ throw new UserDirectoryException("Cannot get user with " + key
+ + "=" + value, e);
+ }
+ }
+
@Override
public Authorization getAuthorization(User user) {
return new LdifAuthorization((DirectoryUser) user,
protected DirectoryUser newRole(LdapName dn, int type, Attributes attrs) {
LdifUser newRole;
- BasicAttribute objectClass = new BasicAttribute("objectClass");
+ BasicAttribute objClass = new BasicAttribute(objectClass.name());
if (type == Role.USER) {
+ String userObjClass = getUserObjectClass();
+ objClass.add(userObjClass);
+ if (inetOrgPerson.name().equals(userObjClass)) {
+ objClass.add(organizationalPerson.name());
+ objClass.add(person.name());
+ } else if (organizationalPerson.name().equals(userObjClass)) {
+ objClass.add(person.name());
+ }
+ objClass.add(top);
+ attrs.put(objClass);
newRole = new LdifUser(this, dn, attrs);
- objectClass.add("inetOrgPerson");
- objectClass.add("organizationalPerson");
- objectClass.add("person");
- objectClass.add("top");
} else if (type == Role.GROUP) {
+ objClass.add(getGroupObjectClass());
+ objClass.add(top);
+ attrs.put(objClass);
newRole = new LdifGroup(this, dn, attrs);
- objectClass.add("groupOfNames");
- objectClass.add("top");
} else
throw new UserDirectoryException("Unsupported type " + type);
- newRole.getAttributes().put(objectClass);
return newRole;
}
checkEdit();
WorkingCopy wc = getWorkingCopy();
LdapName dn = toDn(name);
- if (!daoHasRole(dn) && !wc.getNewUsers().containsKey(dn))
- return false;
- DirectoryUser user = (DirectoryUser) getRole(name);
- wc.getDeletedUsers().put(dn, user);
- // FIXME clarify directgroups
- for (DirectoryGroup group : getDirectGroups(user)) {
+ boolean actuallyDeleted;
+ if (daoHasRole(dn) || wc.getNewUsers().containsKey(dn)) {
+ DirectoryUser user = (DirectoryUser) getRole(name);
+ wc.getDeletedUsers().put(dn, user);
+ actuallyDeleted = true;
+ } else {// just removing from groups (e.g. system roles)
+ actuallyDeleted = false;
+ }
+ for (LdapName groupDn : getDirectGroups(dn)) {
+ DirectoryUser group = doGetRole(groupDn);
group.getAttributes().get(getMemberAttributeId())
.remove(dn.toString());
}
- return true;
+ return actuallyDeleted;
}
// TRANSACTION
return uri;
}
- protected void setUri(URI uri) {
- this.uri = uri;
- }
-
protected List<String> getIndexedUserProperties() {
return indexedUserProperties;
}
this.indexedUserProperties = indexedUserProperties;
}
- protected void setReadOnly(boolean isReadOnly) {
- this.isReadOnly = isReadOnly;
+ private static boolean readOnlyDefault(URI uri) {
+ if (uri == null)
+ return true;
+ if (uri.getScheme().equals("file")) {
+ File file = new File(uri);
+ if (file.exists())
+ return !file.canWrite();
+ else
+ return !file.getParentFile().canWrite();
+ }
+ return true;
}
public boolean isReadOnly() {
- return isReadOnly;
+ return readOnly;
}
UserAdmin getExternalRoles() {
return externalRoles;
}
- public void setExternalRoles(UserAdmin externalRoles) {
- this.externalRoles = externalRoles;
+ public String getBaseDn() {
+ return baseDn;
+ }
+
+ protected String getUserObjectClass() {
+ return userObjectClass;
+ }
+
+ protected String getGroupObjectClass() {
+ return groupObjectClass;
}
- public void setSyncRegistry(TransactionSynchronizationRegistry syncRegistry) {
- // this.syncRegistry = syncRegistry;
+ public Dictionary<String, ?> getProperties() {
+ return properties;
+ }
+
+ public void setExternalRoles(UserAdmin externalRoles) {
+ this.externalRoles = externalRoles;
}
public void setTransactionManager(TransactionManager transactionManager) {