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 12:04:21 UTC

[causeway] 01/01: CAUSEWAY-3519: removes ImpersonatedUserHolder

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

commit cdf6b1a333c312bebfe669c161e616fd7106911b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Jul 5 14:04:13 2023 +0200

    CAUSEWAY-3519: removes ImpersonatedUserHolder
    
    - no need to delegate, as the UserService had not much to do anyway
---
 .../services/user/ImpersonatedUserHolder.java      | 58 ------------------
 .../causeway/applib/services/user/UserMemento.java | 13 ++--
 .../causeway/applib/services/user/UserService.java | 70 +++++++++++++---------
 .../CausewayModuleCoreRuntimeServices.java         |  2 -
 .../user/ImpersonatedUserHolderDefault.java        | 69 ---------------------
 .../manager/AuthenticationManager.java             |  2 +-
 .../userprof/UserProfileUiServiceDefault.java      | 20 +++----
 .../AuthenticatedWebSessionForCauseway.java        | 26 ++++----
 ...nticatedWebSessionForCauseway_Authenticate.java |  8 +--
 9 files changed, 77 insertions(+), 191 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/causeway/applib/services/user/ImpersonatedUserHolder.java b/api/applib/src/main/java/org/apache/causeway/applib/services/user/ImpersonatedUserHolder.java
deleted file mode 100644
index e14c841f32..0000000000
--- a/api/applib/src/main/java/org/apache/causeway/applib/services/user/ImpersonatedUserHolder.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- *  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.causeway.applib.services.user;
-
-import java.util.Optional;
-
-/**
- * Used by the framework's default implementation of {@link UserService} to
- * allow the current user to be temporarily impersonated.
- *
- * <p>
- *     The intention is that viewers provide an implementation of this service.
- *     Note that the Wicket viewer <i>does</i> implement this service and
- *     uses an {@link javax.servlet.http.HttpSession}; this will have the side-effect
- * </p>
- *
- * @since 2.0 {@index}
- */
-public interface ImpersonatedUserHolder {
-
-    /**
-     * Whether or not this implementation supports impersonation.
-     *
-     * <p>
-     *     If it does <i>not</i>, then {@link #setUserMemento(UserMemento)}
-     *     is expected/required to throw an exception.
-     *     throw
-     * </p>
-     */
-    boolean supportsImpersonation();
-
-    /**
-     * Sets the user memento to be impersonated.
-     *
-     * @param userMemento
-     */
-    void setUserMemento(final UserMemento userMemento);
-
-    Optional<UserMemento> getUserMemento();
-
-    void clearUserMemento();
-}
diff --git a/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserMemento.java b/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserMemento.java
index 78561b9ecf..646c954864 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserMemento.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/services/user/UserMemento.java
@@ -24,6 +24,7 @@ import java.io.Serializable;
 import java.net.URL;
 import java.util.List;
 import java.util.Locale;
+import java.util.Objects;
 import java.util.stream.Stream;
 
 import javax.inject.Named;
@@ -154,10 +155,8 @@ implements Serializable {
         @Order(PriorityPrecedence.LATE)
         @EventListener(UserMemento.TitleUiEvent.class)
         public void on(final UserMemento.TitleUiEvent ev) {
-            val userMemento = ev.getSource();
-            assert userMemento != null;
-            val title = String.format("%s %s", userMemento.getName(), userMemento.isImpersonating() ? " (impersonating)" : "");
-            ev.setTitle(title);
+            val userMemento = Objects.requireNonNull(ev.getSource());
+            ev.setTitle(userMemento.nameFormatted());
         }
     }
 
