You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/11/03 19:15:17 UTC

[sling-org-apache-sling-resourceaccesssecurity] branch master updated: SLING-10905 support multivalue properties (#3)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 93aca1c  SLING-10905 support multivalue properties (#3)
93aca1c is described below

commit 93aca1c47c4679736ae72ae44146460e9db0f8bc
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed Nov 3 20:15:14 2021 +0100

    SLING-10905 support multivalue properties (#3)
---
 .../impl/ResourceAccessGateHandler.java            | 29 ++++----
 .../impl/ResourceAccessGateHandlerTest.java        | 80 ++++++++++++++++++++++
 2 files changed, 94 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourceaccesssecurity/impl/ResourceAccessGateHandler.java b/src/main/java/org/apache/sling/resourceaccesssecurity/impl/ResourceAccessGateHandler.java
index 0d849d1..1dbea1a 100644
--- a/src/main/java/org/apache/sling/resourceaccesssecurity/impl/ResourceAccessGateHandler.java
+++ b/src/main/java/org/apache/sling/resourceaccesssecurity/impl/ResourceAccessGateHandler.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.resourceaccesssecurity.impl;
 
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.regex.Matcher;
@@ -25,22 +26,23 @@ import java.util.regex.Pattern;
 
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.resourceaccesssecurity.ResourceAccessGate;
+import org.jetbrains.annotations.NotNull;
 import org.osgi.framework.ServiceReference;
 
 public class ResourceAccessGateHandler {
 
-    private final ResourceAccessGate resourceAccessGate;
+    private final @NotNull ResourceAccessGate resourceAccessGate;
 
-    private final ServiceReference<ResourceAccessGate> reference;
+    private final @NotNull ServiceReference<ResourceAccessGate> reference;
 
-    private final Pattern pathPattern;
+    private final @NotNull Pattern pathPattern;
     private final Set<ResourceAccessGate.Operation> operations = new HashSet<>();
     private final Set<ResourceAccessGate.Operation> finalOperations = new HashSet<>();
 
     /**
      * constructor
      */
-    public ResourceAccessGateHandler ( final ServiceReference<ResourceAccessGate> resourceAccessGateRef, final ResourceAccessGate resourceAccessGate ) {
+    public ResourceAccessGateHandler( @NotNull final ServiceReference<ResourceAccessGate> resourceAccessGateRef, @NotNull final ResourceAccessGate resourceAccessGate ) {
         this.reference = resourceAccessGateRef;
         this.resourceAccessGate = resourceAccessGate;
         // extract the service property "path"
@@ -52,8 +54,8 @@ public class ResourceAccessGateHandler {
         }
 
         // extract the service property "operations"
-        final String ops[] = PropertiesUtil.toStringArray( resourceAccessGateRef.getProperty(ResourceAccessGate.OPERATIONS) );
-        if ( ops != null && ops.length > 0 ) {
+        final String[] ops = PropertiesUtil.toStringArray( resourceAccessGateRef.getProperty(ResourceAccessGate.OPERATIONS) );
+        if ( ops != null && ops.length > 0) {
             for (final String opAsString : ops) {
                 final ResourceAccessGate.Operation operation = ResourceAccessGate.Operation.fromString(opAsString);
                 if ( operation != null ) {
@@ -61,16 +63,13 @@ public class ResourceAccessGateHandler {
                 }
             }
         } else {
-           for (final ResourceAccessGate.Operation op : ResourceAccessGate.Operation.values() ) {
-               operations.add(op);
-           }
+           Collections.addAll(operations, ResourceAccessGate.Operation.values());
         }
 
         // extract the service property "finaloperations"
-        final String finalOps = PropertiesUtil.toString(resourceAccessGateRef.getProperty(ResourceAccessGate.FINALOPERATIONS), null );
-        if ( finalOps != null && finalOps.length() > 0 ) {
-            final String[] finOpsArray = finalOps.split( "," );
-            for (final String opAsString : finOpsArray) {
+        final String[] finalOps = PropertiesUtil.toStringArray( resourceAccessGateRef.getProperty(ResourceAccessGate.FINALOPERATIONS) );
+        if ( finalOps != null ) {
+            for (final String opAsString : finalOps) {
                 final ResourceAccessGate.Operation operation = ResourceAccessGate.Operation.fromString(opAsString);
                 if ( operation != null ) {
                     finalOperations.add(operation);
@@ -80,7 +79,7 @@ public class ResourceAccessGateHandler {
 
     }
 
-    public boolean matches ( final String path, final ResourceAccessGate.Operation operation ) {
+    public boolean matches( final String path, final ResourceAccessGate.Operation operation ) {
         boolean returnValue = false;
 
         if ( operations.contains( operation ) ) {
@@ -101,7 +100,7 @@ public class ResourceAccessGateHandler {
         return finalOperations.contains(operation);
     }
 
-    public ResourceAccessGate getResourceAccessGate () {
+    public @NotNull ResourceAccessGate getResourceAccessGate() {
         return resourceAccessGate;
     }
 
diff --git a/src/test/java/org/apache/sling/resourceaccesssecurity/impl/ResourceAccessGateHandlerTest.java b/src/test/java/org/apache/sling/resourceaccesssecurity/impl/ResourceAccessGateHandlerTest.java
new file mode 100644
index 0000000..1f5e8e6
--- /dev/null
+++ b/src/test/java/org/apache/sling/resourceaccesssecurity/impl/ResourceAccessGateHandlerTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.sling.resourceaccesssecurity.impl;
+
+import org.apache.sling.resourceaccesssecurity.ResourceAccessGate;
+import org.apache.sling.resourceaccesssecurity.ResourceAccessGate.Operation;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.osgi.framework.ServiceReference;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ResourceAccessGateHandlerTest {
+    @Mock
+    ResourceAccessGate gate;
+
+    @Mock
+    ServiceReference<ResourceAccessGate> gateRef;
+
+    @Test
+    public void testSingleValueProperties() {
+        Mockito.when(gateRef.getProperty(ResourceAccessGate.PATH)).thenReturn("/content");
+        Mockito.when(gateRef.getProperty(ResourceAccessGate.OPERATIONS)).thenReturn("read");
+        Mockito.when(gateRef.getProperty(ResourceAccessGate.FINALOPERATIONS)).thenReturn("read");
+        ResourceAccessGateHandler gateHandler = new ResourceAccessGateHandler(gateRef, gate);
+        Assert.assertTrue(gateHandler.matches("/content", Operation.READ));
+        Assert.assertFalse(gateHandler.matches("/content", Operation.EXECUTE));
+        Assert.assertTrue(gateHandler.isFinalOperation(Operation.READ));
+        Assert.assertFalse(gateHandler.isFinalOperation(Operation.EXECUTE));
+    }
+    
+    @Test
+    public void testMultiValueProperties() {
+        Mockito.when(gateRef.getProperty(ResourceAccessGate.PATH)).thenReturn("/content");
+        Mockito.when(gateRef.getProperty(ResourceAccessGate.OPERATIONS)).thenReturn(new String[]{"read", "update", "invalid"});
+        Mockito.when(gateRef.getProperty(ResourceAccessGate.FINALOPERATIONS)).thenReturn(new String[]{"read", "update", "invalid"});
+        ResourceAccessGateHandler gateHandler = new ResourceAccessGateHandler(gateRef, gate);
+        Assert.assertTrue(gateHandler.matches("/content", Operation.READ));
+        Assert.assertFalse(gateHandler.matches("/othercontent", Operation.READ));
+        Assert.assertTrue(gateHandler.matches("/content", Operation.UPDATE));
+        Assert.assertFalse(gateHandler.matches("/content", Operation.EXECUTE));
+        Assert.assertTrue(gateHandler.isFinalOperation(Operation.READ));
+        Assert.assertTrue(gateHandler.isFinalOperation(Operation.UPDATE));
+    }
+
+    @Test
+    public void testDefaultOperationsAndPath() {
+        Mockito.when(gateRef.getProperty(ResourceAccessGate.OPERATIONS)).thenReturn(new String[] {});
+        ResourceAccessGateHandler gateHandler = new ResourceAccessGateHandler(gateRef, gate);
+        Assert.assertTrue(gateHandler.matches("/content1", Operation.READ));
+        Assert.assertTrue(gateHandler.matches(null, Operation.READ));
+        Assert.assertTrue(gateHandler.matches("/content2", Operation.CREATE));
+        Assert.assertTrue(gateHandler.matches(null, Operation.CREATE));
+        Assert.assertTrue(gateHandler.matches("/content3", Operation.EXECUTE));
+        Assert.assertTrue(gateHandler.matches(null, Operation.EXECUTE));
+        Assert.assertTrue(gateHandler.matches("/content4", Operation.ORDER_CHILDREN));
+        Assert.assertTrue(gateHandler.matches(null, Operation.ORDER_CHILDREN));
+        Assert.assertTrue(gateHandler.matches("/content5", Operation.UPDATE));
+        Assert.assertTrue(gateHandler.matches(null, Operation.UPDATE));
+    }
+}