Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitee219ef

Browse files
committed
[Security] Allow switching to another user when already switched
1 parentb3b368b commitee219ef

File tree

13 files changed

+109
-11
lines changed

13 files changed

+109
-11
lines changed

‎src/Symfony/Bundle/SecurityBundle/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Added security configuration for priority-based access decision strategy
8+
* Added`switch_user.allow_already_switched` option to allow switching seamlessly when already switched (default`false`)
89

910
5.0.0
1011
-----

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
238238
->scalarNode('provider')->end()
239239
->scalarNode('parameter')->defaultValue('_switch_user')->end()
240240
->scalarNode('role')->defaultValue('ROLE_ALLOWED_TO_SWITCH')->end()
241+
->booleanNode('allow_already_switched')->defaultFalse()->end()
241242
->end()
242243
->end()
243244
;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,10 @@ private function createSwitchUserListener(ContainerBuilder $container, string $i
684684
thrownewInvalidConfigurationException(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));
685685
}
686686

687+
if ($stateless &&$config['allow_already_switched']) {
688+
thrownewInvalidConfigurationException(sprintf('Cannot set "allow_already_switched" to true for the "switch_user" listener on firewall "%s" as it is stateless.',$id));
689+
}
690+
687691
$switchUserListenerId ='security.authentication.switchuser_listener.'.$id;
688692
$listener =$container->setDefinition($switchUserListenerId,newChildDefinition('security.authentication.switchuser_listener'));
689693
$listener->replaceArgument(1,newReference($userProvider));
@@ -692,6 +696,7 @@ private function createSwitchUserListener(ContainerBuilder $container, string $i
692696
$listener->replaceArgument(6,$config['parameter']);
693697
$listener->replaceArgument(7,$config['role']);
694698
$listener->replaceArgument(9,$stateless);
699+
$listener->replaceArgument(10,$config['allow_already_switched']);
695700

696701
return$switchUserListenerId;
697702
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
<argument>ROLE_ALLOWED_TO_SWITCH</argument>
203203
<argumenttype="service"id="event_dispatcher"on-invalid="null"/>
204204
<argument>false</argument><!-- Stateless-->
205+
<argument>false</argument><!-- Consecutive Switching-->
205206
</service>
206207

207208
<serviceid="security.access_listener"class="Symfony\Component\Security\Http\Firewall\AccessListener">

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function testFirewalls()
111111
[
112112
'parameter' =>'_switch_user',
113113
'role' =>'ROLE_ALLOWED_TO_SWITCH',
114+
'allow_already_switched' =>false,
114115
],
115116
],
116117
[

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/SwitchUserTest.php‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bundle\SecurityBundle\Tests\Functional;
1313

1414
useSymfony\Component\HttpFoundation\JsonResponse;
15+
useSymfony\Component\Security\Core\Exception\AlreadySwitchedException;
1516
useSymfony\Component\Security\Http\Firewall\SwitchUserListener;
1617

1718
class SwitchUserTestextends AbstractWebTestCase
@@ -36,8 +37,20 @@ public function testSwitchedUserCannotSwitchToOther()
3637
$client->request('GET','/profile?_switch_user=user_cannot_switch_1');
3738
$client->request('GET','/profile?_switch_user=user_cannot_switch_2');
3839

39-
$this->assertEquals(500,$client->getResponse()->getStatusCode());
40-
$this->assertEquals('user_cannot_switch_1',$client->getProfile()->getCollector('security')->getUser());
40+
$this->assertSame(403,$client->getResponse()->getStatusCode());
41+
$this->assertSame(AlreadySwitchedException::class,$client->getProfile()->getCollector('exception')->getException()->getPrevious()->getPrevious()->getClass());
42+
$this->assertSame('user_cannot_switch_1',$client->getProfile()->getCollector('security')->getUser());
43+
}
44+
45+
publicfunctiontestAlreadySwitchedUserCanSwitch()
46+
{
47+
$client =$this->createAuthenticatedClient('user_can_switch','switchuser_already_switched.yml');
48+
49+
$client->request('GET','/profile?_switch_user=user_cannot_switch_1');
50+
$client->request('GET','/profile?_switch_user=user_cannot_switch_2');
51+
52+
$this->assertSame(200,$client->getResponse()->getStatusCode());
53+
$this->assertSame('user_cannot_switch_2',$client->getProfile()->getCollector('security')->getUser());
4154
}
4255

4356
publicfunctiontestSwitchedUserExit()
@@ -73,9 +86,9 @@ public function getTestParameters()
7386
];
7487
}
7588

76-
protectedfunctioncreateAuthenticatedClient($username)
89+
protectedfunctioncreateAuthenticatedClient($username,string$rootConfig ='switchuser.yml')
7790
{
78-
$client =$this->createClient(['test_case' =>'StandardFormLogin','root_config' =>'switchuser.yml']);
91+
$client =$this->createClient(['test_case' =>'StandardFormLogin','root_config' =>$rootConfig]);
7992
$client->followRedirects(true);
8093

8194
$form =$client->request('GET','/login')->selectButton('login')->form();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
imports:
2+
-{ resource: ./config.yml }
3+
4+
security:
5+
providers:
6+
in_memory:
7+
memory:
8+
users:
9+
user_can_switch:{ password: test, roles: [ROLE_USER, ROLE_ALLOWED_TO_SWITCH] }
10+
user_cannot_switch_1:{ password: test, roles: [ROLE_USER] }
11+
user_cannot_switch_2:{ password: test, roles: [ROLE_USER] }
12+
firewalls:
13+
default:
14+
switch_user:{ allow_already_switched: true }

‎src/Symfony/Bundle/SecurityBundle/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"symfony/config":"^4.4|^5.0",
2222
"symfony/dependency-injection":"^4.4|^5.0",
2323
"symfony/http-kernel":"^5.0",
24-
"symfony/security-core":"^4.4|^5.0",
24+
"symfony/security-core":"^5.1",
2525
"symfony/security-csrf":"^4.4|^5.0",
2626
"symfony/security-guard":"^4.4|^5.0",
2727
"symfony/security-http":"^5.1"

‎src/Symfony/Component/Security/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Added access decision strategy to override access decisions by voter service priority
8+
* Added`bool $allowAlreadySwitched` argument to the`SwitchUserListener` constructor (default`false`)
89

910
5.0.0
1011
-----
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Security\Core\Exception;
13+
14+
/**
15+
* Thrown when trying to switch to another user while being already switched.
16+
*
17+
* @author Robin Chalas <robin.chalas@gmail.com>
18+
*/
19+
finalclass AlreadySwitchedExceptionextends AuthenticationException
20+
{
21+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp