-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP] [Security] Fix #2172 User switching is not available for pre-authenticated users (oldest open bug!) #19059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6038f7
add functionnal tests
pasdeloup ee8528e
fix AbstractPreAuthenticatedListener to allow sharing other types of …
pasdeloup 6fb6a56
fix coding standards
pasdeloup 7b48ebb
fix array syntax to be compatible with PHP 5.3
pasdeloup e61a29f
fix coding standards
pasdeloup 123a1d7
improve logs: use directly , no need to recall getter
pasdeloup 1c3cb0b
fix code style
pasdeloup 9f56ae0
fix code style
pasdeloup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/Symfony/Bundle/SecurityBundle/Tests/Functional/SwitchUserPreAuthenticatedTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional; | ||
|
||
class SwitchUserPreAuthenticatedTest extends WebTestCase | ||
{ | ||
/** | ||
* @dataProvider getTestParameters | ||
*/ | ||
public function testSwitchUser($originalUser, $targetUser, $expectedUser, $expectedStatus) | ||
{ | ||
$client = $this->createAuthenticatedClient($originalUser); | ||
|
||
$client->request('GET', '/profile?_switch_user='.$targetUser); | ||
|
||
$this->assertEquals($expectedStatus, $client->getResponse()->getStatusCode()); | ||
$this->assertEquals($expectedUser, $client->getProfile()->getCollector('security')->getUser()); | ||
} | ||
|
||
public function testSwitchedUserCannotSwitchToOther() | ||
{ | ||
$client = $this->createAuthenticatedClient('user_can_switch'); | ||
|
||
$client->request('GET', '/profile?_switch_user=user_cannot_switch_1'); | ||
$client->request('GET', '/profile?_switch_user=user_cannot_switch_2'); | ||
|
||
$this->assertEquals(500, $client->getResponse()->getStatusCode()); | ||
$this->assertEquals('user_cannot_switch_1', $client->getProfile()->getCollector('security')->getUser()); | ||
} | ||
|
||
public function testSwitchedUserExit() | ||
{ | ||
$client = $this->createAuthenticatedClient('user_can_switch'); | ||
|
||
$client->request('GET', '/profile?_switch_user=user_cannot_switch_1'); | ||
$client->request('GET', '/profile?_switch_user=_exit'); | ||
|
||
$this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
$this->assertEquals('user_can_switch', $client->getProfile()->getCollector('security')->getUser()); | ||
} | ||
|
||
public function getTestParameters() | ||
{ | ||
return array( | ||
'unauthorized_user_cannot_switch' => array('user_cannot_switch_1', 'user_cannot_switch_1', 'user_cannot_switch_1', 403), | ||
'authorized_user_can_switch' => array('user_can_switch', 'user_cannot_switch_1', 'user_cannot_switch_1', 200), | ||
'authorized_user_cannot_switch_to_non_existent' => array('user_can_switch', 'user_does_not_exist', 'user_can_switch', 500), | ||
'authorized_user_can_switch_to_himself' => array('user_can_switch', 'user_can_switch', 'user_can_switch', 200), | ||
); | ||
} | ||
|
||
protected function createAuthenticatedClient($username) | ||
{ | ||
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'switchuser_preauthenticated.yml')); | ||
$client->followRedirects(true); | ||
|
||
$client->setServerParameters(array( | ||
'SSL_CLIENT_S_DN_Email' => $username, | ||
'SSL_CLIENT_S_DN' => 'test', | ||
)); | ||
|
||
return $client; | ||
} | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
parent::deleteTmpDir('StandardFormLogin'); | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
parent::deleteTmpDir('StandardFormLogin'); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...dle/SecurityBundle/Tests/Functional/app/StandardFormLogin/switchuser_preauthenticated.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
imports: | ||
- { resource: ./config.yml } | ||
|
||
security: | ||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
user_can_switch: { password: test, roles: [ROLE_USER, ROLE_ALLOWED_TO_SWITCH] } | ||
user_cannot_switch_1: { password: test, roles: [ROLE_USER] } | ||
user_cannot_switch_2: { password: test, roles: [ROLE_USER] } | ||
firewalls: | ||
default: | ||
switch_user: true | ||
form_login: false | ||
x509: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
parent::
and notself::
orstatic::
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the way all the tests have already been wrotten, I just kept the same syntax.
In this context, it's the same, and maybe the more obvious to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's convention.. i guess it's okay then.
From my personal POV i think it's just weird... why bypass the current class layer?