jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r1532842 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/ oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/
Date Wed, 16 Oct 2013 18:21:58 GMT
Author: angela
Date: Wed Oct 16 18:21:58 2013
New Revision: 1532842

URL: http://svn.apache.org/r1532842
Log:
OAK-1098 : AuthorizableImpl methods should convert path to Oak path (WIP)

Added:
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/RemappingTest.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizablePropertiesImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java?rev=1532842&r1=1532841&r2=1532842&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizableImpl.java
Wed Oct 16 18:21:58 2013
@@ -230,7 +230,7 @@ abstract class AuthorizableImpl implemen
      */
     private AuthorizableProperties getAuthorizableProperties() {
         if (properties == null) {
-            properties = new AuthorizablePropertiesImpl(this);
+            properties = new AuthorizablePropertiesImpl(this, userManager.getNamePathMapper());
         }
         return properties;
     }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizablePropertiesImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizablePropertiesImpl.java?rev=1532842&r1=1532841&r2=1532842&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizablePropertiesImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/AuthorizablePropertiesImpl.java
Wed Oct 16 18:21:58 2013
@@ -54,25 +54,26 @@ class AuthorizablePropertiesImpl impleme
     private static final Logger log = LoggerFactory.getLogger(AuthorizablePropertiesImpl.class);
 
     private final AuthorizableImpl authorizable;
+    private final NamePathMapper namePathMapper;
 
