You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/07/05 15:09:05 UTC

[causeway] branch 3519-broken.impersonate updated: CAUSEWAY-3519: cleaning up the logic

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

ahuber pushed a commit to branch 3519-broken.impersonate
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/3519-broken.impersonate by this push:
     new 646c381d64 CAUSEWAY-3519: cleaning up the logic
646c381d64 is described below

commit 646c381d647c2b9f465bd5516b94bcb228a6bde8
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Jul 5 17:09:00 2023 +0200

    CAUSEWAY-3519: cleaning up the logic
---
 .../causeway/applib/services/user/UserService.java |  2 +-
 .../session/InteractionServiceDefault.java         | 17 +------------
 .../AuthenticatedWebSessionForCauseway.java        | 16 +++++++------
 .../integration/WebRequestCycleForCauseway.java    | 28 +++++++++++++---------
 4 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserService.java b/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserService.java
index 5687004f0f..97e1f4d9e9 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserService.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserService.java
@@ -103,7 +103,7 @@ public class UserService {
 
         currentUser.ifPresent(
                 u->_Assert.assertEquals(u.isImpersonating(), isImpersonating(), ()->
-                    "framework bug: UserService and InteractionService disagree on impersonation state"));
+                    "framework bug: InteractionService and UserService disagree on impersonation state\n\t"));
 
         return currentUser;
     }
diff --git a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/session/InteractionServiceDefault.java b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/session/InteractionServiceDefault.java
index 37127d71e9..4d6e649cb5 100644
--- a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/session/InteractionServiceDefault.java
+++ b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/session/InteractionServiceDefault.java
@@ -31,6 +31,7 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Provider;
 
+import org.apache.causeway.applib.services.command.Command;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
 import org.springframework.context.annotation.Lazy;
@@ -41,7 +42,6 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
 
 import org.apache.causeway.applib.annotation.PriorityPrecedence;
 import org.apache.causeway.applib.services.clock.ClockService;
-import org.apache.causeway.applib.services.command.Command;
 import org.apache.causeway.applib.services.iactn.Interaction;
 import org.apache.causeway.applib.services.iactnlayer.InteractionContext;
 import org.apache.causeway.applib.services.iactnlayer.InteractionLayer;
