You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2017/12/22 14:30:52 UTC

[sling-org-apache-sling-jcr-repoinit] branch master updated: SLING-7226 - support intermediate path in user and service user creation

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git


The following commit(s) were added to refs/heads/master by this push:
     new d039439  SLING-7226 - support intermediate path in user and service user creation
d039439 is described below

commit d039439fe21905a8a086979c375c0c21e5e08d49
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Dec 22 15:30:37 2017 +0100

    SLING-7226 - support intermediate path in user and service user creation
---
 .../apache/sling/jcr/repoinit/impl/UserUtil.java   | 43 ++++++++++++++++++++--
 .../sling/jcr/repoinit/impl/UserVisitor.java       |  4 +-
 .../sling/jcr/repoinit/CreateServiceUsersTest.java | 10 +++++
 .../apache/sling/jcr/repoinit/CreateUsersTest.java | 16 ++++++++
 .../jcr/repoinit/impl/ManyServiceUsersTest.java    |  2 +-
 .../apache/sling/jcr/repoinit/impl/TestUtil.java   | 20 ++++++++++
 6 files changed, 88 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/sling/jcr/repoinit/impl/UserUtil.java b/src/main/java/org/apache/sling/jcr/repoinit/impl/UserUtil.java
index 60ea023..ca88864 100644
--- a/src/main/java/org/apache/sling/jcr/repoinit/impl/UserUtil.java
+++ b/src/main/java/org/apache/sling/jcr/repoinit/impl/UserUtil.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.jcr.repoinit.impl;
 
