X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.security.core%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fuseradmin%2FLdifUserAdmin.java;h=4613ef5289fbb331ecc61e8d982c93c6d3e41c87;hb=ea63d7d123a50ff10657946ce3d928a57944621d;hp=830488589c2331311d1f7148e0dc31b6b4993046;hpb=48a1e034607afb28d84480463b57f74a80a29929;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java b/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java index 830488589..4613ef528 100644 --- a/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java +++ b/org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java @@ -8,28 +8,26 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.Dictionary; import java.util.Hashtable; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; -import javax.naming.InvalidNameException; import javax.naming.NamingEnumeration; import javax.naming.directory.Attributes; import javax.naming.ldap.LdapName; +import javax.transaction.TransactionManager; import org.apache.commons.io.IOUtils; import org.osgi.framework.Filter; import org.osgi.service.useradmin.Role; -import org.osgi.service.useradmin.User; -/** User admin implementation using LDIF file(s) as backend. */ +/** + * A user admin based on a LDIF files. Requires a {@link TransactionManager} and + * an open transaction for write access. + */ public class LdifUserAdmin extends AbstractUserDirectory { - SortedMap users = new TreeMap(); - SortedMap groups = new TreeMap(); - - private Map> userIndexes = new LinkedHashMap>(); + private SortedMap users = new TreeMap(); + private SortedMap groups = new TreeMap(); public LdifUserAdmin(String uri, String baseDn) { this(fromUri(uri, baseDn)); @@ -42,14 +40,12 @@ public class LdifUserAdmin extends AbstractUserDirectory { public LdifUserAdmin(InputStream in) { super(new Hashtable()); load(in); - setReadOnly(true); - setUri(null); } private static Dictionary fromUri(String uri, String baseDn) { Hashtable res = new Hashtable(); - res.put(UserAdminProps.uri.getFullName(), uri); - res.put(UserAdminProps.baseDn.getFullName(), baseDn); + res.put(UserAdminConf.uri.property(), uri); + res.put(UserAdminConf.baseDn.property(), baseDn); return res; } @@ -67,8 +63,12 @@ public class LdifUserAdmin extends AbstractUserDirectory { } public void save() { - if (getUri() == null || isReadOnly()) - throw new UserDirectoryException("Cannot save LDIF user admin"); + if (getUri() == null) + 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()))) { save(out); } catch (IOException e) { @@ -89,7 +89,6 @@ public class LdifUserAdmin extends AbstractUserDirectory { } } - @SuppressWarnings("unchecked") protected void load(InputStream in) { try { users.clear(); @@ -112,26 +111,6 @@ public class LdifUserAdmin extends AbstractUserDirectory { } } } - - // indexes - for (String attr : getIndexedUserProperties()) - userIndexes.put(attr, new TreeMap()); - - for (DirectoryUser user : users.values()) { - Dictionary properties = user.getProperties(); - for (String attr : getIndexedUserProperties()) { - Object value = properties.get(attr); - if (value != null) { - DirectoryUser otherUser = userIndexes.get(attr).put( - value.toString(), user); - if (otherUser != null) - throw new UserDirectoryException("User " + user - + " and user " + otherUser - + " both have property " + attr - + " set to " + value); - } - } - } } catch (Exception e) { throw new UserDirectoryException( "Cannot load user admin service from LDIF", e); @@ -175,32 +154,13 @@ public class LdifUserAdmin extends AbstractUserDirectory { return res; } - protected void doGetUser(String key, String value, - List collectedUsers) { - assert key != null; - DirectoryUser user = userIndexes.get(key).get(value); - if (user != null) - collectedUsers.add(user); - } - @Override - protected List getDirectGroups(User user) { - LdapName dn; - if (user instanceof LdifUser) - dn = ((LdifUser) user).getDn(); - else - try { - dn = new LdapName(user.getName()); - } catch (InvalidNameException e) { - throw new UserDirectoryException("Badly formatted user name " - + user.getName(), e); - } - - List directGroups = new ArrayList(); + protected List getDirectGroups(LdapName dn) { + List directGroups = new ArrayList(); for (LdapName name : groups.keySet()) { DirectoryGroup group = groups.get(name); if (group.getMemberNames().contains(dn)) - directGroups.add(group); + directGroups.add(group.getDn()); } return directGroups; }