]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.core/src/org/argeo/osgi/useradmin/AbstractUserDirectory.java
Fix issue with propagating user removal.
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / AbstractUserDirectory.java
index c7448b574366679cdaf575cb65b021eb940f010b..884e4ce09ff0ccd1e550f01fe3c402dadae12608 100644 (file)
@@ -26,7 +26,6 @@ import javax.naming.ldap.Rdn;
 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;
@@ -37,12 +36,12 @@ import org.osgi.framework.Filter;
 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);
 
@@ -56,24 +55,21 @@ public abstract class AbstractUserDirectory implements UserAdmin {
 
        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(Dictionary<String, ?> properties) {
+       AbstractUserDirectory(Dictionary<String, ?> properties) {
                // TODO make a copy?
                this.properties = properties;
 
-               String uriStr = LdapProperties.uri.getValue(properties);
+               String uriStr = UserAdminConf.uri.getValue(properties);
                if (uriStr == null)
                        uri = null;
                else
@@ -83,26 +79,21 @@ public abstract class AbstractUserDirectory implements UserAdmin {
                                throw new UserDirectoryException("Badly formatted URI", e);
                        }
 
-               baseDn = LdapProperties.baseDn.getValue(properties).toString();
-               String isReadOnly = LdapProperties.readOnly.getValue(properties);
+               baseDn = UserAdminConf.baseDn.getValue(properties).toString();
+               String isReadOnly = UserAdminConf.readOnly.getValue(properties);
                if (isReadOnly == null)
                        this.isReadOnly = readOnlyDefault(uri);
                else
                        this.isReadOnly = new Boolean(isReadOnly);
-               
-               this.userObjectClass = LdapProperties.userObjectClass
+
+               this.userObjectClass = UserAdminConf.userObjectClass
                                .getValue(properties);
-               this.groupObjectClass = LdapProperties.groupObjectClass
+               this.groupObjectClass = UserAdminConf.groupObjectClass
                                .getValue(properties);
        }
 
-       // public AbstractUserDirectory(URI uri, boolean isReadOnly) {
-       // this.uri = uri;
-       // this.isReadOnly = isReadOnly;
-       // }
-
-       /** 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);
 
@@ -110,9 +101,6 @@ public abstract class AbstractUserDirectory implements UserAdmin {
 
        protected abstract List<DirectoryUser> doGetRoles(Filter f);
 
-       protected abstract void doGetUser(String key, String value,
-                       List<DirectoryUser> collectedUsers);
-
        public void init() {
 
        }
@@ -125,10 +113,6 @@ public abstract class AbstractUserDirectory implements UserAdmin {
                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() {
@@ -172,7 +156,7 @@ public abstract class AbstractUserDirectory implements UserAdmin {
                }
        }
 
-       List<Role> getAllRoles(User user) {
+       List<Role> getAllRoles(DirectoryUser user) {
                List<Role> allRoles = new ArrayList<Role>();
                if (user != null) {
                        collectRoles(user, allRoles);
@@ -182,9 +166,10 @@ public abstract class AbstractUserDirectory implements UserAdmin {
                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);
                }
@@ -197,18 +182,22 @@ public abstract class AbstractUserDirectory implements UserAdmin {
        // 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();
@@ -257,6 +246,19 @@ public abstract class AbstractUserDirectory implements UserAdmin {
                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,
@@ -317,16 +319,20 @@ public abstract class AbstractUserDirectory implements UserAdmin {
                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
@@ -411,6 +417,10 @@ public abstract class AbstractUserDirectory implements UserAdmin {
                return groupObjectClass;
        }
 
+       public Dictionary<String, ?> getProperties() {
+               return properties;
+       }
+
        public void setExternalRoles(UserAdmin externalRoles) {
                this.externalRoles = externalRoles;
        }