@@ -172,6 +171,12 @@ implements Serializable {
     @NonNull
     String name;
 
+    public String nameFormatted() {
+        return isImpersonating()
+                ? String.format("%s 👻", name)
+                : name;
+    }
+
     /**
      * Excluded from {@link #equals(Object) equality} checks.
      */
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 7b07fc65f4..4ade06cc7f 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
@@ -34,11 +34,11 @@ import org.apache.causeway.applib.CausewayModuleApplib;
 import org.apache.causeway.applib.annotation.PriorityPrecedence;
 import org.apache.causeway.applib.services.iactnlayer.InteractionContext;
 import org.apache.causeway.applib.services.iactnlayer.InteractionLayerTracker;
+import org.apache.causeway.applib.services.keyvaluestore.KeyValueSessionStore;
 import org.apache.causeway.applib.services.sudo.SudoService;
 import org.apache.causeway.commons.internal.exceptions._Exceptions;
 
 import lombok.RequiredArgsConstructor;
-import lombok.val;
 
 /**
  * Allows the domain object to obtain the identity of the user interacting with
@@ -53,7 +53,7 @@ import lombok.val;
  * <p>
  * In addition, if impersonation has been invoked through the
  * {@link ImpersonateMenu}, then this service will report the impersonated user,
- * with the companion {@link ImpersonatedUserHolder} taking responsibilty for
+ * with the companion {@link KeyValueSessionStore} taking responsibilty for
  * remembering the impersonated user over multiple (http) requests, eg using an
  * http session. It's important to note that under these circumstances the user
  * reported by this service (the &quot;effective&quot; user) will <i>not</i> be
@@ -66,7 +66,6 @@ import lombok.val;
  * @see org.apache.causeway.applib.services.iactnlayer.InteractionContext
  * @see SudoService
  * @see ImpersonateMenu
- * @see ImpersonatedUserHolder
  *
  * @since 1.x revised in 2.0 {@index}
  */
@@ -84,8 +83,10 @@ public class UserService {
      */
     public static final String NOBODY = "__causeway_nobody";
 
+    private static final String SESSION_KEY_IMPERSONATED_USER = UserService.class.getName() + "#sudo";
+
     private final Provider<InteractionLayerTracker> iInteractionLayerTrackerProvider;
-    private final List<ImpersonatedUserHolder> impersonatedUserHolders;
+    private final Optional<KeyValueSessionStore> keyValueSessionStore;
 
     /**
      * Returns the details about the current user, either the &quot;effective&quot;
@@ -94,10 +95,7 @@ public class UserService {
      * {@link InteractionContext} of the current thread).
      */
     public Optional<UserMemento> currentUser() {
-        val impersonatedUserIfAny = impersonatedUserIfAny();
-        return impersonatedUserIfAny.isPresent()
-                ? impersonatedUserIfAny
-                : iInteractionLayerTrackerProvider.get()
+        return iInteractionLayerTrackerProvider.get() //TODO[CAUSEWAY-3519] must return the amended user
                     .currentInteractionContext()
                     .map(InteractionContext::getUser);
     }
@@ -158,7 +156,7 @@ public class UserService {
      * @see #stopImpersonating()
      */
     public boolean isImpersonating() {
-        return impersonatedUserIfAny().isPresent();
+        return lookupSudoUser().isPresent();
     }
 
     /**
@@ -180,20 +178,9 @@ public class UserService {
      *         request.
      */
     public boolean supportsImpersonation() {
-        return impersonatingHolder().isPresent();
-    }
-
-    private Optional<ImpersonatedUserHolder> impersonatingHolder() {
-        for (ImpersonatedUserHolder impersonatedUserHolder : impersonatedUserHolders) {
-            try {
-                if (impersonatedUserHolder.supportsImpersonation()) {
-                    return Optional.of(impersonatedUserHolder);
-                }
-            } catch (Exception ignore) {
-                // ignore
-            }
-        }
-        return Optional.empty();
+        return keyValueSessionStore
+            .map(KeyValueSessionStore::isSessionAvailable)
+            .orElse(false);
     }
 
     /**
@@ -222,9 +209,17 @@ public class UserService {
             final String userName,
             final List<String> roles,
             final String multiTenancyToken) {
-        impersonatingHolder()
-        .ifPresent(x -> x.setUserMemento(UserMemento.ofNameAndRoleNames(userName, roles)
-                .withImpersonating(true).withMultiTenancyToken(multiTenancyToken)));
+        setSudoUser(UserMemento.ofNameAndRoleNames(userName, roles)
+                .withMultiTenancyToken(multiTenancyToken));
+    }
+
+    /**
+     * Optionally the impersonated user, based on whether
+     * a call to {@link #impersonateUser(String, List, String)}
+     * was made within the current HTTP session.
+     */
+    public Optional<UserMemento> lookupImpersonatedUser() {
+        return lookupSudoUser();
     }
 
     /**
@@ -246,11 +241,28 @@ public class UserService {
      * @see #isImpersonating()
      */
     public void stopImpersonating() {
-        impersonatingHolder().ifPresent(ImpersonatedUserHolder::clearUserMemento);
+        clearSudoUser();
+    }
+
+    // -- HELPER - KEY STORE FOR IMPERSONATING
+
+    private void setSudoUser(final @Nullable UserMemento userMemento) {
+        if(userMemento==null) {
+            clearSudoUser();
+            return;
+        }
+        keyValueSessionStore
+            .ifPresent(store->store.put(SESSION_KEY_IMPERSONATED_USER, userMemento));
+    }
+
+    private Optional<UserMemento> lookupSudoUser() {
+        return keyValueSessionStore
+            .flatMap(store->store.lookupAs(SESSION_KEY_IMPERSONATED_USER, UserMemento.class));
     }
 
-    private Optional<UserMemento> impersonatedUserIfAny() {
-        return impersonatingHolder().flatMap(ImpersonatedUserHolder::getUserMemento);
+    private void clearSudoUser() {
+        keyValueSessionStore
+            .ifPresent(store->store.clear(SESSION_KEY_IMPERSONATED_USER));
     }
 
 }
diff --git a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/CausewayModuleCoreRuntimeServices.java b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/CausewayModuleCoreRuntimeServices.java
index ab6295a4b6..5810ce7d02 100644
--- a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/CausewayModuleCoreRuntimeServices.java
+++ b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/CausewayModuleCoreRuntimeServices.java
@@ -68,7 +68,6 @@ import org.apache.causeway.core.runtimeservices.spring.SpringBeansService;
 import org.apache.causeway.core.runtimeservices.transaction.TransactionServiceSpring;
 import org.apache.causeway.core.runtimeservices.urlencoding.UrlEncodingServiceWithCompression;
 import org.apache.causeway.core.runtimeservices.user.ImpersonateMenuAdvisorDefault;
-import org.apache.causeway.core.runtimeservices.user.ImpersonatedUserHolderDefault;
 import org.apache.causeway.core.runtimeservices.user.UserCurrentSessionTimeZoneHolderDefault;
 import org.apache.causeway.core.runtimeservices.userreg.EmailNotificationServiceDefault;
 import org.apache.causeway.core.runtimeservices.wrapper.WrapperFactoryDefault;
@@ -96,7 +95,6 @@ import org.apache.causeway.core.runtimeservices.xmlsnapshot.XmlSnapshotServiceDe
         FactoryServiceDefault.class,
         HomePageResolverServiceDefault.class,
         ImpersonateMenuAdvisorDefault.class,
-        ImpersonatedUserHolderDefault.class,
         InteractionDtoFactoryDefault.class,
         InteractionIdGeneratorDefault.class,
         InteractionServiceDefault.class,
diff --git a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/user/ImpersonatedUserHolderDefault.java b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/user/ImpersonatedUserHolderDefault.java
deleted file mode 100644
index 959108db22..0000000000
--- a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/user/ImpersonatedUserHolderDefault.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- *  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.causeway.core.runtimeservices.user;
-
-import java.util.Optional;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-
-import org.springframework.stereotype.Service;
-
-import org.apache.causeway.applib.annotation.PriorityPrecedence;
-import org.apache.causeway.applib.services.keyvaluestore.KeyValueSessionStore;
-import org.apache.causeway.applib.services.user.ImpersonatedUserHolder;
-import org.apache.causeway.applib.services.user.UserMemento;
-import org.apache.causeway.core.runtimeservices.CausewayModuleCoreRuntimeServices;
-
-@Service
-@Named(CausewayModuleCoreRuntimeServices.NAMESPACE + ".ImpersonatedUserHolderDefault")
-@javax.annotation.Priority(PriorityPrecedence.MIDPOINT)
-public class ImpersonatedUserHolderDefault implements ImpersonatedUserHolder {
-
-    private static final String SESSION_KEY_IMPERSONATED_USER =
-            ImpersonatedUserHolderDefault.class.getName() + "#userMemento";
-
-    @Inject private Optional<KeyValueSessionStore> keyValueSessionStore = Optional.empty();
-
-    @Override
-    public boolean supportsImpersonation() {
-        return keyValueSessionStore
-            .map(KeyValueSessionStore::isSessionAvailable)
-            .orElse(false);
-    }
-
-    @Override
-    public void setUserMemento(final UserMemento userMemento) {
-        keyValueSessionStore
-            .ifPresent(store->store.put(SESSION_KEY_IMPERSONATED_USER, userMemento));
-    }
-
-    @Override
-    public Optional<UserMemento> getUserMemento() {
-        return keyValueSessionStore
-            .flatMap(store->store.lookupAs(SESSION_KEY_IMPERSONATED_USER, UserMemento.class));
-    }
-
-    @Override
-    public void clearUserMemento() {
-        keyValueSessionStore
-            .ifPresent(store->store.clear(SESSION_KEY_IMPERSONATED_USER));
-    }
-
-}
diff --git a/core/security/src/main/java/org/apache/causeway/core/security/authentication/manager/AuthenticationManager.java b/core/security/src/main/java/org/apache/causeway/core/security/authentication/manager/AuthenticationManager.java
index f18fa9889a..da0fa63898 100644
--- a/core/security/src/main/java/org/apache/causeway/core/security/authentication/manager/AuthenticationManager.java
+++ b/core/security/src/main/java/org/apache/causeway/core/security/authentication/manager/AuthenticationManager.java
@@ -26,7 +26,6 @@ import javax.annotation.Priority;
 import javax.inject.Inject;
 import javax.inject.Named;
 
-import org.apache.causeway.core.security.CausewayModuleCoreSecurity;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.lang.Nullable;
 import org.springframework.stereotype.Service;
@@ -41,6 +40,7 @@ import org.apache.causeway.applib.util.ToString;
 import org.apache.causeway.commons.collections.Can;
 import org.apache.causeway.commons.internal.base._Timing;
 import org.apache.causeway.commons.internal.collections._Maps;
+import org.apache.causeway.core.security.CausewayModuleCoreSecurity;
 import org.apache.causeway.core.security.authentication.AuthenticationRequest;
 import org.apache.causeway.core.security.authentication.Authenticator;
 import org.apache.causeway.core.security.authentication.standard.RandomCodeGenerator;
diff --git a/viewers/commons/services/src/main/java/org/apache/causeway/viewer/commons/services/userprof/UserProfileUiServiceDefault.java b/viewers/commons/services/src/main/java/org/apache/causeway/viewer/commons/services/userprof/UserProfileUiServiceDefault.java
index 9d7d3bfaa6..6809fef874 100644
--- a/viewers/commons/services/src/main/java/org/apache/causeway/viewer/commons/services/userprof/UserProfileUiServiceDefault.java
+++ b/viewers/commons/services/src/main/java/org/apache/causeway/viewer/commons/services/userprof/UserProfileUiServiceDefault.java
@@ -30,6 +30,7 @@ import org.apache.causeway.applib.services.i18n.TranslationContext;
 import org.apache.causeway.applib.services.i18n.TranslationService;
 import org.apache.causeway.applib.services.user.UserMemento;
 import org.apache.causeway.applib.services.user.UserService;
+import org.apache.causeway.commons.internal.base._Strings;
 import org.apache.causeway.viewer.commons.applib.services.userprof.UserProfileUiModel;
 import org.apache.causeway.viewer.commons.applib.services.userprof.UserProfileUiService;
 import org.apache.causeway.viewer.commons.services.CausewayModuleViewerCommonsServices;
@@ -58,23 +59,18 @@ public class UserProfileUiServiceDefault implements UserProfileUiService {
                 .orElse(String.format("<%s>", translated("Anonymous")));
     }
 
-    private String userNameFor(final UserMemento x) {
-        final String username = x.getName();
-        if (x.isImpersonating()) {
-            return String.format("%s (%s)", username, translated("impersonating"));
+    private String userNameFor(final UserMemento user) {
+        if (user.isImpersonating()) {
+            return user.nameFormatted();
         }
-        val realName = x.getRealName();
-        return !isNullOrEmpty(realName)
-                ? realName
-                : username;
+        val realName = user.getRealName();
+        return _Strings.isNullOrEmpty(realName)
+                ? user.getName()
+                : realName;
     }
 
     private String translated(final String str) {
         return translationService.translate(TranslationContext.forClassName(getClass()), str);
     }
 
-    private static boolean isNullOrEmpty(final String realName) {
-        return realName == null || realName.equals("");
-    }
-
 }
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 2282dee91c..6f0301a575 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
@@ -32,9 +32,9 @@ import org.apache.causeway.applib.clock.VirtualClock;
 import org.apache.causeway.applib.services.clock.ClockService;
 import org.apache.causeway.applib.services.iactnlayer.InteractionContext;
 import org.apache.causeway.applib.services.session.SessionSubscriber;
-import org.apache.causeway.applib.services.user.ImpersonatedUserHolder;
 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.core.metamodel.context.MetaModelContext;
 import org.apache.causeway.core.security.authentication.AuthenticationRequestPassword;
@@ -180,7 +180,7 @@ implements
 
     /**
      * Returns an {@link InteractionContext} either as authenticated (and then cached on the session subsequently),
-     * or taking into account {@link ImpersonatedUserHolder impersonation}.
+     * or taking into account {@link UserService impersonation}.
      *
      * <p>
      *     The session must still {@link AuthenticationManager#isSessionValid(InteractionContext) be valid}, though
@@ -189,17 +189,17 @@ implements
      */
     public synchronized InteractionContext getAuthentication() {
 
-        if(this.authentication == null) {
+        if(authentication == null) {
             return null;
         }
-        if(!getAuthenticationManager().isSessionValid(this.authentication)) {
+        if(!getAuthenticationManager().isSessionValid(authentication)) {
             return null;
         }
         signIn(true);
 
-        val impersonatedUserHolder = lookupServiceElseFail(ImpersonatedUserHolder.class);
-        return impersonatedUserHolder.getUserMemento()
-                .map(x -> this.authentication.withUser(x))
+        return lookupServiceElseFail(UserService.class)
+                .lookupImpersonatedUser()
+                .map(sudoUser -> this.authentication.withUser(sudoUser))
                 .orElse(this.authentication);
     }
 
@@ -240,6 +240,12 @@ implements
         super.detach();
     }
 
+    @Override
+    public void replaceSession() {
+        // do nothing here because this will lead to problems with Shiro
+        // see https://issues.apache.org/jira/browse/CAUSEWAY-1018
+    }
+
     private void log(
             final SessionSubscriber.Type type,
             final String username,
@@ -286,11 +292,7 @@ implements
         return VirtualClock.system();
     }
 
-    @Override
-    public void replaceSession() {
-        // do nothing here because this will lead to problems with Shiro
-        // see https://issues.apache.org/jira/browse/CAUSEWAY-1018
-    }
+
 
 
 
diff --git a/viewers/wicket/viewer/src/test/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway_Authenticate.java b/viewers/wicket/viewer/src/test/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway_Authenticate.java
index c9b72728fa..41dec57621 100644
--- a/viewers/wicket/viewer/src/test/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway_Authenticate.java
+++ b/viewers/wicket/viewer/src/test/java/org/apache/causeway/viewer/wicket/viewer/integration/AuthenticatedWebSessionForCauseway_Authenticate.java
@@ -34,7 +34,7 @@ import static org.hamcrest.Matchers.nullValue;
 
 import org.apache.causeway.applib.services.iactnlayer.InteractionLayerTracker;
 import org.apache.causeway.applib.services.iactnlayer.InteractionService;
-import org.apache.causeway.applib.services.user.ImpersonatedUserHolder;
+import org.apache.causeway.applib.services.user.UserService;
 import org.apache.causeway.core.metamodel._testing.MetaModelContext_forTesting;
 import org.apache.causeway.core.metamodel.context.MetaModelContext;
 import org.apache.causeway.core.security._testing.InteractionService_forTesting;
@@ -51,7 +51,7 @@ class AuthenticatedWebSessionForCauseway_Authenticate {
     protected Request mockRequest = Mockito.mock(Request.class);
     protected Authenticator mockAuthenticator = Mockito.mock(Authenticator.class);
     protected InteractionService mockInteractionService = Mockito.mock(InteractionService.class);
-    protected ImpersonatedUserHolder mockImpersonatedUserHolder = Mockito.mock(ImpersonatedUserHolder.class);
+    protected UserService mockUserService = Mockito.mock(UserService.class);
     protected InteractionLayerTracker mockInteractionLayerTracker = Mockito.mock(InteractionLayerTracker.class);
 
     protected AuthenticatedWebSessionForCauseway webSession;
@@ -62,7 +62,7 @@ class AuthenticatedWebSessionForCauseway_Authenticate {
 
         mmc = MetaModelContext_forTesting.builder()
                 .singleton(mockInteractionService)
-                .singleton(mockImpersonatedUserHolder)
+                .singleton(mockUserService)
                 .build();
 
         authMgr = new AuthenticationManager(
@@ -96,7 +96,7 @@ class AuthenticatedWebSessionForCauseway_Authenticate {
     void delegatesToAuthenticationManagerAndCachesAuthSessionIfOk() {
 
         Mockito
-        .when(mockImpersonatedUserHolder.getUserMemento())
+        .when(mockUserService.lookupImpersonatedUser())
         .thenReturn(Optional.empty());
 
         Mockito