]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.util/src/org/argeo/util/directory/ldap/AbstractLdapDirectory.java
Make CMS running without data area, and remove unnecessary dependencies.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / directory / ldap / AbstractLdapDirectory.java
index eab82e0ec3a0ba2ea2426b01d62a6d7ab20f4c56..36047d53e7e8786751fb0547fb1247346ab07e47 100644 (file)
@@ -19,6 +19,7 @@ import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttributes;
 import javax.naming.ldap.LdapName;
 import javax.naming.ldap.Rdn;
 import javax.transaction.xa.XAResource;
@@ -37,8 +38,8 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
        protected static final String SHARED_STATE_USERNAME = "javax.security.auth.login.name";
        protected static final String SHARED_STATE_PASSWORD = "javax.security.auth.login.password";
 
-       protected final LdapName baseDn;
-       protected final Hashtable<String, Object> configProperties;
+       private final LdapName baseDn;
+       private final Hashtable<String, Object> configProperties;
        private final Rdn userBaseRdn, groupBaseRdn, systemRoleBaseRdn;
        private final String userObjectClass, groupObjectClass;
        private String memberAttributeId = "member";
@@ -65,7 +66,11 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                        String key = keys.nextElement();
                        configProperties.put(key, props.get(key));
                }
-               baseDn = toLdapName(DirectoryConf.baseDn.getValue(configProperties));
+
+               String baseDnStr = DirectoryConf.baseDn.getValue(configProperties);
+               if (baseDnStr == null)
+                       throw new IllegalArgumentException("Base DN must be specified: " + configProperties);
+               baseDn = toLdapName(baseDnStr);
                this.scoped = scoped;
 
                if (uriArg != null) {
@@ -113,20 +118,31 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                        disabled = Boolean.parseBoolean(disabledStr);
                else
                        disabled = false;
-
-               URI u = URI.create(uri);
-               if (!getRealm().isEmpty() || DirectoryConf.SCHEME_LDAP.equals(u.getScheme())
-                               || DirectoryConf.SCHEME_LDAPS.equals(u.getScheme())) {
+               if (!getRealm().isEmpty()) {
+                       // IPA multiple LDAP causes URI parsing to fail
+                       // TODO manage generic redundant LDAP case
                        directoryDao = new LdapDao(this);
-               } else if (DirectoryConf.SCHEME_FILE.equals(u.getScheme())) {
-                       directoryDao = new LdifDao(this);
-               } else if (DirectoryConf.SCHEME_OS.equals(u.getScheme())) {
-                       directoryDao = new OsUserDirectory(this);
-                       // singleUser = true;
                } else {
-                       throw new IllegalArgumentException("Unsupported scheme " + u.getScheme());
+                       if (uri != null) {
+                               URI u = URI.create(uri);
+                               if (DirectoryConf.SCHEME_LDAP.equals(u.getScheme())
+                                               || DirectoryConf.SCHEME_LDAPS.equals(u.getScheme())) {
+                                       directoryDao = new LdapDao(this);
+                               } else if (DirectoryConf.SCHEME_FILE.equals(u.getScheme())) {
+                                       directoryDao = new LdifDao(this);
+                               } else if (DirectoryConf.SCHEME_OS.equals(u.getScheme())) {
+                                       directoryDao = new OsUserDirectory(this);
+                                       // singleUser = true;
+                               } else {
+                                       throw new IllegalArgumentException("Unsupported scheme " + u.getScheme());
+                               }
+                       } else {
+                               // in memory
+                               directoryDao = new LdifDao(this);
+                       }
                }
-               xaResource = new WorkingCopyXaResource<>(directoryDao);
+               if (directoryDao != null)
+                       xaResource = new WorkingCopyXaResource<>(directoryDao);
        }
 
        /*
@@ -249,8 +265,18 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                                        Object value = values.next();
                                        LdapName groupDn = new LdapName(value.toString());
                                        LdapEntry group = doGetRole(groupDn);
-                                       if (group != null)
+                                       if (group != null) {
+                                               allRoles.add(group);
+                                       } else {
+                                               // user doesn't have the right to retrieve role, but we know it exists
+                                               // otherwise memberOf would not work
+                                               Attributes a = new BasicAttributes();
+                                               a.put(LdapNameUtils.getLastRdn(groupDn).getType(),
+                                                               LdapNameUtils.getLastRdn(groupDn).getValue());
+                                               a.put(LdapAttrs.objectClass.name(), LdapObjs.groupOfNames.name());
+                                               group = newGroup(groupDn, a);
                                                allRoles.add(group);
+                                       }
                                }
                        } catch (NamingException e) {
                                throw new IllegalStateException("Cannot get memberOf groups for " + user, e);
@@ -281,6 +307,26 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                return directoryDao.doGetDirectHierarchyUnits(baseDn, functionalOnly);
        }
 
+       @Override
+       public String getHierarchyUnitName() {
+               return getName();
+       }
+
+       @Override
+       public HierarchyUnit getParent() {
+               return null;
+       }
+
+       @Override
+       public boolean isFunctional() {
+               return true;
+       }
+
+       @Override
+       public Directory getDirectory() {
+               return this;
+       }
+
        /*
         * PATHS
         */
@@ -314,9 +360,11 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                        LdapName name = (LdapName) getBaseDn().clone();
                        String[] segments = path.split("/");
                        Rdn parentRdn = null;
-                       for (String segment : segments) {
+                       // segments[0] is the directory itself
+                       for (int i = 0; i < segments.length; i++) {
+                               String segment = segments[i];
                                // TODO make attr names configurable ?
-                               String attr = LdapAttrs.ou.name();
+                               String attr = path.startsWith("accounts/")/* IPA */ ? LdapAttrs.cn.name() : LdapAttrs.ou.name();
                                if (parentRdn != null) {
                                        if (getUserBaseRdn().equals(parentRdn))
                                                attr = LdapAttrs.uid.name();
@@ -339,6 +387,10 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
        /*
         * UTILITIES
         */
+       protected boolean isExternal(LdapName name) {
+               return !name.startsWith(baseDn);
+       }
+
        protected static boolean hasObjectClass(Attributes attrs, LdapObjs objectClass) {
                return hasObjectClass(attrs, objectClass.name());
        }