- Start factorizing LDIF and LDAP
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / LdifUserAdmin.java
index acbf1112f1e24d92b35926420aabc538406ecf5c..b1e9ceb49808a26f88ede1b0c56c004575fb731b 100644 (file)
@@ -4,7 +4,6 @@ import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Dictionary;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -17,22 +16,18 @@ import javax.naming.NamingEnumeration;
 import javax.naming.directory.Attributes;
 import javax.naming.ldap.LdapName;
 
+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.Role;
 import org.osgi.service.useradmin.User;
-import org.osgi.service.useradmin.UserAdmin;
 
 /** User admin implementation using LDIF file(s) as backend. */
-public class LdifUserAdmin implements UserAdmin {
+public class LdifUserAdmin extends AbstractLdapUserAdmin {
        SortedMap<LdapName, LdifUser> users = new TreeMap<LdapName, LdifUser>();
        SortedMap<LdapName, LdifGroup> groups = new TreeMap<LdapName, LdifGroup>();
 
-       private final boolean isReadOnly;
-       private final URI uri;
-
-       private List<String> indexedUserProperties = Arrays.asList(new String[] {
-                       "uid", "mail", "cn" });
        private Map<String, Map<String, LdifUser>> userIndexes = new LinkedHashMap<String, Map<String, LdifUser>>();
 
        public LdifUserAdmin(String uri) {
@@ -40,28 +35,28 @@ public class LdifUserAdmin implements UserAdmin {
        }
 
        public LdifUserAdmin(String uri, boolean isReadOnly) {
-               this.isReadOnly = isReadOnly;
+               setReadOnly(isReadOnly);
                try {
-                       this.uri = new URI(uri);
+                       setUri(new URI(uri));
                } catch (URISyntaxException e) {
                        throw new ArgeoUserAdminException("Invalid URI " + uri, e);
                }
 
-               if (!isReadOnly && !this.uri.getScheme().equals("file:"))
-                       throw new UnsupportedOperationException(this.uri.getScheme()
+               if (!isReadOnly && !getUri().getScheme().equals("file:"))
+                       throw new UnsupportedOperationException(getUri().getScheme()
                                        + "not supported read-write.");
 
                try {
-                       load(this.uri.toURL().openStream());
+                       load(getUri().toURL().openStream());
                } catch (Exception e) {
-                       throw new ArgeoUserAdminException("Cannot open URL " + this.uri, e);
+                       throw new ArgeoUserAdminException("Cannot open URL " + getUri(), e);
                }
        }
 
        public LdifUserAdmin(InputStream in) {
                load(in);
-               isReadOnly = true;
-               this.uri = null;
+               setReadOnly(true);
+               setUri(null);
        }
 
        protected void load(InputStream in) {
@@ -89,12 +84,12 @@ public class LdifUserAdmin implements UserAdmin {
                                group.loadMembers(this);
 
                        // indexes
-                       for (String attr : indexedUserProperties)
+                       for (String attr : getIndexedUserProperties())
                                userIndexes.put(attr, new TreeMap<String, LdifUser>());
 
                        for (LdifUser user : users.values()) {
                                Dictionary<String, Object> properties = user.getProperties();
-                               for (String attr : indexedUserProperties) {
+                               for (String attr : getIndexedUserProperties()) {
                                        Object value = properties.get(attr);
                                        if (value != null) {
                                                LdifUser otherUser = userIndexes.get(attr).put(
@@ -102,7 +97,7 @@ public class LdifUserAdmin implements UserAdmin {
                                                if (otherUser != null)
                                                        throw new ArgeoUserAdminException("User " + user
                                                                        + " and user " + otherUser
-                                                                       + " both habe property " + attr
+                                                                       + " both have property " + attr
                                                                        + " set to " + value);
                                        }
                                }
@@ -155,13 +150,20 @@ public class LdifUserAdmin implements UserAdmin {
 
        @Override
        public Role[] getRoles(String filter) throws InvalidSyntaxException {
+               ArrayList<Role> res = new ArrayList<Role>();
                if (filter == null) {
-                       ArrayList<Role> res = new ArrayList<Role>();
                        res.addAll(users.values());
                        res.addAll(groups.values());
-                       return res.toArray(new Role[res.size()]);
+               } else {
+                       Filter f = FrameworkUtil.createFilter(filter);
+                       for (LdifUser user : users.values())
+                               if (f.match(user.getProperties()))
+                                       res.add(user);
+                       for (LdifUser group : groups.values())
+                               if (f.match(group.getProperties()))
+                                       res.add(group);
                }
-               throw new UnsupportedOperationException();
+               return res.toArray(new Role[res.size()]);
        }
 
        @Override
@@ -175,7 +177,7 @@ public class LdifUserAdmin implements UserAdmin {
 
                // Try all indexes
                List<LdifUser> collectedUsers = new ArrayList<LdifUser>(
-                               indexedUserProperties.size());
+                               getIndexedUserProperties().size());
                // try dn
                LdifUser user = null;
                try {
@@ -197,8 +199,4 @@ public class LdifUserAdmin implements UserAdmin {
                // throw new UnsupportedOperationException();
        }
 
-       public boolean getIsReadOnly() {
-               return isReadOnly;
-       }
-
 }