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

cziegeler pushed a commit to branch SLING-9622
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-auth-core.git


The following commit(s) were added to refs/heads/SLING-9622 by this push:
     new 7d6ac7a  SLING-9622 : Handle mapping within auth.core bundle and 
update auth requirements when mapping changes
7d6ac7a is described below

commit 7d6ac7afa578721fb4ca6985d4024a45de3ccf64
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Tue Aug 4 18:11:51 2020 +0200

    SLING-9622 : Handle mapping within auth.core bundle and update auth 
requirements when mapping changes
---
 .../impl/SlingAuthenticatorServiceListener.java    | 16 ++++-----------
 .../SlingAuthenticatorServiceListenerTest.java     | 24 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java
 
b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java
index ef19bb9..6c24a8b 100644
--- 
a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java
+++ 
b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListener.java
@@ -19,8 +19,6 @@
 package org.apache.sling.auth.core.impl;
 
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.Dictionary;
 import java.util.HashSet;
 import java.util.Hashtable;
@@ -232,7 +230,7 @@ public class SlingAuthenticatorServiceListener implements 
AllServiceListener, Ev
             case MODIFIED : this.modifiedService(mapper, action.reference); 
break;
             case UPDATE: final List<AuthenticationRequirementHolder> list = 
props.get(id);
                          if (!list.isEmpty() ) {
-                             this.modifiedService(mapper, 
list.get(0).serviceReference, regProps.get(id));
+                             this.modifiedService(mapper, 
list.get(0).serviceReference);
                          }
         }
     }
@@ -256,7 +254,7 @@ public class SlingAuthenticatorServiceListener implements 
AllServiceListener, Ev
         }
     }
 
-    private Set<String> buildPathsSet(final ResourceMapper mapper, final 
Collection<String> authReqPaths) {
+    private Set<String> buildPathsSet(final ResourceMapper mapper, final 
String[] authReqPaths) {
         final Set<String> paths = new HashSet<>();
         for(final String authReq : authReqPaths) {
             if (authReq != null && authReq.length() > 0) {
@@ -280,7 +278,7 @@ public class SlingAuthenticatorServiceListener implements 
AllServiceListener, Ev
         final String[] authReqPaths = 
PropertiesUtil.toStringArray(ref.getProperty(AuthConstants.AUTH_REQUIREMENTS));
         if ( authReqPaths != null ) {
             final Long id = (Long)ref.getProperty(Constants.SERVICE_ID);
-            final Set<String> paths = buildPathsSet(mapper, 
Arrays.asList(authReqPaths));
+            final Set<String> paths = buildPathsSet(mapper, authReqPaths);
 
             if ( !paths.isEmpty() ) {
                 final List<AuthenticationRequirementHolder> authReqList = new 
ArrayList<AuthenticationRequirementHolder>();
@@ -288,7 +286,7 @@ public class SlingAuthenticatorServiceListener implements 
AllServiceListener, Ev
                     
authReqList.add(AuthenticationRequirementHolder.fromConfig(authReq, ref));
                 }
 
-                // keep original set for modified
+                // keep original
                 regProps.put(id, paths);
                 registerService(authReqList);
                 props.put(id, authReqList);
@@ -302,12 +300,6 @@ public class SlingAuthenticatorServiceListener implements 
AllServiceListener, Ev
      */
     private void modifiedService(final ResourceMapper mapper, final 
ServiceReference<?> ref) {
         final String[] authReqPaths = 
PropertiesUtil.toStringArray(ref.getProperty(AuthConstants.AUTH_REQUIREMENTS));
-
-        modifiedService(mapper, ref, Arrays.asList(authReqPaths));
-    }
-
-
-    private void modifiedService(final ResourceMapper mapper, final 
ServiceReference<?> ref, final Collection<String> authReqPaths) {
         final Long id = (Long)ref.getProperty(Constants.SERVICE_ID);
         if ( authReqPaths != null ) {
             final Set<String> oldPaths = regProps.get(id);
diff --git 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java
 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java
index a5724c3..065f613 100644
--- 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java
+++ 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorServiceListenerTest.java
@@ -223,4 +223,28 @@ public class SlingAuthenticatorServiceListenerTest {
         assertTrue(cache.getHolders().isEmpty());
     }
 
+    @Test public void testRegistrationAndUpdatingMapping() throws 
LoginException {
+        final PathBasedHolderCache<AuthenticationRequirementHolder> cache = 
new PathBasedHolderCache<AuthenticationRequirementHolder>();
+        final BundleContext context = mock(BundleContext.class);
+        final ResourceMapper mapper = mock(ResourceMapper.class);
+        
when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", 
"/path2", "/path3"));
+        final SlingAuthenticatorServiceListener listener = 
SlingAuthenticatorServiceListener.createListener(context, callable -> 
callable.run(), createFactoryForMapper(mapper), cache);
+
+        final ServiceReference<?> ref = createServiceReference(new String[] 
{"/path1"});
+        listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, 
ref));
+
+        assertPaths(cache, new String[] {"/path1", "/path2", "/path3"},
+                           new ServiceReference<?>[] {ref, ref, ref});
+
+        // update mapper
+        
when(mapper.getAllMappings("/path1")).thenReturn(Arrays.asList("/path1", 
"/path5"));
+        listener.handleEvent(null);
+
+        assertPaths(cache, new String[] {"/path1", "/path5"},
+                new ServiceReference<?>[] {ref, ref});
+
+        listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, 
ref));
+
+        assertTrue(cache.getHolders().isEmpty());
+    }
 }

Reply via email to