LDAP support for hierarchy unit. Code clean up.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / osgi / useradmin / AbstractUserDirectory.java
index e13f56289ce8b44b2f9629d2e297aadccbf03c03..dde39e0abd287fa0ddcb3f69cf6927574f1c26c0 100644 (file)
@@ -129,6 +129,10 @@ abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
                        disabled = false;
        }
 
+       /*
+        * ABSTRACT METHODS
+        */
+
        /** Returns the groups this user is a direct member of. */
        protected abstract List<LdapName> getDirectGroups(LdapName dn);
 
@@ -140,6 +144,14 @@ abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
 
        protected abstract AbstractUserDirectory scope(User user);
 
+       protected abstract HierarchyUnit doGetHierarchyUnit(LdapName dn);
+
+       protected abstract Iterable<HierarchyUnit> doGetDirectHierarchyUnits(LdapName searchBase, boolean functionalOnly);
+
+       /*
+        * INITIALIZATION
+        */
+
        public void init() {
 
        }
@@ -500,55 +512,25 @@ abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
        /*
         * HIERARCHY
         */
-//     @Override
-//     public int getHierarchyChildCount() {
-//             return 0;
-//     }
-//
-//     @Override
-//     public HierarchyUnit getHierarchyChild(int i) {
-//             throw new IllegalArgumentException("No child hierarchy unit available");
-//     }
-//
-//     @Override
-//     public HierarchyUnit getParent() {
-//             return null;
-//     }
-//
-//     @Override
-//     public int getHierarchyUnitType() {
-//             return 0;
-//     }
-//
-//     @Override
-//     public String getHierarchyUnitName() {
-//             String name = LdapNameUtils.getLastRdnValue(baseDn);
-//             // TODO check ou, o, etc.
-//             return name;
-//     }
-
        @Override
        public HierarchyUnit getHierarchyUnit(String path) {
-               throw new UnsupportedOperationException();
+               LdapName dn = pathToName(path);
+               return doGetHierarchyUnit(dn);
        }
 
        @Override
        public HierarchyUnit getHierarchyUnit(Role role) {
-               throw new UnsupportedOperationException();
+               LdapName dn = LdapNameUtils.toLdapName(role.getName());
+               LdapName huDn = LdapNameUtils.getParent(dn);
+               HierarchyUnit hierarchyUnit = doGetHierarchyUnit(huDn);
+               if (hierarchyUnit == null)
+                       throw new IllegalStateException("No hierarchy unit found for " + role);
+               return hierarchyUnit;
        }
 
-//     @Override
-//     public List<? extends Role> getHierarchyUnitRoles(String filter, boolean deep) {
-//             try {
-//                     return getRoles(getBaseDn(), filter, deep);
-//             } catch (InvalidSyntaxException e) {
-//                     throw new IllegalArgumentException("Cannot filter " + filter + " " + getBaseDn(), e);
-//             }
-//     }
-
        @Override
        public Iterable<HierarchyUnit> getDirectHierarchyUnits(boolean functionalOnly) {
-               throw new UnsupportedOperationException();
+               return doGetDirectHierarchyUnits(baseDn, functionalOnly);
        }
 
        /*
@@ -562,7 +544,7 @@ abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
        protected DirectoryGroup newGroup(LdapName name, Attributes attrs) {
                if (LdapNameUtils.getParentRdn(name).equals(getSystemRoleBaseRdn()))
                        return new LdifGroup.LdifSystemPermissions(this, name, attrs);
-               
+
                if (hasObjectClass(attrs, LdapObjs.organization))
                        return new LdifGroup.LdifOrganization(this, name, attrs);
                else