X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fuseradmin%2FLdifUserAdmin.java;h=832e8e57819a87eaf72f261057615a1c312a544d;hb=35ba2254032b9ba0cf54fa542127818d235ac557;hp=aab96dd7ad4cf7c319e34da4fe1c15a9517c003e;hpb=780f1fce719bb66b4e4899c2339cb49d62c07dc6;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.enterprise/src/org/argeo/osgi/useradmin/LdifUserAdmin.java b/org.argeo.enterprise/src/org/argeo/osgi/useradmin/LdifUserAdmin.java index aab96dd7a..832e8e578 100644 --- a/org.argeo.enterprise/src/org/argeo/osgi/useradmin/LdifUserAdmin.java +++ b/org.argeo.enterprise/src/org/argeo/osgi/useradmin/LdifUserAdmin.java @@ -9,7 +9,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Collections; import java.util.Dictionary; import java.util.HashSet; import java.util.Hashtable; @@ -39,28 +41,43 @@ public class LdifUserAdmin extends AbstractUserDirectory { private SortedMap groups = new TreeMap(); public LdifUserAdmin(String uri, String baseDn) { - this(fromUri(uri, baseDn)); + this(fromUri(uri, baseDn), false); } public LdifUserAdmin(Dictionary properties) { - super(null, properties); + this(properties, false); } - public LdifUserAdmin(URI uri, Dictionary properties) { - super(uri, properties); + protected LdifUserAdmin(Dictionary properties, boolean scoped) { + super(null, properties, scoped); } - @Deprecated - public LdifUserAdmin(InputStream in) { - super(null, new Hashtable()); - load(in); + public LdifUserAdmin(URI uri, Dictionary properties) { + super(uri, properties, false); } @Override protected AbstractUserDirectory scope(User user) { + Dictionary credentials = user.getCredentials(); + String username = (String) credentials.get(SHARED_STATE_USERNAME); + if (username == null) + username = user.getName(); + Object pwdCred = credentials.get(SHARED_STATE_PASSWORD); + byte[] pwd = (byte[]) pwdCred; + if (pwd != null) { + char[] password = DigestUtils.bytesToChars(pwd); + User directoryUser = (User) getRole(username); + if (!directoryUser.hasCredential(null, password)) + throw new UserDirectoryException("Invalid credentials"); + } else { + throw new UserDirectoryException("Password is required"); + } Dictionary properties = cloneProperties(); properties.put(UserAdminConf.readOnly.name(), "true"); - return new LdifUserAdmin(properties); + LdifUserAdmin scopedUserAdmin = new LdifUserAdmin(properties, true); + scopedUserAdmin.groups = Collections.unmodifiableSortedMap(groups); + scopedUserAdmin.users = Collections.unmodifiableSortedMap(users); + return scopedUserAdmin; } private static Dictionary fromUri(String uri, String baseDn) { @@ -71,13 +88,15 @@ public class LdifUserAdmin extends AbstractUserDirectory { } public void init() { + try { - if (getUri().getScheme().equals("file")) { - File file = new File(getUri()); + URI u = new URI(getUri()); + if (u.getScheme().equals("file")) { + File file = new File(u); if (!file.exists()) return; } - load(getUri().toURL().openStream()); + load(u.toURL().openStream()); } catch (Exception e) { throw new UserDirectoryException("Cannot open URL " + getUri(), e); } @@ -88,9 +107,9 @@ public class LdifUserAdmin extends AbstractUserDirectory { throw new UserDirectoryException("Cannot save LDIF user admin: no URI is set"); if (isReadOnly()) throw new UserDirectoryException("Cannot save LDIF user admin: " + getUri() + " is read-only"); - try (FileOutputStream out = new FileOutputStream(new File(getUri()))) { + try (FileOutputStream out = new FileOutputStream(new File(new URI(getUri())))) { save(out); - } catch (IOException e) { + } catch (IOException | URISyntaxException e) { throw new UserDirectoryException("Cannot save user admin to " + getUri(), e); } } @@ -132,10 +151,10 @@ public class LdifUserAdmin extends AbstractUserDirectory { objectClasses: while (objectClasses.hasMore()) { String objectClass = objectClasses.next().toString(); // System.out.println(" " + objectClass); - if (objectClass.equals(inetOrgPerson.name())) { + if (objectClass.toLowerCase().equals(inetOrgPerson.name().toLowerCase())) { users.put(key, new LdifUser(this, key, attributes)); break objectClasses; - } else if (objectClass.equals(getGroupObjectClass())) { + } else if (objectClass.toLowerCase().equals(getGroupObjectClass().toLowerCase())) { groups.put(key, new LdifGroup(this, key, attributes)); break objectClasses; } @@ -149,9 +168,7 @@ public class LdifUserAdmin extends AbstractUserDirectory { public void destroy() { if (users == null || groups == null) throw new UserDirectoryException("User directory " + getBaseDn() + " is already destroyed"); - users.clear(); users = null; - groups.clear(); groups = null; } @@ -169,7 +186,6 @@ public class LdifUserAdmin extends AbstractUserDirectory { return users.containsKey(dn) || groups.containsKey(dn); } - @SuppressWarnings("unchecked") protected List doGetRoles(Filter f) { ArrayList res = new ArrayList(); if (f == null) { @@ -177,13 +193,6 @@ public class LdifUserAdmin extends AbstractUserDirectory { res.addAll(groups.values()); } else { for (DirectoryUser user : users.values()) { - // System.out.println("\n" + user.getName()); - // Dictionary props = user.getProperties(); - // for (Enumeration keys = props.keys(); keys - // .hasMoreElements();) { - // String key = keys.nextElement(); - // System.out.println(" " + key + "=" + props.get(key)); - // } if (f.match(user.getProperties())) res.add(user); }