Skip to content

Commit cb69c96

Browse files
Merge branch '4.4'
* 4.4: (39 commits) [Console] Fix #33915, Detect dimensions using mode CON if vt100 is supported [PhpUnitBridge] Also search for composer.phar in git root folder [HttpKernel][DataCollectorInterface] Ease compatibility Add tests to ensure defaultLocale is properly passed to the URL generator [DependencyInjection] Fix broken references in tests [VarDumper] display the method we're in when dumping stack traces [HttpClient] Retry safe requests when then fail before the body arrives [Console] Rename some methods related to redraw frequency Avoid using of kernel after shutdown Simplify PHP CS Fixer configuration [PropertyInfo] Fixed type extraction for nullable collections of non-nullable elements [FrameworkBundle] [HttpKernel] fixed correct EOL and EOM month Fix CS [Serializer] Fix property name usage for denormalization Name test accordingly to the tested class Fix MockFileSessionStorageTest::sessionDir being used after it's unset [Security] Fix SwitchUserToken wrongly deauthenticated Supporting Bootstrap 4 custom switches Add new Form WeekType bumped Symfony version to 4.3.7 ...
2 parents aaa2735 + 0477394 commit cb69c96

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Authentication/Token/AbstractToken.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,12 @@ private function hasUserChanged(UserInterface $user): bool
271271
}
272272

273273
$userRoles = array_map('strval', (array) $user->getRoles());
274-
$rolesChanged = \count($userRoles) !== \count($this->getRoleNames()) || \count($userRoles) !== \count(array_intersect($userRoles, $this->getRoleNames()));
275274

276-
if ($rolesChanged) {
275+
if ($this instanceof SwitchUserToken) {
276+
$userRoles[] = 'ROLE_PREVIOUS_ADMIN';
277+
}
278+
279+
if (\count($userRoles) !== \count($this->getRoleNames()) || \count($userRoles) !== \count(array_intersect($userRoles, $this->getRoleNames()))) {
277280
return true;
278281
}
279282

Tests/Authentication/Token/SwitchUserTokenTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
1616
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
17+
use Symfony\Component\Security\Core\User\UserInterface;
1718

1819
class SwitchUserTokenTest extends TestCase
1920
{
@@ -38,4 +39,38 @@ public function testSerialize()
3839
$this->assertSame('provider-key', $unserializedOriginalToken->getProviderKey());
3940
$this->assertEquals(['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH'], $unserializedOriginalToken->getRoleNames());
4041
}
42+
43+
public function testSetUserDoesNotDeauthenticate()
44+
{
45+
$impersonated = new class() implements UserInterface {
46+
public function getUsername()
47+
{
48+
return 'impersonated';
49+
}
50+
51+
public function getPassword()
52+
{
53+
return null;
54+
}
55+
56+
public function eraseCredentials()
57+
{
58+
}
59+
60+
public function getRoles()
61+
{
62+
return ['ROLE_USER'];
63+
}
64+
65+
public function getSalt()
66+
{
67+
return null;
68+
}
69+
};
70+
71+
$originalToken = new UsernamePasswordToken('impersonator', 'foo', 'provider-key', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']);
72+
$token = new SwitchUserToken($impersonated, 'bar', 'provider-key', ['ROLE_USER', 'ROLE_PREVIOUS_ADMIN'], $originalToken);
73+
$token->setUser($impersonated);
74+
$this->assertTrue($token->isAuthenticated());
75+
}
4176
}

0 commit comments

Comments
 (0)