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 2020/12/04 15:07:12 UTC

[isis] 03/03: ISIS-2464: proper equals and hashcode for AuthenitcationSessionAbstract, UserMemento, RoleMemento and ExecutionContext

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

commit 55929fa28a2a7b9c187cf5ab6dca17906345673f
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Dec 4 16:06:52 2020 +0100

    ISIS-2464: proper equals and hashcode for AuthenitcationSessionAbstract,
    UserMemento, RoleMemento and ExecutionContext
---
 .../isis/applib/services/user/RoleMemento.java     |  2 +
 .../isis/applib/services/user/UserMemento.java     | 97 +++++++++++++++-------
 .../core/runtime/iactn/InteractionTracker.java     |  4 +-
 .../runtimeservices/clock/ClockServiceDefault.java |  2 +-
 .../authentication/AuthenticationSession.java      | 17 +---
 .../AuthenticationSessionAbstract.java             | 55 ++++++++----
 .../authentication/standard/SimpleSession.java     | 31 -------
 7 files changed, 112 insertions(+), 96 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/isis/applib/services/user/RoleMemento.java b/api/applib/src/main/java/org/apache/isis/applib/services/user/RoleMemento.java
index e90d1fe..4558de9 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/services/user/RoleMemento.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/services/user/RoleMemento.java
@@ -24,7 +24,9 @@ import java.io.Serializable;
 import org.apache.isis.applib.annotation.MemberOrder;
 
 import lombok.Getter;
+import lombok.Value;
 
+@Value
 // tag::refguide[]
 public final class RoleMemento implements Serializable {
 
diff --git a/api/applib/src/main/java/org/apache/isis/applib/services/user/UserMemento.java b/api/applib/src/main/java/org/apache/isis/applib/services/user/UserMemento.java
index 2df0b56..9a15048 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/services/user/UserMemento.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/services/user/UserMemento.java
@@ -21,6 +21,7 @@ package org.apache.isis.applib.services.user;
 
 import java.io.Serializable;
 import java.util.List;
+import java.util.Objects;
 import java.util.stream.Stream;
 
 import org.apache.isis.applib.annotation.MemberOrder;
@@ -29,7 +30,6 @@ import org.apache.isis.commons.internal.collections._Lists;
 
 import lombok.Getter;
 import lombok.NonNull;
-import lombok.experimental.UtilityClass;
 
 /**
  * Details about a user and his roles.
@@ -141,29 +141,8 @@ public final class UserMemento implements Serializable {
                 .map(RoleMemento::getName);
     }
 
-//XXX implemented as regex match, java-doc is not specific about what these methods actually do; so if in doubt, rather remove     
-//    /**
-//     * Determines if the user fulfills the specified role.
-//     *
-//     * @param role  the role to search for, regular expressions are allowed
-//     */
-//    public boolean hasRole(final RoleMemento role) {
-//        return hasRole(role.getName());
-//    }
-//
-//    /**
-//     * Determines if the user fulfills the specified role. Roles are compared
-//     * lexically by role name.
-//     */
-//    public boolean hasRole(final String roleName) {
-//        for (final RoleMemento role : roles) {
-//            if (role.getName().matches(roleName)) {
-//                return true;
-//            }
-//        }
-//        return false;
-//    }
-
+    // -- TO STRING, EQUALS, HASHCODE
+    
     @Override
     public String toString() {
         final StringBuilder buf = new StringBuilder();
@@ -172,19 +151,75 @@ public final class UserMemento implements Serializable {
         }
         return "User [name=" + getName() + ",roles=" + buf.toString() + "]";
     }
-
-    @UtilityClass
-    public static class NameType {
-        @UtilityClass
-        public static class Meta {
-            public static final int MAX_LEN = 50;
+    
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (obj.getClass() != getClass()) {
+            return false;
         }
+        return isEqualsTo((UserMemento) obj);
     }
 
-
+    private boolean isEqualsTo(final UserMemento other) {
+        if(!Objects.equals(this.getName(), other.getName())) {
+            return false;
+        }
+        if(!Objects.equals(this.getRoles(), other.getRoles())) {
+            return false;
+        }
+        return true;
+    }
+    
+    @Override
+    public int hashCode() {
+        return getName().hashCode();
+    }
+        
+    
     // tag::refguide[]
 
     // ...
 
 }
 // end::refguide[]
+
+// -- REMOVED
+
+//XXX implemented as regex match, java-doc is not specific about what these methods actually do; so if in doubt, rather remove     
+///**
+//* Determines if the user fulfills the specified role.
+//*
+//* @param role  the role to search for, regular expressions are allowed
+//*/
+//public boolean hasRole(final RoleMemento role) {
+//  return hasRole(role.getName());
+//}
+//
+///**
+//* Determines if the user fulfills the specified role. Roles are compared
+//* lexically by role name.
+//*/
+//public boolean hasRole(final String roleName) {
+//  for (final RoleMemento role : roles) {
+//      if (role.getName().matches(roleName)) {
+//          return true;
+//      }
+//  }
+//  return false;
+//}
+
+//XXX not used    
+//@UtilityClass
+//public static class NameType {
+//  @UtilityClass
+//  public static class Meta {
+//      public static final int MAX_LEN = 50;
+//  }
+//}
+
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/iactn/InteractionTracker.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/iactn/InteractionTracker.java
index 60a1c90..1e99ee2 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/iactn/InteractionTracker.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/iactn/InteractionTracker.java
@@ -53,8 +53,8 @@ extends
         return currentInteractionLayer().map(InteractionLayer::getAuthenticationSession);
     }
     
