Simplify simple user admin configurations
authormbaudier <mbaudier@mostar>
Mon, 6 Nov 2017 09:07:34 +0000 (10:07 +0100)
committermbaudier <mbaudier@mostar>
Mon, 6 Nov 2017 09:07:34 +0000 (10:07 +0100)
org.argeo.cms/src/org/argeo/cms/internal/kernel/FirstInit.java
org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeUserAdmin.java
org.argeo.enterprise/src/org/argeo/osgi/useradmin/AbstractUserDirectory.java
org.argeo.enterprise/src/org/argeo/osgi/useradmin/LdapUserAdmin.java
org.argeo.enterprise/src/org/argeo/osgi/useradmin/LdifUserAdmin.java
org.argeo.enterprise/src/org/argeo/osgi/useradmin/UserAdminConf.java

index 1c7cb149752a7c30ce6b56da3ad1f9db8da21929..7406b67e4308839dc72ce37e719ad987e7559390 100644 (file)
@@ -100,7 +100,8 @@ class FirstInit {
                String nodeRolesUri = getFrameworkProp(NodeConstants.ROLES_URI);
                String baseNodeRoleDn = NodeConstants.ROLES_BASEDN;
                if (nodeRolesUri == null) {
-                       File nodeRolesFile = new File(nodeBaseDir, baseNodeRoleDn + ".ldif");
+                       nodeRolesUri = baseNodeRoleDn + ".ldif";
+                       File nodeRolesFile = new File(nodeBaseDir, nodeRolesUri);
                        if (!nodeRolesFile.exists())
                                try {
                                        FileUtils.copyInputStreamToFile(getClass().getResourceAsStream(baseNodeRoleDn + ".ldif"),
@@ -108,7 +109,7 @@ class FirstInit {
                                } catch (IOException e) {
                                        throw new CmsException("Cannot copy demo resource", e);
                                }
-                       nodeRolesUri = nodeRolesFile.toURI().toString();
+                       // nodeRolesUri = nodeRolesFile.toURI().toString();
                }
                uris.add(nodeRolesUri);
 
@@ -116,7 +117,8 @@ class FirstInit {
                String userAdminUris = getFrameworkProp(NodeConstants.USERADMIN_URIS);
                if (userAdminUris == null) {
                        String demoBaseDn = "dc=example,dc=com";
-                       File businessRolesFile = new File(nodeBaseDir, demoBaseDn + ".ldif");
+                       userAdminUris = demoBaseDn + ".ldif";
+                       File businessRolesFile = new File(nodeBaseDir, userAdminUris);
                        if (!businessRolesFile.exists())
                                try {
                                        FileUtils.copyInputStreamToFile(getClass().getResourceAsStream(demoBaseDn + ".ldif"),
@@ -124,7 +126,7 @@ class FirstInit {
                                } catch (IOException e) {
                                        throw new CmsException("Cannot copy demo resource", e);
                                }
-                       userAdminUris = businessRolesFile.toURI().toString();
+                       // userAdminUris = businessRolesFile.toURI().toString();
                        log.warn("## DEV Using dummy base DN " + demoBaseDn);
                        // TODO downgrade security level
                }
@@ -142,8 +144,8 @@ class FirstInit {
                                        if (uri.startsWith("/") || uri.startsWith("./") || uri.startsWith("../"))
                                                u = new File(uri).getCanonicalFile().toURI();
                                        else if (!uri.contains("/")) {
-                                               u = KernelUtils.getOsgiInstanceUri(KernelConstants.DIR_NODE + '/' + uri);
-                                               // u = new URI(nodeBaseDir.toURI() + uri);
+                                               // u = KernelUtils.getOsgiInstanceUri(KernelConstants.DIR_NODE + '/' + uri);
+                                               u = new URI(uri);
                                        } else
                                                throw new CmsException("Cannot interpret " + uri + " as an uri");
                                } else if (u.getScheme().equals("file")) {
index caadadefa8785054696476473892a90419b5262d..8410b3958aef378d49a34dbd260b43f83696a128 100644 (file)
@@ -95,14 +95,18 @@ class NodeUserAdmin extends AggregatingUserAdmin implements ManagedServiceFactor
                String uri = (String) properties.get(UserAdminConf.uri.name());
                URI u;
                try {
-                       u = new URI(uri);
+                       if (uri == null) {
+                               String baseDn = (String) properties.get(UserAdminConf.baseDn.name());
+                               u = KernelUtils.getOsgiInstanceUri(KernelConstants.DIR_NODE + '/' + baseDn + ".ldif");
+                       } else
+                               u = new URI(uri);
                } catch (URISyntaxException e) {
                        throw new CmsException("Badly formatted URI " + uri, e);
                }
 
                // Create
                AbstractUserDirectory userDirectory = u.getScheme().equals("ldap") ? new LdapUserAdmin(properties)
-                               : new LdifUserAdmin(properties);
+                               : new LdifUserAdmin(u, properties);
                Object realm = userDirectory.getProperties().get(UserAdminConf.realm.name());
                addUserDirectory(userDirectory);
 
index 081d9e1faa76888705c314355c6a86c4e03b0f47..e4b25ae81caceab5b2ff07b40364bc59bba67c86 100644 (file)
@@ -67,22 +67,27 @@ public abstract class AbstractUserDirectory implements UserAdmin, UserDirectory
        private TransactionManager transactionManager;
        private WcXaResource xaResource = new WcXaResource(this);
 
-       public AbstractUserDirectory(Dictionary<String, ?> props) {
+       public AbstractUserDirectory(URI uriArg, Dictionary<String, ?> props) {
                properties = new Hashtable<String, Object>();
                for (Enumeration<String> keys = props.keys(); keys.hasMoreElements();) {
                        String key = keys.nextElement();
                        properties.put(key, props.get(key));
                }
 
-               String uriStr = UserAdminConf.uri.getValue(properties);
-               if (uriStr == null)
-                       uri = null;
-               else
-                       try {
-                               uri = new URI(uriStr);
-                       } catch (URISyntaxException e) {
-                               throw new UserDirectoryException("Badly formatted URI " + uriStr, e);
-                       }
+               if (uriArg != null) {
+                       uri = uriArg;
+                       // uri from properties is ignored
+               } else {
+                       String uriStr = UserAdminConf.uri.getValue(properties);
+                       if (uriStr == null)
+                               uri = null;
+                       else
+                               try {
+                                       uri = new URI(uriStr);
+                               } catch (URISyntaxException e) {
+                                       throw new UserDirectoryException("Badly formatted URI " + uriStr, e);
+                               }
+               }
 
                userObjectClass = UserAdminConf.userObjectClass.getValue(properties);
                userBase = UserAdminConf.userBase.getValue(properties);
@@ -410,6 +415,8 @@ public abstract class AbstractUserDirectory implements UserAdmin, UserDirectory
        private static boolean readOnlyDefault(URI uri) {
                if (uri == null)
                        return true;
+               if (uri.getScheme() == null)
+                       return false;// assume relative file to be writable
                if (uri.getScheme().equals("file")) {
                        File file = new File(uri);
                        if (file.exists())
index 7486e3ecf25b38685d6fe8d996ba573f6325ff07..494d9c2dfaf0c0b78981f0615daf1719a93cb496 100644 (file)
@@ -39,7 +39,7 @@ public class LdapUserAdmin extends AbstractUserDirectory {
        private InitialLdapContext initialLdapContext = null;
 
        public LdapUserAdmin(Dictionary<String, ?> properties) {
-               super(properties);
+               super(null, properties);
                try {
                        Hashtable<String, Object> connEnv = new Hashtable<String, Object>();
                        connEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
index 8ec967b7007a73c8875e557b6e8ecee6c0305105..3e683b6116fbc2b435ff53e9871f3319d63f676f 100644 (file)
@@ -8,6 +8,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.HashSet;
@@ -41,11 +42,16 @@ public class LdifUserAdmin extends AbstractUserDirectory {
        }
 
        public LdifUserAdmin(Dictionary<String, ?> properties) {
-               super(properties);
+               super(null, properties);
        }
 
+       public LdifUserAdmin(URI uri, Dictionary<String, ?> properties) {
+               super(uri, properties);
+       }
+
+       @Deprecated
        public LdifUserAdmin(InputStream in) {
-               super(new Hashtable<String, Object>());
+               super(null, new Hashtable<String, Object>());
                load(in);
        }
 
index 83cbf795c7fc91c9c118298b4f8a3f3b53fa7f4b..19426b6c5c072a8ee22108d22429597e0e1aa8a8 100644 (file)
@@ -129,6 +129,7 @@ public enum UserAdminConf {
                                scheme = u.getScheme();
                        }
                        String path = u.getPath();
+                       // base DN
                        String bDn = path.substring(path.lastIndexOf('/') + 1, path.length());
                        if (bDn.endsWith(".ldif"))
                                bDn = bDn.substring(0, bDn.length() - ".ldif".length());
@@ -162,7 +163,7 @@ public enum UserAdminConf {
                                res.put(Context.SECURITY_PRINCIPAL, principal);
                        if (credentials != null)
                                res.put(Context.SECURITY_CREDENTIALS, credentials);
-                       if (scheme != null) {
+                       if (scheme != null) {// relative URIs are dealt with externally
                                URI bareUri = new URI(scheme, null, u.getHost(), u.getPort(),
                                                scheme.equals("file") ? u.getPath() : null, null, null);
                                res.put(uri.name(), bareUri.toString());