Skip to content

Commit ad9962a

Browse files
committed
[Security] Fixed SwitchUserListener when exiting an impersonication with AnonymousToken
If you configure a firewall with switch user with `role: IS_AUTHENTICATED_ANONYMOUSLY` it's impossible to exit the impersonation because the next line `$this->provider->refreshUser($original->getUser())` will fail. It fails because `RefreshUser` expects an instance of `UserInterface` and here it's a string. Therefore, it does not make sense to refresh an Anonymous Token, right ?
1 parent b46aa11 commit ad9962a

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Firewall/SwitchUserListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1515
use Symfony\Component\Security\Core\SecurityContextInterface;
16+
use Symfony\Component\Security\Core\User\UserInterface;
1617
use Symfony\Component\Security\Core\User\UserProviderInterface;
1718
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1819
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
@@ -162,7 +163,7 @@ private function attemptExitUser(Request $request)
162163
throw new AuthenticationCredentialsNotFoundException('Could not find original Token object.');
163164
}
164165

165-
if (null !== $this->dispatcher) {
166+
if (null !== $this->dispatcher && $original->getUser() instanceof UserInterface) {
166167
$user = $this->provider->refreshUser($original->getUser());
167168
$switchEvent = new SwitchUserEvent($request, $user);
168169
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);

0 commit comments

Comments
 (0)