+import java.security.Principal;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
@@ -27,6 +28,35 @@ import org.apache.jackrabbit.api.security.user.UserManager;
 /** Utilities for User management */
 public class UserUtil {
 
+    static class SameNamePrincipal implements Principal {
+
+        private final String name;
+
+        SameNamePrincipal(String name) {
+            if(name == null) {
+                throw new IllegalArgumentException("Name cannot be null");
+            }
+            this.name = name;
+        }
+
+        @Override
+        public String getName() {
+            return name;
+        }
+
+        @Override
+        public boolean equals(Object other) {
+            return (
+                    other instanceof SameNamePrincipal)
+                    && (this.name.equals(((SameNamePrincipal)other).name));
+        }
+
+        @Override
+        public int hashCode() {
+            return name.hashCode();
+        }
+    }
+
     public static UserManager getUserManager(Session session) throws RepositoryException {
         if(!(session instanceof JackrabbitSession)) {
             throw new IllegalArgumentException("Session is not a JackrabbitSession");
@@ -39,8 +69,8 @@ public class UserUtil {
     }
 
     /** Create a service user - fails if it already exists */
-    public static void createServiceUser(Session session, String username) throws RepositoryException {
-        getUserManager(session).createSystemUser(username, null);
+    public static void createServiceUser(Session session, String username, String path) throws RepositoryException {
+        getUserManager(session).createSystemUser(username, path);
     }
 
     /** True if specified service user exists */
@@ -77,8 +107,13 @@ public class UserUtil {
     }
 
     /** Create a user - fails if it already exists */
-    public static void createUser(Session session, String username, String password) throws RepositoryException {
-        getUserManager(session).createUser(username, password);
+    public static void createUser(Session session, String username, String password, String path) throws RepositoryException {
+        if(path == null) {
+            getUserManager(session).createUser(username, password);
+        } else {
+            final Principal p = new SameNamePrincipal(username);
+            getUserManager(session).createUser(username, password, p, path);
+        }
     }
 
     /** True if specified user exists */
diff --git a/src/main/java/org/apache/sling/jcr/repoinit/impl/UserVisitor.java b/src/main/java/org/apache/sling/jcr/repoinit/impl/UserVisitor.java
index e04edde..e53047e 100644
--- a/src/main/java/org/apache/sling/jcr/repoinit/impl/UserVisitor.java
+++ b/src/main/java/org/apache/sling/jcr/repoinit/impl/UserVisitor.java
@@ -44,7 +44,7 @@ class UserVisitor extends DoNothingVisitor {
         try {
             if (!UserUtil.userExists(session, username)) {
                 log.info("Creating service user {}", username);
-                UserUtil.createServiceUser(session, username);
+                UserUtil.createServiceUser(session, username, s.getPath());
             } else if (UserUtil.isServiceUser(session, username)) {
                 log.info("Service user {} already exists, no changes made.", username);
             } else {
@@ -81,7 +81,7 @@ class UserVisitor extends DoNothingVisitor {
                 } else {
                     log.info("Creating user {}", username);
                 }
-                UserUtil.createUser(session, username, pwd);
+                UserUtil.createUser(session, username, pwd, u.getPath());
             } else {
                 log.info("User {} already exists, no changes made", username);
             }
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/CreateServiceUsersTest.java b/src/test/java/org/apache/sling/jcr/repoinit/CreateServiceUsersTest.java
index fbb641e..fe6c339 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/CreateServiceUsersTest.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/CreateServiceUsersTest.java
@@ -95,4 +95,14 @@ public class CreateServiceUsersTest {
             U.assertServiceUser("after deleting users", user(i), false);
         }
     }
+
+    @Test
+    public void createServiceUserWithPathTest() throws Exception {
+        final String userId = namePrefix + "_cdst";
+        // Oak requires system/ prefix for service users
+        final String path = "system/forServiceUser/test";
+        U.assertServiceUser("at start of test", userId, false);
+        U.parseAndExecute("create service user " + userId + " with path " + path);
+        U.assertServiceUser("after creating user", userId, true, path);
+    }
 }
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/CreateUsersTest.java b/src/test/java/org/apache/sling/jcr/repoinit/CreateUsersTest.java
index 6574372..e23449f 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/CreateUsersTest.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/CreateUsersTest.java
@@ -63,6 +63,22 @@ public class CreateUsersTest {
         U.assertUser("after deleting user", userId, false);
     }
 
+    @Test
+    public void createUserWithPathTest() throws Exception {
+        final String userId = namePrefix + "_cuwpt";
+        final String path = "testusers/folder_for_" + userId;
+        U.parseAndExecute("create user " + userId + " with path " + path);
+        U.assertUser("after creating user " + userId, userId, true, path);
+    }
+
+    @Test
+    public void createUserWithPathAndPasswordTest() throws Exception {
+        final String userId = namePrefix + "_cuwpt";
+        final String path = "testuserwithpassword/folder_for_" + userId;
+        U.parseAndExecute("create user " + userId + " with path " + path + " with password asdf");
+        U.assertUser("after creating user " + userId, userId, true, path);
+    }
+
     private String user(int index) {
         return namePrefix + "_" + index;
     }
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/impl/ManyServiceUsersTest.java b/src/test/java/org/apache/sling/jcr/repoinit/impl/ManyServiceUsersTest.java
index b1970c4..aabbe4b 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/impl/ManyServiceUsersTest.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/impl/ManyServiceUsersTest.java
@@ -76,7 +76,7 @@ public class ManyServiceUsersTest {
             otherSession = s.getRepository().login(new SimpleCredentials("admin", "admin".toCharArray()));
             for(int i=1; i < N_USERS; i++) {
                 final String username = getClass().getSimpleName() + "_" + uniqueId + "_" + i;
-                UserUtil.createServiceUser(s, username);
+                UserUtil.createServiceUser(s, username, null);
                 U.assertServiceUser("Right after creation at index " + i, username, true);
                 
                 // Required for setAcl to work
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java b/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java
index 6ba9c2e..8da7c72 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java
@@ -64,7 +64,21 @@ public class TestUtil {
         }
     }
 
+    private void assertPathContains(User u, String pathShouldContain) throws RepositoryException {
+        if(pathShouldContain != null) {
+            final String path = u.getPath();
+            assertTrue(
+                    "Expecting path " + path + " to contain " + pathShouldContain,
+                    path.contains(pathShouldContain)
+            );
+        }
+    }
+
     public void assertUser(String info, String id, boolean expectToExist) throws RepositoryException {
+        assertUser(info, id, expectToExist, null);
+    }
+
+    public void assertUser(String info, String id, boolean expectToExist, String pathShouldContain) throws RepositoryException {
         final Authorizable a = UserUtil.getUserManager(adminSession).getAuthorizable(id);
         if(!expectToExist) {
             assertNull(info + ", expecting Principal to be absent:" + id, a);
@@ -72,10 +86,15 @@ public class TestUtil {
             assertNotNull(info + ", expecting Principal to exist:" + id, a);
             final User u = (User)a;
             assertFalse(info + ", expecting Principal to be a plain user:" + id, u.isSystemUser());
+            assertPathContains(u, pathShouldContain);
         }
     }
 
     public void assertServiceUser(String info, String id, boolean expectToExist) throws RepositoryException {
+        assertServiceUser(info, id, expectToExist, null);
+    }
+
+    public void assertServiceUser(String info, String id, boolean expectToExist, String pathShouldContain) throws RepositoryException {
         final Authorizable a = UserUtil.getUserManager(adminSession).getAuthorizable(id);
         if(!expectToExist) {
             assertNull(info + ", expecting Principal to be absent:" + id, a);
@@ -83,6 +102,7 @@ public class TestUtil {
             assertNotNull(info + ", expecting Principal to exist:" + id, a);
             final User u = (User)a;
             assertTrue(info + ", expecting Principal to be a System user:" + id, u.isSystemUser());
+            assertPathContains(u, pathShouldContain);
         }
     }
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@sling.apache.org" <co...@sling.apache.org>'].