Skip to content

Commit cdae16c

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 1314365 commit cdae16c

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\HttpFoundation\RedirectResponse;
2323
use Symfony\Component\HttpFoundation\Request;
2424
use Symfony\Component\Security\Core\Role\SwitchUserRole;
25+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
2526
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2627
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
2728
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -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 instanceof AnonymousToken) {
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)