@@ -186,11 +186,6 @@ implements
     public InteractionLayer openInteraction(
             final @NonNull InteractionContext interactionContextToUse) {
 
-        if(interactionContextToUse.getUser().isImpersonating()
-                || interactionContextToUse.getUser().getName().equals("sven")) {
-            System.err.printf("ia open %s%n", interactionContextToUse.getUser());
-        }
-
         val causewayInteraction = getOrCreateCausewayInteraction();
 
         // check whether we should reuse any current authenticationLayer,
@@ -268,11 +263,6 @@ implements
             final @NonNull InteractionContext interactionContext,
             final @NonNull Callable<R> callable) {
 
-        if(interactionContext.getUser().isImpersonating()
-                || interactionContext.getUser().getName().equals("sven")) {
-            System.err.printf("ia call %s%n", interactionContext.getUser());
-        }
-
         final int stackSizeWhenEntering = interactionLayerStack.get().size();
         openInteraction(interactionContext);
         try {
@@ -288,11 +278,6 @@ implements
             final @NonNull InteractionContext interactionContext,
             final @NonNull ThrowingRunnable runnable) {
 
-        if(interactionContext.getUser().isImpersonating()
-                || interactionContext.getUser().getName().equals("sven")) {
-            System.err.printf("ia run %s%n", interactionContext.getUser());
-        }
-
         final int stackSizeWhenEntering = interactionLayerStack.get().size();
         openInteraction(interactionContext);
         try {
diff --git a/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway.java b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway.java
index 9876eec5d4..44b6709988 100644
--- a/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway.java
+++ b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway.java
@@ -36,6 +36,7 @@ import org.apache.causeway.applib.services.user.UserMemento;
 import org.apache.causeway.applib.services.user.UserMemento.AuthenticationSource;
 import org.apache.causeway.applib.services.user.UserService;
 import org.apache.causeway.commons.collections.Can;
+import org.apache.causeway.commons.internal.assertions._Assert;
 import org.apache.causeway.core.metamodel.context.MetaModelContext;
 import org.apache.causeway.core.security.authentication.AuthenticationRequestPassword;
 import org.apache.causeway.core.security.authentication.manager.AuthenticationManager;
@@ -80,6 +81,11 @@ implements
      * As populated in {@link #signIn(String, String)}.
      */
     private InteractionContext authentication;
+    private void setAuthentication(final InteractionContext authentication) {
+        _Assert.assertFalse(authentication.getUser().isImpersonating(), ()-> 
+                "framework bug: cannot signin with an impersonated user");
+        this.authentication = authentication;
+    }
 
     @Getter
     private UUID sessionGuid;
@@ -114,8 +120,8 @@ implements
     public synchronized boolean authenticate(final String username, final String password) {
         val authenticationRequest = new AuthenticationRequestPassword(username, password);
         authenticationRequest.addRole(UserMemento.AUTHORIZED_USER_ROLE);
-        this.authentication = getAuthenticationManager().authenticate(authenticationRequest);
-        if (this.authentication != null) {
+        setAuthentication(getAuthenticationManager().authenticate(authenticationRequest));
+        if (authentication != null) {
             log(SessionSubscriber.Type.LOGIN, username, null);
             return true;
         } else {
@@ -163,7 +169,7 @@ implements
 
     @Override
     public void amendInteractionContext(final UnaryOperator<InteractionContext> updater) {
-        this.authentication = updater.apply(authentication);
+        setAuthentication(updater.apply(authentication));
     }
 
     /**
@@ -279,8 +285,4 @@ implements
         return VirtualClock.system();
     }
 
-
-
-
-
 }
diff --git a/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java
index 6b75acacb1..c761989225 100644
--- a/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java
+++ b/viewers/wicket/viewer/src/main/java/org/apache/causeway/viewer/wicket/viewer/integration/WebRequestCycleForCauseway.java
@@ -137,26 +137,32 @@ implements
             return;
         }
 
-        val interactionService = getInteractionService();
 
-        // participate if an InteractionContext was already provided through some other mechanism
-        val interactionContext0 = getInteractionService()
-            .currentInteractionContext()
+        // participate if an InteractionContext was already provided through some other mechanism,
+        // but fail early if the current user is impersonating
+        // (seeing this if going back the browser history into a page, that was previously impersonated)
+        val interactionService = getInteractionService();
+        val currentInteractionContext = interactionService.currentInteractionContext();
+        if(currentInteractionContext.isPresent()
+                && currentInteractionContext.get().getUser().isImpersonating()) {
+            throw _Exceptions.illegalState("cannot enter a new request cycle with a left over impersonating user");
+        }
+        val interactionContext0 = currentInteractionContext
             .orElseGet(AuthenticatedWebSessionForCauseway.get()::getAuthentication);
 
+        if (interactionContext0 == null) {
+            log.warn("onBeginRequest out - session was not opened (because no authentication)");
+            return;
+        }
+
         // impersonation support
-        val interactionContext = lookupServiceElseFail(UserService.class)
+        val interactionContext1 = lookupServiceElseFail(UserService.class)
                 .lookupImpersonatedUser()
                 .map(sudoUser -> interactionContext0.withUser(sudoUser))
                 .orElse(interactionContext0);
 
-        if (interactionContext == null) {
-            log.warn("onBeginRequest out - session was not opened (because no authentication)");
-            return;
-        }
-
         // Note: this is a no-op if an interactionContext layer was already opened and is unchanged.
-        interactionService.openInteraction(interactionContext);
+        interactionService.openInteraction(interactionContext1);
 
         log.debug("onBeginRequest out - session was opened");
     }