Support i18n in directory
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / directory / ldap / AbstractLdapDirectory.java
index 36047d53e7e8786751fb0547fb1247346ab07e47..28d8d081ccdcfb875e5ce8941086f01cc3be3c00 100644 (file)
@@ -10,6 +10,7 @@ import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Locale;
 import java.util.Optional;
 import java.util.StringJoiner;
 
@@ -34,6 +35,7 @@ import org.argeo.util.transaction.WorkControl;
 import org.argeo.util.transaction.WorkingCopyXaResource;
 import org.argeo.util.transaction.XAResourceProvider;
 
+/** A {@link Directory} based either on LDAP or LDIF. */
 public abstract class AbstractLdapDirectory implements Directory, XAResourceProvider {
        protected static final String SHARED_STATE_USERNAME = "javax.security.auth.login.name";
        protected static final String SHARED_STATE_PASSWORD = "javax.security.auth.login.password";
@@ -146,23 +148,7 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
        }
 
        /*
-        * ABSTRACT METHODS
-        */
-
-//     public abstract HierarchyUnit doGetHierarchyUnit(LdapName dn);
-//
-//     public abstract Iterable<HierarchyUnit> doGetDirectHierarchyUnits(LdapName searchBase, boolean functionalOnly);
-//
-//     protected abstract Boolean daoHasEntry(LdapName dn);
-//
-//     protected abstract LdapEntry daoGetEntry(LdapName key) throws NameNotFoundException;
-//
-//     protected abstract List<LdapEntry> doGetEntries(LdapName searchBase, Filter f, boolean deep);
-//
-//     /** Returns the groups this user is a direct member of. */
-//     protected abstract List<LdapName> getDirectGroups(LdapName dn);
-       /*
-        * INITIALIZATION
+        * INITIALISATION
         */
 
        public void init() {
@@ -176,9 +162,9 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
        /*
         * CREATION
         */
-       protected abstract LdapEntry newUser(LdapName name, Attributes attrs);
+       protected abstract LdapEntry newUser(LdapName name);
 
-       protected abstract LdapEntry newGroup(LdapName name, Attributes attrs);
+       protected abstract LdapEntry newGroup(LdapName name);
 
        /*
         * EDITION
@@ -270,11 +256,7 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                                        } 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);
+                                               group = newGroup(groupDn);
                                                allRoles.add(group);
                                        }
                                }
@@ -282,10 +264,13 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                                throw new IllegalStateException("Cannot get memberOf groups for " + user, e);
                        }
                } else {
-                       for (LdapName groupDn : getDirectoryDao().getDirectGroups(user.getDn())) {
-                               // TODO check for loops
+                       directGroups: for (LdapName groupDn : getDirectoryDao().getDirectGroups(user.getDn())) {
                                LdapEntry group = doGetRole(groupDn);
                                if (group != null) {
+                                       if (allRoles.contains(group)) {
+                                               // important in order to avoi loops
+                                               continue directGroups;
+                                       }
                                        allRoles.add(group);
                                        collectGroups(group, allRoles);
                                }
@@ -312,6 +297,16 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                return getName();
        }
 
+       @Override
+       public String getHierarchyUnitLabel(Locale locale) {
+               String key = LdapNameUtils.getLastRdn(getBaseDn()).getType();
+               Object value = LdapEntry.getLocalized(asLdapEntry().getProperties(), key, locale);
+               if (value == null)
+                       value = getHierarchyUnitName();
+               assert value != null;
+               return value.toString();
+       }
+
        @Override
        public HierarchyUnit getParent() {
                return null;
@@ -327,12 +322,31 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                return this;
        }
 
+       @Override
+       public HierarchyUnit createHierarchyUnit(String path) {
+               checkEdit();
+               LdapEntryWorkingCopy wc = getWorkingCopy();
+               LdapName dn = pathToName(path);
+               if ((getDirectoryDao().entryExists(dn) && !wc.getDeletedData().containsKey(dn))
+                               || wc.getNewData().containsKey(dn))
+                       throw new IllegalArgumentException("Already a hierarchy unit " + path);
+               BasicAttributes attrs = new BasicAttributes(true);
+               attrs.put(LdapAttrs.objectClass.name(), LdapObjs.organizationalUnit.name());
+               Rdn nameRdn = dn.getRdn(dn.size() - 1);
+               // TODO deal with multiple attr RDN
+               attrs.put(nameRdn.getType(), nameRdn.getValue());
+               wc.getModifiedData().put(dn, attrs);
+               LdapHierarchyUnit newHierarchyUnit = new LdapHierarchyUnit(this, dn);
+               wc.getNewData().put(dn, newHierarchyUnit);
+               return newHierarchyUnit;
+       }
+
        /*
         * PATHS
         */
 
        @Override
-       public String getContext() {
+       public String getBase() {
                return getBaseDn().toString();
        }