Skip to content

Commit ccabea1

Browse files
committed
[Security] Allow configuring a redirect url via route name when switching user
1 parent ddaedd2 commit ccabea1

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
257257
->scalarNode('provider')->end()
258258
->scalarNode('parameter')->defaultValue('_switch_user')->end()
259259
->scalarNode('role')->defaultValue('ROLE_ALLOWED_TO_SWITCH')->end()
260-
->scalarNode('target_url')->defaultValue(null)->end()
260+
->scalarNode('target_route')->defaultValue(null)->end()
261261
->end()
262262
->end()
263263
->arrayNode('required_badges')

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,8 @@ private function createSwitchUserListener(ContainerBuilder $container, string $i
845845
if (!$userProvider) {
846846
throw new InvalidConfigurationException(sprintf('Not configuring explicitly the provider for the "switch_user" listener on "%s" firewall is ambiguous as there is more than one registered provider.', $id));
847847
}
848-
if ($stateless && null !== $config['target_url']) {
849-
throw new InvalidConfigurationException(sprintf('Cannot set a "target_url" for the "switch_user" listener on the "%s" firewall as it is stateless.', $id));
848+
if ($stateless && null !== $config['target_route']) {
849+
throw new InvalidConfigurationException(sprintf('Cannot set a "target_route" for the "switch_user" listener on the "%s" firewall as it is stateless.', $id));
850850
}
851851

852852
$switchUserListenerId = 'security.authentication.switchuser_listener.'.$id;
@@ -857,7 +857,7 @@ private function createSwitchUserListener(ContainerBuilder $container, string $i
857857
$listener->replaceArgument(6, $config['parameter']);
858858
$listener->replaceArgument(7, $config['role']);
859859
$listener->replaceArgument(9, $stateless);
860-
$listener->replaceArgument(10, $config['target_url']);
860+
$listener->replaceArgument(11, $config['target_route']);
861861

862862
return $switchUserListenerId;
863863
}

src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@
151151
'ROLE_ALLOWED_TO_SWITCH',
152152
service('event_dispatcher')->nullOnInvalid(),
153153
false, // Stateless
154-
abstract_arg('Target Url'),
154+
service('router')->nullOnInvalid(),
155+
abstract_arg('Target Route'),
155156
])
156157
->tag('monolog.logger', ['channel' => 'security'])
157158

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testFirewalls()
165165
[
166166
'parameter' => '_switch_user',
167167
'role' => 'ROLE_ALLOWED_TO_SWITCH',
168-
'target_url' => null,
168+
'target_route' => null,
169169
],
170170
[
171171
'csrf_parameter' => '_csrf_token',

src/Symfony/Component/Security/Http/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)