Skip to content

Commit 8308df8

Browse files
94nonichalasr
authored andcommitted
[Security] Allow configuring a target url when switching user
1 parent a76a3d4 commit 8308df8

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Firewall/SwitchUserListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class SwitchUserListener extends AbstractListener
5151
private ?LoggerInterface $logger;
5252
private ?EventDispatcherInterface $dispatcher;
5353
private bool $stateless;
54+
private ?string $targetUrl;
5455

55-
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)
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)
5657
{
5758
if ('' === $firewallName) {
5859
throw new \InvalidArgumentException('$firewallName must not be empty.');
@@ -68,6 +69,7 @@ public function __construct(TokenStorageInterface $tokenStorage, UserProviderInt
6869
$this->logger = $logger;
6970
$this->dispatcher = $dispatcher;
7071
$this->stateless = $stateless;
72+
$this->targetUrl = $targetUrl;
7173
}
7274

7375
/**
@@ -122,7 +124,7 @@ public function authenticate(RequestEvent $event)
122124
if (!$this->stateless) {
123125
$request->query->remove($this->usernameParameter);
124126
$request->server->set('QUERY_STRING', http_build_query($request->query->all(), '', '&'));
125-
$response = new RedirectResponse($request->getUri(), 302);
127+
$response = new RedirectResponse($this->targetUrl ?? $request->getUri(), 302);
126128

127129
$event->setResponse($response);
128130
}

0 commit comments

Comments
 (0)