You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/02/28 07:35:36 UTC

[isis] branch master updated: ISIS-2534: simplify AuthorizorShiro impl., as spec-loader is no longer required to infer logical type names

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

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new cd59c25  ISIS-2534: simplify AuthorizorShiro impl., as spec-loader is no longer required to infer logical type names
cd59c25 is described below

commit cd59c25c960cec71b4af9377f2d396d70117ceba
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sun Feb 28 08:35:20 2021 +0100

    ISIS-2534: simplify AuthorizorShiro impl., as spec-loader is no longer
    required to infer logical type names
    
    also moving logical type name formatting logic to TypeIdentifier
---
 .../apache/isis/applib/id/FeatureIdentifier.java   | 39 ++++++++++++++----
 .../org/apache/isis/applib/id/TypeIdentifier.java  | 33 ++++++++++++++-
 .../services/wrapper/events/InteractionEvent.java  |  2 +-
 .../shiro/authorization/AuthorizorShiro.java       | 47 ++--------------------
 .../security/shiro/TypeIdentifierTestFactory.java  |  6 +--
 security/shiro/src/test/resources/shiro.ini        |  6 +--
 6 files changed, 74 insertions(+), 59 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/isis/applib/id/FeatureIdentifier.java b/api/applib/src/main/java/org/apache/isis/applib/id/FeatureIdentifier.java
index 99d3a4b..691f8d1 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/id/FeatureIdentifier.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/id/FeatureIdentifier.java
@@ -27,9 +27,19 @@ import org.apache.isis.commons.collections.Can;
 import org.apache.isis.commons.internal.base._Strings;
 
 import lombok.Getter;
