Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e89f0ff

Browse filesBrowse files
committed
feature #46338 [Security] Allow configuring a target url when switching user (94noni)
This PR was merged into the 6.2 branch. Discussion ---------- [Security] Allow configuring a target url when switching user | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | none | License | MIT | Doc PR | if accepted When using the [user switch](https://symfony.com/doc/current/security/impersonating_user.html) feature, I sometime found myself needing to redirect to a specific url (I took as example the logout target config) Tests will be checked as well as doc if PR is acceptable Thus I am proposing this feature, thank you, Commits ------- 2872b97 [Security] Allow configuring a target url when switching user
2 parents b4fe48a + 2872b97 commit e89f0ff
Copy full SHA for e89f0ff

File tree

4 files changed

+10
-2
lines changed
Filter options

4 files changed

+10
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
244244
->scalarNode('provider')->end()
245245
->scalarNode('parameter')->defaultValue('_switch_user')->end()
246246
->scalarNode('role')->defaultValue('ROLE_ALLOWED_TO_SWITCH')->end()
247+
->scalarNode('target_url')->defaultValue(null)->end()
247248
->end()
248249
->end()
249250
->arrayNode('required_badges')

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,9 @@ private function createSwitchUserListener(ContainerBuilder $container, string $i
843843
if (!$userProvider) {
844844
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));
845845
}
846+
if ($stateless && null !== $config['target_url']) {
847+
throw new InvalidConfigurationException(sprintf('Cannot set a "target_url" for the "switch_user" listener on the "%s" firewall as it is stateless.', $id));
848+
}
846849

847850
$switchUserListenerId = 'security.authentication.switchuser_listener.'.$id;
848851
$listener = $container->setDefinition($switchUserListenerId, new ChildDefinition('security.authentication.switchuser_listener'));
@@ -852,6 +855,7 @@ private function createSwitchUserListener(ContainerBuilder $container, string $i
852855
$listener->replaceArgument(6, $config['parameter']);
853856
$listener->replaceArgument(7, $config['role']);
854857
$listener->replaceArgument(9, $stateless);
858+
$listener->replaceArgument(10, $config['target_url']);
855859

856860
return $switchUserListenerId;
857861
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
'ROLE_ALLOWED_TO_SWITCH',
152152
service('event_dispatcher')->nullOnInvalid(),
153153
false, // Stateless
154+
abstract_arg('Target Url'),
154155
])
155156
->tag('monolog.logger', ['channel' => 'security'])
156157

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php
+4-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.