-    default Optional<ExecutionContext> currentExecutionEnvironment() {
-        return currentAuthenticationSession().map(AuthenticationSession::getExecutionEnvironment);
+    default Optional<ExecutionContext> currentExecutionContext() {
+        return currentAuthenticationSession().map(AuthenticationSession::getExecutionContext);
     }
     
     @Override
diff --git a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/clock/ClockServiceDefault.java b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/clock/ClockServiceDefault.java
index dc1a4ec..acfc9f6 100644
--- a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/clock/ClockServiceDefault.java
+++ b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/clock/ClockServiceDefault.java
@@ -28,7 +28,7 @@ public class ClockServiceDefault implements ClockService {
 
     @Override
     public VirtualClock getClock() {
-        return interactionTracker.currentExecutionEnvironment()
+        return interactionTracker.currentExecutionContext()
         .map(ExecutionContext::getClock)
         .orElseGet(VirtualClock::system);
     }
diff --git a/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSession.java b/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSession.java
index 7ea5690..fa6222f 100644
--- a/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSession.java
+++ b/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSession.java
@@ -61,17 +61,6 @@ public interface AuthenticationSession extends Serializable {
     String getValidationCode();
 
     /**
-     * For viewers (in particular) to store additional attributes, analogous to
-     * an <tt>HttpSession</tt>.
-     */
-    Object getAttribute(String attributeName);
-
-    /**
-     * @see #getAttribute(String)
-     */
-    void setAttribute(String attributeName, Object attribute);
-
-    /**
      * The {@link MessageBroker} that holds messages for this user.
      */
     MessageBroker getMessageBroker();
@@ -83,16 +72,16 @@ public interface AuthenticationSession extends Serializable {
      * own simulated (or actual) user 
      */
     default UserMemento getUser() {
-        return getExecutionEnvironment().getUser();
+        return getExecutionContext().getUser();
     }
     
     /**
-     * The execution environment (programmatically) simulated (or actual), belonging to this session.
+     * The {@link ExecutionContext} (programmatically) simulated (or actual), belonging to this session.
      * 
      * @apiNote immutable, allows an {@link Interaction} to (logically) run with its 
      * own simulated (or actual) clock 
      */
-    ExecutionContext getExecutionEnvironment();
+    ExecutionContext getExecutionContext();
 
     /**
      * To support external security mechanisms such as keycloak, where the validity of the session is defined by
diff --git a/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSessionAbstract.java b/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSessionAbstract.java
index 15ef6c5..a5d098f 100644
--- a/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSessionAbstract.java
+++ b/core/security/src/main/java/org/apache/isis/core/security/authentication/AuthenticationSessionAbstract.java
@@ -19,8 +19,6 @@
 package org.apache.isis.core.security.authentication;
 
 import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.Objects;
 
 import org.apache.isis.applib.services.iactn.ExecutionContext;
@@ -41,7 +39,7 @@ implements AuthenticationSession, Serializable {
     // -- Constructor, fields
 
     @Getter(onMethod_ = {@Override}) @NonNull 
-    private final ExecutionContext executionEnvironment;
+    private final ExecutionContext executionContext;
     
     @Getter(onMethod_ = {@Override}) @NonNull 
     private final String userName;
@@ -55,13 +53,11 @@ implements AuthenticationSession, Serializable {
     @Getter(onMethod_ = {@Override}) @NonNull
     private final MessageBroker messageBroker;
     
-    private final Map<String, Object> attributeByName = new HashMap<String, Object>();
-
     public AuthenticationSessionAbstract(
             @NonNull final ExecutionContext executionEnvironment,
             @NonNull final String validationCode) {
 
-        this.executionEnvironment = executionEnvironment;
+        this.executionContext = executionEnvironment;
         this.userName = getUser().getName();
         this.roles = Can.ofCollection(getUser().getRoles())
                 .map(RoleMemento::getName);
@@ -87,18 +83,17 @@ implements AuthenticationSession, Serializable {
 
     // -- Attributes
 
-    @Override
-    public Object getAttribute(final String attributeName) {
-        return attributeByName.get(attributeName);
-    }
-
-    @Override
-    public void setAttribute(final String attributeName, final Object attribute) {
-        attributeByName.put(attributeName, attribute);
-    }
+//    @Override
+//    public Object getAttribute(final String attributeName) {
+//        return attributeByName.get(attributeName);
+//    }
+//
+//    @Override
+//    public void setAttribute(final String attributeName, final Object attribute) {
+//        attributeByName.put(attributeName, attribute);
+//    }
 
-
-    // -- toString
+    // -- TO STRING, EQUALS, HASHCODE
 
     private static final ToString<AuthenticationSessionAbstract> toString = ToString
             .toString("name", AuthenticationSessionAbstract::getUserName)
@@ -108,5 +103,31 @@ implements AuthenticationSession, Serializable {
     public String toString() {
         return toString.toString(this);
     }
+    
+    @Override
+    public boolean equals(final Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (obj.getClass() != getClass()) {
+            return false;
+        }
+        return isEqualsTo((AuthenticationSessionAbstract) obj);
+    }
+
+    private boolean isEqualsTo(final AuthenticationSessionAbstract other) {
+        if(!getExecutionContext().equals(other.getExecutionContext())) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return getUserName().hashCode(); // its good enough to hash on just the user's name
+    }
 
 }
diff --git a/core/security/src/main/java/org/apache/isis/core/security/authentication/standard/SimpleSession.java b/core/security/src/main/java/org/apache/isis/core/security/authentication/standard/SimpleSession.java
index 7aee1e7..c2367ec 100644
--- a/core/security/src/main/java/org/apache/isis/core/security/authentication/standard/SimpleSession.java
+++ b/core/security/src/main/java/org/apache/isis/core/security/authentication/standard/SimpleSession.java
@@ -69,35 +69,4 @@ extends AuthenticationSessionAbstract {
     @Getter @Setter
     private Type type = Type.DEFAULT;
 
-
-    @Override
-    public boolean equals(final Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (obj == null) {
-            return false;
-        }
-        if (obj.getClass() != getClass()) {
-            return false;
-        }
-        final SimpleSession other = (SimpleSession) obj;
-        return equals(other);
-    }
-
-    public boolean equals(final SimpleSession other) {
-        if (other == this) {
-            return true;
-        }
-        if (other == null) {
-            return false;
-        }
-        return getUserName().equals(other.getUserName());
-    }
-
-    @Override
-    public int hashCode() {
-        return getUserName().hashCode();
-    }
-
 }