-    AuthorizablePropertiesImpl(AuthorizableImpl authorizable) {
+    AuthorizablePropertiesImpl(AuthorizableImpl authorizable, NamePathMapper namePathMapper)
{
         this.authorizable = authorizable;
+        this.namePathMapper = namePathMapper;
     }
 
     //---------------------------------------------< AuthorizableProperties >---
     @Override
     public Iterator<String> getNames(String relPath) throws RepositoryException {
-        checkRelativePath(relPath);
-
+        String oakPath = getOakPath(relPath);
         Tree tree = getTree();
-        TreeLocation location = getLocation(tree, relPath);
+        TreeLocation location = getLocation(tree, oakPath);
         Tree parent = location.getTree();
         if (parent != null && Text.isDescendantOrEqual(tree.getPath(), parent.getPath()))
{
             List<String> l = new ArrayList<String>();
             for (PropertyState property : parent.getProperties()) {
                 String propName = property.getName();
                 if (isAuthorizableProperty(tree, location.getChild(propName), false)) {
-                    l.add(propName);
+                    l.add(namePathMapper.getJcrName(propName));
                 }
             }
             return l.iterator();
@@ -86,9 +87,8 @@ class AuthorizablePropertiesImpl impleme
      */
     @Override
     public boolean hasProperty(String relPath) throws RepositoryException {
-        checkRelativePath(relPath);
-
-        return isAuthorizableProperty(getTree(), getLocation(getTree(), relPath), true);
+        String oakPath = getOakPath(relPath);
+        return isAuthorizableProperty(getTree(), getLocation(getTree(), oakPath), true);
     }
 
     /**
@@ -96,18 +96,16 @@ class AuthorizablePropertiesImpl impleme
      */
     @Override
     public Value[] getProperty(String relPath) throws RepositoryException {
-        checkRelativePath(relPath);
-
+        String oakPath = getOakPath(relPath);
         Tree tree = getTree();
         Value[] values = null;
-        PropertyState property = getAuthorizableProperty(tree, getLocation(tree, relPath),
true);
+        PropertyState property = getAuthorizableProperty(tree, getLocation(tree, oakPath),
true);
         if (property != null) {
-            NamePathMapper npMapper = authorizable.getUserManager().getNamePathMapper();
             if (property.isArray()) {
-                List<Value> vs = ValueFactoryImpl.createValues(property, npMapper);
+                List<Value> vs = ValueFactoryImpl.createValues(property, namePathMapper);
                 values = vs.toArray(new Value[vs.size()]);
             } else {
-                values = new Value[]{ValueFactoryImpl.createValue(property, npMapper)};
+                values = new Value[]{ValueFactoryImpl.createValue(property, namePathMapper)};
             }
         }
         return values;
@@ -121,13 +119,11 @@ class AuthorizablePropertiesImpl impleme
         if (value == null) {
             removeProperty(relPath);
         } else {
-            checkRelativePath(relPath);
+            String oakPath = getOakPath(relPath);
+            String name = Text.getName(oakPath);
+            PropertyState propertyState = PropertyStates.createProperty(name, value);
 
-            String name = Text.getName(relPath);
-            PropertyState propertyState =
-                    PropertyStates.createProperty(name, value);
-
-            String intermediate = (relPath.equals(name)) ? null : Text.getRelativeParent(relPath,
1);
+            String intermediate = (oakPath.equals(name)) ? null : Text.getRelativeParent(oakPath,
1);
             Tree parent = getOrCreateTargetTree(intermediate);
             checkProtectedProperty(parent, propertyState);
 
@@ -143,13 +139,12 @@ class AuthorizablePropertiesImpl impleme
         if (values == null) {
             removeProperty(relPath);
         } else {
-            checkRelativePath(relPath);
-
-            String name = Text.getName(relPath);
+            String oakPath = getOakPath(relPath);
+            String name = Text.getName(oakPath);
             PropertyState propertyState =
                     PropertyStates.createProperty(name, Arrays.asList(values));
 
-            String intermediate = (relPath.equals(name)) ? null : Text.getRelativeParent(relPath,
1);
+            String intermediate = (oakPath.equals(name)) ? null : Text.getRelativeParent(oakPath,
1);
             Tree parent = getOrCreateTargetTree(intermediate);
             checkProtectedProperty(parent, propertyState);
 
@@ -162,10 +157,9 @@ class AuthorizablePropertiesImpl impleme
      */
     @Override
     public boolean removeProperty(String relPath) throws RepositoryException {
-        checkRelativePath(relPath);
-
+        String oakPath = getOakPath(relPath);
         Tree node = getTree();
-        TreeLocation propertyLocation = getLocation(node, relPath);
+        TreeLocation propertyLocation = getLocation(node, oakPath);
         if (propertyLocation.getProperty() != null) {
             if (isAuthorizableProperty(node, propertyLocation, true)) {
                 return propertyLocation.remove();
@@ -304,9 +298,14 @@ class AuthorizablePropertiesImpl impleme
         return loc;
     }
 
-    private static void checkRelativePath(String relativePath) throws RepositoryException
{
-        if (relativePath == null || relativePath.isEmpty() || relativePath.charAt(0) == '/')
{
-            throw new RepositoryException("Relative path expected. Found " + relativePath);
+    private String getOakPath(String relPath) throws RepositoryException {
+        if (relPath == null || relPath.isEmpty() || relPath.charAt(0) == '/') {
+            throw new RepositoryException("Relative path expected. Found " + relPath);
+        }
+        String oakPath = namePathMapper.getOakPathKeepIndex(relPath);
+        if (oakPath == null) {
+            throw new RepositoryException("Failed to resolve relative path: " + relPath);
         }
+        return oakPath;
     }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java?rev=1532842&r1=1532841&r2=1532842&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImpl.java
Wed Oct 16 18:21:58 2013
@@ -25,6 +25,7 @@ import org.apache.jackrabbit.api.securit
 import org.apache.jackrabbit.api.security.user.User;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.namepath.NamePathMapper;
 import org.apache.jackrabbit.oak.spi.security.user.AuthorizableType;
 import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
 import org.apache.jackrabbit.oak.spi.security.user.util.PasswordUtil;
@@ -64,10 +65,11 @@ class UserImpl extends AuthorizableImpl 
     public Principal getPrincipal() throws RepositoryException {
         Tree userTree = getTree();
         String principalName = getPrincipalName();
+        NamePathMapper npMapper = getUserManager().getNamePathMapper();
         if (isAdmin()) {
-            return new AdminPrincipalImpl(principalName, userTree, getUserManager().getNamePathMapper());
+            return new AdminPrincipalImpl(principalName, userTree, npMapper);
         } else {
-            return new TreeBasedPrincipal(principalName, userTree, getUserManager().getNamePathMapper());
+            return new TreeBasedPrincipal(principalName, userTree, npMapper);
         }
     }
 

Added: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/RemappingTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/RemappingTest.java?rev=1532842&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/RemappingTest.java
(added)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/security/user/RemappingTest.java
Wed Oct 16 18:21:58 2013
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.jcr.security.user;
+
+import java.util.Iterator;
+import java.util.List;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.oak.plugins.name.NamespaceConstants;
+import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
+import org.junit.Test;
+
+/**
+ * Tests to assert that all user mgt methods that include name/path conversion
+ * from JCR to OAK and back are properly implemented.
+ */
+public class RemappingTest extends AbstractUserTest {
+
+    private Session session;
+    private Authorizable authorizable;
+
+    private List<String> unmappedPaths = ImmutableList.of("uTest:property", "uTest:node/uTest:property2");
+    private List<String> mappedPaths = ImmutableList.of("my:property", "my:node/my:property2");
+    private Value value;
+    private Value value2;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        superuser.getWorkspace().getNamespaceRegistry().registerNamespace("uTest", "http://jackrabbit-oak.apache.org");
+        value = superuser.getValueFactory().createValue("uTest:value", PropertyType.NAME);
+        value2 = superuser.getValueFactory().createValue("uTest:value2", PropertyType.NAME);
+        for (String relPath : unmappedPaths) {
+            user.setProperty(relPath, value);
+        }
+        superuser.save();
+
+        session = getHelper().getSuperuserSession();
+        session.setNamespacePrefix("my", "http://jackrabbit-oak.apache.org");
+        session.setNamespacePrefix("myRep", NamespaceConstants.NAMESPACE_REP);
+
+        authorizable = getUserManager(session).getAuthorizable(user.getID());
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        session.logout();
+        super.tearDown();
+    }
+
+    @Test
+    public void testGetAuthorizablePath() throws Exception {
+        assertTrue(user.getPath().startsWith(UserConstants.DEFAULT_USER_PATH));
+
+        String path = authorizable.getPath();
+        assertFalse(path.startsWith(UserConstants.DEFAULT_USER_PATH));
+    }
+
+    @Test
+    public void testGetAuthorizableByPath() throws Exception {
+        assertNotNull(getUserManager(session).getAuthorizableByPath(authorizable.getPath()));
+    }
+
+    @Test
+    public void testGetAuthorizableByPath2() throws Exception {
+        try {
+            getUserManager(session).getAuthorizableByPath(user.getPath());
+            fail("invalid path must be detected");
+        } catch (RepositoryException e) {
+            // success
+        }
+    }
+
+    @Test
+    public void testFindAuthorizable() throws Exception {
+        // TODO
+    }
+
+    @Test
+    public void testGetPropertyNames() throws Exception {
+        Iterator it = authorizable.getPropertyNames();
+        assertTrue(it.hasNext());
+        assertEquals("my:property", it.next());
+        assertFalse(it.hasNext());
+    }
+
+    @Test
+    public void testGetPropertyNames2() throws Exception {
+        Iterator it = authorizable.getPropertyNames("my:node");
+        assertTrue(it.hasNext());
+        assertEquals("my:property2", it.next());
+        assertFalse(it.hasNext());
+    }
+
+    @Test
+    public void testGetPropertyNames3() throws Exception {
+        try {
+            Iterator it = authorizable.getPropertyNames("uTest:node");
+            fail();
+        } catch (RepositoryException e) {
+            // success
+        }
+    }
+
+    @Test
+    public void testHasProperty() throws Exception {
+        for (String relPath : mappedPaths) {
+            assertTrue(authorizable.hasProperty(relPath));
+        }
+    }
+
+    @Test
+    public void testHasProperty2() throws Exception {
+        for (String relPath : unmappedPaths) {
+            try {
+                authorizable.hasProperty(relPath);
+                fail();
+            } catch (RepositoryException e) {
+                // success
+            }
+        }
+    }
+
+    @Test
+    public void testGetProperty() throws Exception {
+        for (String relPath : mappedPaths) {
+            Value[] values = authorizable.getProperty(relPath);
+            assertNotNull(values);
+            assertEquals(1, values.length);
+            assertEquals("my:value", values[0].getString());
+        }
+    }
+
+    @Test
+    public void testGetProperty2() throws Exception {
+        for (String relPath : unmappedPaths) {
+            try {
+                authorizable.getProperty(relPath);
+                fail();
+            } catch (RepositoryException e) {
+                // success
+            }
+        }
+    }
+
+    @Test
+    public void testSetProperty() throws Exception {
+        for (String relPath : mappedPaths) {
+            authorizable.setProperty(relPath, value2);
+            authorizable.setProperty(relPath, new Value[] {value2});
+        }
+    }
+
+    @Test
+    public void testSetProperty2() throws Exception {
+        for (String relPath : unmappedPaths) {
+            try {
+                authorizable.setProperty(relPath, value2);
+                fail();
+            } catch (RepositoryException e) {
+                // success
+            }
+
+            try {
+                authorizable.setProperty(relPath, new Value[] {value2});
+                fail();
+            } catch (RepositoryException e) {
+                // success
+            }
+        }
+    }
+
+    @Test
+    public void testRemoveProperty() throws Exception {
+        for (String relPath : mappedPaths) {
+            authorizable.removeProperty(relPath);
+        }
+    }
+
+    @Test
+    public void testRemoveProperty2() throws Exception {
+        for (String relPath : unmappedPaths) {
+            try {
+                authorizable.removeProperty(relPath);
+                fail();
+            } catch (RepositoryException e) {
+                // success
+            }
+        }
+    }
+}
\ No newline at end of file



Mime
View raw message