+import lombok.NonNull;
+import lombok.val;
 
 /**
- * @since 1.x revised for 2.0 {@index}
+ * Combines {@link TypeIdentifier} and member identification (from properties, collections or actions),
+ * to a fully qualified <i>feature</i> identifier.
+ * <p> 
+ * For {@link FeatureIdentifier}(s) of type {@link FeatureIdentifier.Type#CLASS} member information is 
+ * left empty.   
+ *  
+ * @apiNote Revised from former {@code Identifier (v1.x)}.
+ * @since 2.0 {@index}
+ * @see TypeIdentifier
  */
 public class FeatureIdentifier implements Comparable<FeatureIdentifier> {
 
@@ -52,7 +62,8 @@ public class FeatureIdentifier implements Comparable<FeatureIdentifier> {
     public static FeatureIdentifier propertyOrCollectionIdentifier(
             final TypeIdentifier typeIdentifier,
             final String propertyOrCollectionName) {
-        return new FeatureIdentifier(typeIdentifier, propertyOrCollectionName, Can.empty(), Type.PROPERTY_OR_COLLECTION);
+        return new FeatureIdentifier(typeIdentifier, propertyOrCollectionName, Can.empty(), 
+                Type.PROPERTY_OR_COLLECTION);
     }
 
     public static FeatureIdentifier actionIdentifier(
@@ -124,11 +135,19 @@ public class FeatureIdentifier implements Comparable<FeatureIdentifier> {
                 : className + "#" + memberNameAndParameterClassNamesIdentityString;
     }
 
+    // -- LOGICAL ID
+    
+    public String getLogicalIdentityString(final @NonNull String delimiter) {
+        return typeIdentifier.getLogicalTypeName() 
+                + delimiter 
+                + memberNameAndParameterClassNamesIdentityString;
+    }
+    
     // -- NATURAL NAMES
     
     public String getClassNaturalName() {
-        final String className = getClassName();
-        final String isolatedName = className.substring(className.lastIndexOf('.') + 1);
+        val className = getClassName();
+        val isolatedName = className.substring(className.lastIndexOf('.') + 1);
         return naturalName(isolatedName);
     }
 
@@ -136,7 +155,7 @@ public class FeatureIdentifier implements Comparable<FeatureIdentifier> {
         return naturalName(memberName);
     }
 
-    public Can<String> getMemberParameterNaturalNames() {
+    public Can<String> getMemberParameterClassNaturalNames() {
         return naturalNames(memberParameterClassNames);
     }
 
@@ -210,13 +229,17 @@ public class FeatureIdentifier implements Comparable<FeatureIdentifier> {
             nextCharacter = name.charAt(pos);
 
             if (previousCharacter != SPACE) {
-                if (Character.isUpperCase(character) && !Character.isUpperCase(previousCharacter)) {
+                if (Character.isUpperCase(character) 
+                        && !Character.isUpperCase(previousCharacter)) {
                     naturalName.append(SPACE);
                 }
-                if (Character.isUpperCase(character) && Character.isLowerCase(nextCharacter) && Character.isUpperCase(previousCharacter)) {
+                if (Character.isUpperCase(character) 
+                        && Character.isLowerCase(nextCharacter) 
+                        && Character.isUpperCase(previousCharacter)) {
                     naturalName.append(SPACE);
                 }
-                if (Character.isDigit(character) && !Character.isDigit(previousCharacter)) {
+                if (Character.isDigit(character) 
+                        && !Character.isDigit(previousCharacter)) {
                     naturalName.append(SPACE);
                 }
             }
diff --git a/api/applib/src/main/java/org/apache/isis/applib/id/TypeIdentifier.java b/api/applib/src/main/java/org/apache/isis/applib/id/TypeIdentifier.java
index 020f36e..e221081 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/id/TypeIdentifier.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/id/TypeIdentifier.java
@@ -51,6 +51,10 @@ public final class TypeIdentifier implements Comparable<TypeIdentifier> {
 
     // -- FACTORIES
     
+    /**
+     * Returns a new TypeIdentifier based on the corresponding class
+     * and a {@code logicalNameProvider} for lazy logical name lookup.  
+     */
     public static TypeIdentifier lazy(
             final @NonNull Class<?> correspondingClass, 
             final @NonNull Supplier<String> logicalNameProvider) {
@@ -58,6 +62,10 @@ public final class TypeIdentifier implements Comparable<TypeIdentifier> {
         return new TypeIdentifier(correspondingClass, logicalNameProvider);
     }
     
+    /**
+     * Returns a new TypeIdentifier based on the corresponding class
+     * and (ahead of time) known {@code logicalName}. 
+     */
     public static TypeIdentifier eager(
             final @NonNull Class<?> correspondingClass, 
             final String logicalName) {
@@ -66,7 +74,8 @@ public final class TypeIdentifier implements Comparable<TypeIdentifier> {
     }
     
     /**
-     * Use the corresponding Class's fully qualified name for the logicalName. 
+     * Use the corresponding class's fully qualified name for the {@code logicalName}. 
+     * Most likely used in testing scenarios.
      */
     public static TypeIdentifier fqcn(
             final @NonNull Class<?> correspondingClass) {
@@ -108,6 +117,28 @@ public final class TypeIdentifier implements Comparable<TypeIdentifier> {
         return logicalName;
     }
     
+    /**
+     * The logical type name consists of 2 parts, the <i>namespace</i> and the <i>logical simple name</i>.
+     * Returns a concatenation of <i>namespace</i>, {@code delimiter} and the <i>logical simple name</i>, 
+     * whereas in the absence of a <i>namespace</i> returns a concatenation of {@code root} and the 
+     * <i>logical simple name</i>.
+     * @param root
+     * @param delimiter
+     */
+    public String getLogicalTypeNameFormatted(
+            final @NonNull String root,
+            final @NonNull String delimiter) {
+        val logicalTypeName = getLogicalTypeName();
+        final int lastDot = logicalTypeName.lastIndexOf('.');
+        if(lastDot > 0) {
+            val namespace = logicalTypeName.substring(0, lastDot);
+            val simpleTypeName = logicalTypeName.substring(lastDot + 1);
+            return namespace + delimiter + simpleTypeName;
+        } else {
+            return root + logicalTypeName;
+        }
+    }
+    
     // -- OBJECT CONTRACT
 
     @Override
diff --git a/api/applib/src/main/java/org/apache/isis/applib/services/wrapper/events/InteractionEvent.java b/api/applib/src/main/java/org/apache/isis/applib/services/wrapper/events/InteractionEvent.java
index 9759399..34861ce 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/services/wrapper/events/InteractionEvent.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/services/wrapper/events/InteractionEvent.java
@@ -113,7 +113,7 @@ public abstract class InteractionEvent extends EventObjectBase<Object> {
      * As per {@link #getMemberParameterNames()}, but naturalized.
      */
     public Can<String> getMemberParameterNaturalNames() {
-        return identifier.getMemberParameterNaturalNames();
+        return identifier.getMemberParameterClassNaturalNames();
     }
 
     /**
diff --git a/security/shiro/src/main/java/org/apache/isis/security/shiro/authorization/AuthorizorShiro.java b/security/shiro/src/main/java/org/apache/isis/security/shiro/authorization/AuthorizorShiro.java
index 262de45..b33b80d 100644
--- a/security/shiro/src/main/java/org/apache/isis/security/shiro/authorization/AuthorizorShiro.java
+++ b/security/shiro/src/main/java/org/apache/isis/security/shiro/authorization/AuthorizorShiro.java
@@ -18,9 +18,6 @@
  */
 package org.apache.isis.security.shiro.authorization;
 
-import java.util.Optional;
-
-import javax.inject.Inject;
 import javax.inject.Named;
 
 import org.apache.shiro.SecurityUtils;
@@ -33,12 +30,7 @@ import org.springframework.stereotype.Service;
 
 import org.apache.isis.applib.annotation.OrderPrecedence;
 import org.apache.isis.applib.id.FeatureIdentifier;
-import org.apache.isis.commons.functional.Result;
 import org.apache.isis.commons.internal.assertions._Assert;
-import org.apache.isis.commons.internal.context._Context;
-import org.apache.isis.core.metamodel.spec.ObjectSpecId;
-import org.apache.isis.core.metamodel.spec.ObjectSpecification;
-import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
 import org.apache.isis.core.security.authentication.Authentication;
 import org.apache.isis.core.security.authentication.standard.Authenticator;
 import org.apache.isis.core.security.authorization.standard.Authorizor;
@@ -51,7 +43,8 @@ import lombok.val;
  * in the role of {@link Authorizor}.
  *
  * <p>
- * However, although there are two objects, they are set up to share the same {@link SecurityManager Shiro SecurityManager}
+ * However, although there are two objects, they are set up to share the same 
+ * {@link SecurityManager Shiro SecurityManager}
  * (bound to a thread-local).
  * </p>
  *
@@ -63,8 +56,6 @@ import lombok.val;
 @Qualifier("Shiro")
 public class AuthorizorShiro implements Authorizor {
 
-    @Inject private SpecificationLoader specificationLoader;
-
     @Override
     public boolean isVisible(final Authentication authentication, final FeatureIdentifier identifier) {
         return isPermitted(authentication.getUserName(), identifier, "r");
@@ -96,18 +87,8 @@ public class AuthorizorShiro implements Authorizor {
     }
 
     private String asPermissionsString(FeatureIdentifier identifier) {
-        String fullyQualifiedLogicalTypeName = asFeatureFqns(identifier);
-        int lastDot = fullyQualifiedLogicalTypeName.lastIndexOf('.');
-        String packageName;
-        String className;
-        if(lastDot > 0) {
-            packageName =fullyQualifiedLogicalTypeName.substring(0, lastDot);
-            className = fullyQualifiedLogicalTypeName.substring(lastDot+1);
-        } else {
-            packageName = "";
-            className = fullyQualifiedLogicalTypeName;
-        }
-        return packageName + ":" + className + ":" + identifier.getMemberName();
+        val logicalTypeName = identifier.getTypeIdentifier().getLogicalTypeNameFormatted(":", ":");
+        return logicalTypeName + ":" + identifier.getMemberName();
     }
 
     // -- DEPS
@@ -120,25 +101,5 @@ public class AuthorizorShiro implements Authorizor {
         return ShiroSecurityContext.getSecurityManager();
     }
 
-    // -- HELPER
-
-    /**
-     * @deprecated while this is technically correct, we should not need to call the SpecificationLoader
-     * on every permission check
-     */
-    private String asFeatureFqns(FeatureIdentifier identifier) {
-        val className = identifier.getClassName();
-        return Result.of(()->_Context.loadClass(className))
-                .<String>mapSuccess(this::asFeatureFqns)
-                .presentElse(className);
-    }
-
-    private String asFeatureFqns(Class<?> cls) {
-        return Optional.ofNullable(specificationLoader.loadSpecification(cls))
-                .map(ObjectSpecification::getSpecId)
-                .map(ObjectSpecId::asString)
-                .orElseGet(()->cls.getName());
-    }
-
 
 }
diff --git a/security/shiro/src/test/java/org/apache/isis/security/shiro/TypeIdentifierTestFactory.java b/security/shiro/src/test/java/org/apache/isis/security/shiro/TypeIdentifierTestFactory.java
index 50438fd..7fa0571 100644
--- a/security/shiro/src/test/java/org/apache/isis/security/shiro/TypeIdentifierTestFactory.java
+++ b/security/shiro/src/test/java/org/apache/isis/security/shiro/TypeIdentifierTestFactory.java
@@ -22,9 +22,6 @@ import org.apache.isis.applib.id.TypeIdentifier;
 
 final class TypeIdentifierTestFactory {
 
-    private static class Customer {};
-    private static class Order {};
-    
     static TypeIdentifier customer() {
         return TypeIdentifier.fqcn(Customer.class);
     }
@@ -34,3 +31,6 @@ final class TypeIdentifierTestFactory {
     }
     
 }
+
+final class Customer {}
+final class Order {};
diff --git a/security/shiro/src/test/resources/shiro.ini b/security/shiro/src/test/resources/shiro.ini
index 97c6930..66ca198 100644
--- a/security/shiro/src/test/resources/shiro.ini
+++ b/security/shiro/src/test/resources/shiro.ini
@@ -56,9 +56,9 @@ guest = guest, user, read-only
 # perm = [[!]permGroup/]packageName:className:memberName:r,w
 
 admin = *
-schwartz = org.apache.isis.security.shiro.TypeIdentifierTestFactory:Customer:*,\
-            org.apache.isis.security.shiro.TypeIdentifierTestFactory:Order:submit:*
-goodguy = org.apache.isis.security.shiro.TypeIdentifierTestFactory:Customer:remove:*
+schwartz = org.apache.isis.security.shiro:Customer:*,\
+            org.apache.isis.security.shiro:Order:submit:*
+goodguy = org.apache.isis.security.shiro:Customer:remove:*