Skip to content

Commit cc31ea0

Browse files
94nonichalasr
authored andcommitted
[Security] Allow configuring a redirect url via route name when switching user
1 parent c4b5b8b commit cc31ea0

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Firewall/SwitchUserListener.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\RedirectResponse;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpKernel\Event\RequestEvent;
18+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1819
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1920
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
2021
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -51,9 +52,10 @@ class SwitchUserListener extends AbstractListener
5152
private ?LoggerInterface $logger;
5253
private ?EventDispatcherInterface $dispatcher;
5354
private bool $stateless;
54-
private ?string $targetUrl;
55+
private ?UrlGeneratorInterface $urlGenerator;
56+
private ?string $targetRoute;
5557

56-
public function __construct(TokenStorageInterface $tokenStorage, UserProviderInterface $provider, UserCheckerInterface $userChecker, string $firewallName, AccessDecisionManagerInterface $accessDecisionManager, LoggerInterface $logger = null, string $usernameParameter = '_switch_user', string $role = 'ROLE_ALLOWED_TO_SWITCH', EventDispatcherInterface $dispatcher = null, bool $stateless = false, string $targetUrl = null)
58+
public function __construct(TokenStorageInterface $tokenStorage, UserProviderInterface $provider, UserCheckerInterface $userChecker, string $firewallName, AccessDecisionManagerInterface $accessDecisionManager, LoggerInterface $logger = null, string $usernameParameter = '_switch_user', string $role = 'ROLE_ALLOWED_TO_SWITCH', EventDispatcherInterface $dispatcher = null, bool $stateless = false, UrlGeneratorInterface $urlGenerator = null, string $targetRoute = null)
5759
{
5860
if ('' === $firewallName) {
5961
throw new \InvalidArgumentException('$firewallName must not be empty.');
@@ -69,7 +71,8 @@ public function __construct(TokenStorageInterface $tokenStorage, UserProviderInt
6971
$this->logger = $logger;
7072
$this->dispatcher = $dispatcher;
7173
$this->stateless = $stateless;
72-
$this->targetUrl = $targetUrl;
74+
$this->urlGenerator = $urlGenerator;
75+
$this->targetRoute = $targetRoute;
7376
}
7477

7578
public function supports(Request $request): ?bool
@@ -121,7 +124,7 @@ public function authenticate(RequestEvent $event)
121124
if (!$this->stateless) {
122125
$request->query->remove($this->usernameParameter);
123126
$request->server->set('QUERY_STRING', http_build_query($request->query->all(), '', '&'));
124-
$response = new RedirectResponse($this->targetUrl ?? $request->getUri(), 302);
127+
$response = new RedirectResponse($this->urlGenerator && $this->targetRoute ? $this->urlGenerator->generate($this->targetRoute) : $request->getUri(), 302);
125128

126129
$event->setResponse($response);
127130
}

0 commit comments

Comments
 (0)