+ *
+ * @deprecated since version 4.3, to be removed in 5.0. Use strings as roles instead.
*/
class SwitchUserRole extends Role
{
+ private $deprecationTriggered = false;
private $source;
/**
@@ -29,7 +32,13 @@ class SwitchUserRole extends Role
*/
public function __construct(string $role, TokenInterface $source)
{
- parent::__construct($role);
+ if ($triggerDeprecation = \func_num_args() < 3 || func_get_arg(2)) {
+ @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3 and will be removed in 5.0. Use strings as roles instead.', __CLASS__), E_USER_DEPRECATED);
+
+ $this->deprecationTriggered = true;
+ }
+
+ parent::__construct($role, $triggerDeprecation);
$this->source = $source;
}
@@ -41,6 +50,12 @@ public function __construct(string $role, TokenInterface $source)
*/
public function getSource()
{
+ if (!$this->deprecationTriggered && (\func_num_args() < 1 || func_get_arg(0))) {
+ @trigger_error(sprintf('The "%s" class is deprecated since version 4.3 and will be removed in 5.0. Use strings as roles instead.', __CLASS__), E_USER_DEPRECATED);
+
+ $this->deprecationTriggered = true;
+ }
+
return $this->source;
}
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php
index a101208d681cc..d8d18ddeb9a42 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php
@@ -70,7 +70,7 @@ public function testAuthenticate()
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', $token);
$this->assertEquals('pass', $token->getCredentials());
$this->assertEquals('key', $token->getProviderKey());
- $this->assertEquals([], $token->getRoles());
+ $this->assertEquals([], $token->getRoleNames());
$this->assertEquals(['foo' => 'bar'], $token->getAttributes(), '->authenticate() copies token attributes');
$this->assertSame($user, $token->getUser());
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php
index 26287693d6b08..37d9a42a96319 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php
@@ -14,7 +14,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider;
use Symfony\Component\Security\Core\Exception\DisabledException;
-use Symfony\Component\Security\Core\Role\Role;
class RememberMeAuthenticationProviderTest extends TestCase
{
@@ -78,7 +77,7 @@ public function testAuthenticate()
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $authToken);
$this->assertSame($user, $authToken->getUser());
- $this->assertEquals([new Role('ROLE_FOO')], $authToken->getRoles());
+ $this->assertEquals(['ROLE_FOO'], $authToken->getRoleNames());
$this->assertEquals('', $authToken->getCredentials());
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php
index 7c306d2f1f0f6..e62ac3f9f5f29 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Provider/UserAuthenticationProviderTest.php
@@ -12,11 +12,11 @@
namespace Symfony\Component\Security\Core\Tests\Authentication\Provider;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
-use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
class UserAuthenticationProviderTest extends TestCase
@@ -189,11 +189,14 @@ public function testAuthenticate()
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $authToken);
$this->assertSame($user, $authToken->getUser());
- $this->assertEquals([new Role('ROLE_FOO')], $authToken->getRoles());
+ $this->assertEquals(['ROLE_FOO'], $authToken->getRoleNames());
$this->assertEquals('foo', $authToken->getCredentials());
$this->assertEquals(['foo' => 'bar'], $authToken->getAttributes(), '->authenticate() copies token attributes');
}
+ /**
+ * @group legacy
+ */
public function testAuthenticateWithPreservingRoleSwitchUserRole()
{
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
@@ -224,12 +227,40 @@ public function testAuthenticateWithPreservingRoleSwitchUserRole()
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $authToken);
$this->assertSame($user, $authToken->getUser());
- $this->assertContains(new Role('ROLE_FOO'), $authToken->getRoles(), '', false, false);
+ $this->assertContains('ROLE_FOO', $authToken->getRoleNames(), '', false, false);
$this->assertContains($switchUserRole, $authToken->getRoles(), '', false, false);
$this->assertEquals('foo', $authToken->getCredentials());
$this->assertEquals(['foo' => 'bar'], $authToken->getAttributes(), '->authenticate() copies token attributes');
}
+ public function testAuthenticatePreservesOriginalToken()
+ {
+ $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
+ $user->expects($this->once())
+ ->method('getRoles')
+ ->will($this->returnValue(['ROLE_FOO']))
+ ;
+
+ $provider = $this->getProvider();
+ $provider->expects($this->once())
+ ->method('retrieveUser')
+ ->will($this->returnValue($user))
+ ;
+
+ $originalToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
+ $token = new SwitchUserToken($this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(), 'foo', 'key', [], $originalToken);
+ $token->setAttributes(['foo' => 'bar']);
+
+ $authToken = $provider->authenticate($token);
+
+ $this->assertInstanceOf(SwitchUserToken::class, $authToken);
+ $this->assertSame($originalToken, $authToken->getOriginalToken());
+ $this->assertSame($user, $authToken->getUser());
+ $this->assertContains('ROLE_FOO', $authToken->getRoleNames(), '', false, false);
+ $this->assertEquals('foo', $authToken->getCredentials());
+ $this->assertEquals(['foo' => 'bar'], $authToken->getAttributes(), '->authenticate() copies token attributes');
+ }
+
protected function getSupportedToken()
{
$mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken')->setMethods(['getCredentials', 'getProviderKey', 'getRoles'])->disableOriginalConstructor()->getMock();
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php
index 066ca6892ce53..e87d25a789f1d 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php
@@ -14,61 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\Role\Role;
-use Symfony\Component\Security\Core\Role\SwitchUserRole;
use Symfony\Component\Security\Core\User\User;
-
-class TestUser
-{
- protected $name;
-
- public function __construct($name)
- {
- $this->name = $name;
- }
-
- public function __toString()
- {
- return $this->name;
- }
-}
-
-class ConcreteToken extends AbstractToken
-{
- private $credentials = 'credentials_value';
-
- public function __construct($user, array $roles = [])
- {
- parent::__construct($roles);
-
- $this->setUser($user);
- }
-
- /**
- * {@inheritdoc}
- */
- public function serialize()
- {
- $serialized = [$this->credentials, parent::serialize(true)];
-
- return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
- }
-
- public function unserialize($serialized)
- {
- list($this->credentials, $parentStr) = unserialize($serialized);
- parent::unserialize($parentStr);
- }
-
- public function getCredentials()
- {
- }
-}
+use Symfony\Component\Security\Core\User\UserInterface;
class AbstractTokenTest extends TestCase
{
public function testGetUsername()
{
- $token = $this->getToken(['ROLE_FOO']);
+ $token = new ConcreteToken(['ROLE_FOO']);
$token->setUser('fabien');
$this->assertEquals('fabien', $token->getUsername());
@@ -83,7 +36,7 @@ public function testGetUsername()
public function testEraseCredentials()
{
- $token = $this->getToken(['ROLE_FOO']);
+ $token = new ConcreteToken(['ROLE_FOO']);
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
$user->expects($this->once())->method('eraseCredentials');
@@ -94,19 +47,22 @@ public function testEraseCredentials()
public function testSerialize()
{
- $token = $this->getToken(['ROLE_FOO', new Role('ROLE_BAR')]);
+ $token = new ConcreteToken(['ROLE_FOO', new Role('ROLE_BAR', false)]);
$token->setAttributes(['foo' => 'bar']);
$uToken = unserialize(serialize($token));
- $this->assertEquals($token->getRoles(), $uToken->getRoles());
+ $this->assertEquals($token->getRoleNames(), $uToken->getRoleNames());
$this->assertEquals($token->getAttributes(), $uToken->getAttributes());
}
+ /**
+ * @group legacy
+ */
public function testSerializeWithRoleObjects()
{
$user = new User('name', 'password', [new Role('ROLE_FOO'), new Role('ROLE_BAR')]);
- $token = new ConcreteToken($user, $user->getRoles());
+ $token = new ConcreteToken($user->getRoles(), $user);
$serialized = serialize($token);
$unserialized = unserialize($serialized);
@@ -116,35 +72,42 @@ public function testSerializeWithRoleObjects()
$this->assertEquals($roles, $user->getRoles());
}
- public function testSerializeParent()
+ public function testConstructor()
{
- $user = new TestUser('fabien');
- $token = new ConcreteToken($user, ['ROLE_FOO']);
+ $token = new ConcreteToken(['ROLE_FOO']);
+ $this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
+ }
- $parentToken = new ConcreteToken($user, [new SwitchUserRole('ROLE_PREVIOUS', $token)]);
- $uToken = unserialize(serialize($parentToken));
+ /**
+ * @group legacy
+ */
+ public function testConstructorWithRoleObjects()
+ {
+ $token = new ConcreteToken([new Role('ROLE_FOO')]);
+ $this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
- $this->assertEquals(
- current($parentToken->getRoles())->getSource()->getUser(),
- current($uToken->getRoles())->getSource()->getUser()
- );
+ $token = new ConcreteToken([new Role('ROLE_FOO'), 'ROLE_BAR']);
+ $this->assertEquals(['ROLE_FOO', 'ROLE_BAR'], $token->getRoleNames());
}
- public function testConstructor()
+ /**
+ * @group legacy
+ */
+ public function testGetRoles()
{
- $token = $this->getToken(['ROLE_FOO']);
+ $token = new ConcreteToken(['ROLE_FOO']);
$this->assertEquals([new Role('ROLE_FOO')], $token->getRoles());
- $token = $this->getToken([new Role('ROLE_FOO')]);
+ $token = new ConcreteToken([new Role('ROLE_FOO')]);
$this->assertEquals([new Role('ROLE_FOO')], $token->getRoles());
- $token = $this->getToken([new Role('ROLE_FOO'), 'ROLE_BAR']);
+ $token = new ConcreteToken([new Role('ROLE_FOO'), 'ROLE_BAR']);
$this->assertEquals([new Role('ROLE_FOO'), new Role('ROLE_BAR')], $token->getRoles());
}
public function testAuthenticatedFlag()
{
- $token = $this->getToken();
+ $token = new ConcreteToken();
$this->assertFalse($token->isAuthenticated());
$token->setAuthenticated(true);
@@ -157,7 +120,7 @@ public function testAuthenticatedFlag()
public function testAttributes()
{
$attributes = ['foo' => 'bar'];
- $token = $this->getToken();
+ $token = new ConcreteToken();
$token->setAttributes($attributes);
$this->assertEquals($attributes, $token->getAttributes(), '->getAttributes() returns the token attributes');
@@ -181,7 +144,7 @@ public function testAttributes()
*/
public function testSetUser($user)
{
- $token = $this->getToken();
+ $token = new ConcreteToken();
$token->setUser($user);
$this->assertSame($user, $token->getUser());
}
@@ -202,7 +165,7 @@ public function getUsers()
*/
public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
{
- $token = $this->getToken();
+ $token = new ConcreteToken();
$token->setAuthenticated(true);
$this->assertTrue($token->isAuthenticated());
@@ -236,7 +199,7 @@ public function getUserChanges()
*/
public function testSetUserSetsAuthenticatedToFalseWhenUserChangesAdvancedUser($firstUser, $secondUser)
{
- $token = $this->getToken();
+ $token = new ConcreteToken();
$token->setAuthenticated(true);
$this->assertTrue($token->isAuthenticated());
@@ -275,7 +238,7 @@ public function getUserChangesAdvancedUser()
*/
public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($user)
{
- $token = $this->getToken();
+ $token = new ConcreteToken();
$token->setAuthenticated(true);
$this->assertTrue($token->isAuthenticated());
@@ -285,9 +248,48 @@ public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($
$token->setUser($user);
$this->assertTrue($token->isAuthenticated());
}
+}
+
+class TestUser
+{
+ protected $name;
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ public function __toString()
+ {
+ return $this->name;
+ }
+}
+
+class ConcreteToken extends AbstractToken
+{
+ private $credentials = 'credentials_value';
+
+ public function __construct(array $roles = [], UserInterface $user = null)
+ {
+ parent::__construct($roles);
+
+ if (null !== $user) {
+ $this->setUser($user);
+ }
+ }
- protected function getToken(array $roles = [])
+ public function serialize()
+ {
+ return serialize([$this->credentials, parent::serialize()]);
+ }
+
+ public function unserialize($serialized)
+ {
+ list($this->credentials, $parentStr) = unserialize($serialized);
+ parent::unserialize($parentStr);
+ }
+
+ public function getCredentials()
{
- return $this->getMockForAbstractClass('Symfony\Component\Security\Core\Authentication\Token\AbstractToken', [$roles]);
}
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AnonymousTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AnonymousTokenTest.php
index 7024cc5356cd4..1b00fd4e76aec 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AnonymousTokenTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AnonymousTokenTest.php
@@ -13,7 +13,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
-use Symfony\Component\Security\Core\Role\Role;
class AnonymousTokenTest extends TestCase
{
@@ -23,7 +22,7 @@ public function testConstructor()
$this->assertTrue($token->isAuthenticated());
$token = new AnonymousToken('foo', 'bar', ['ROLE_FOO']);
- $this->assertEquals([new Role('ROLE_FOO')], $token->getRoles());
+ $this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
}
public function testGetKey()
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php
index 7e64aa1a5d4e6..78cda619b2aff 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/PreAuthenticatedTokenTest.php
@@ -13,7 +13,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
-use Symfony\Component\Security\Core\Role\Role;
class PreAuthenticatedTokenTest extends TestCase
{
@@ -24,7 +23,7 @@ public function testConstructor()
$token = new PreAuthenticatedToken('foo', 'bar', 'key', ['ROLE_FOO']);
$this->assertTrue($token->isAuthenticated());
- $this->assertEquals([new Role('ROLE_FOO')], $token->getRoles());
+ $this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
$this->assertEquals('key', $token->getProviderKey());
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/RememberMeTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/RememberMeTokenTest.php
index 8fa0307108f54..fea6161d775a0 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/RememberMeTokenTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/RememberMeTokenTest.php
@@ -13,7 +13,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
-use Symfony\Component\Security\Core\Role\Role;
class RememberMeTokenTest extends TestCase
{
@@ -24,7 +23,7 @@ public function testConstructor()
$this->assertEquals('fookey', $token->getProviderKey());
$this->assertEquals('foo', $token->getSecret());
- $this->assertEquals([new Role('ROLE_FOO')], $token->getRoles());
+ $this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
$this->assertSame($user, $token->getUser());
$this->assertTrue($token->isAuthenticated());
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/Storage/TokenStorageTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/Storage/TokenStorageTest.php
index fd30eea3c57c7..43261b3bd2b60 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/Storage/TokenStorageTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/Storage/TokenStorageTest.php
@@ -13,6 +13,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class TokenStorageTest extends TestCase
{
@@ -20,7 +21,7 @@ public function testGetSetToken()
{
$tokenStorage = new TokenStorage();
$this->assertNull($tokenStorage->getToken());
- $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
+ $token = new UsernamePasswordToken('username', 'password', 'provider');
$tokenStorage->setToken($token);
$this->assertSame($token, $tokenStorage->getToken());
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/SwitchUserTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/SwitchUserTokenTest.php
new file mode 100644
index 0000000000000..5841250959b09
--- /dev/null
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/SwitchUserTokenTest.php
@@ -0,0 +1,41 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Core\Tests\Authentication\Token;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
+
+class SwitchUserTokenTest extends TestCase
+{
+ public function testSerialize()
+ {
+ $originalToken = new UsernamePasswordToken('user', 'foo', 'provider-key', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']);
+ $token = new SwitchUserToken('admin', 'bar', 'provider-key', ['ROLE_USER'], $originalToken);
+
+ $unserializedToken = unserialize(serialize($token));
+
+ $this->assertInstanceOf(SwitchUserToken::class, $unserializedToken);
+ $this->assertSame('admin', $unserializedToken->getUsername());
+ $this->assertSame('bar', $unserializedToken->getCredentials());
+ $this->assertSame('provider-key', $unserializedToken->getProviderKey());
+ $this->assertEquals(['ROLE_USER'], $unserializedToken->getRoleNames());
+
+ $unserializedOriginalToken = $unserializedToken->getOriginalToken();
+
+ $this->assertInstanceOf(UsernamePasswordToken::class, $unserializedOriginalToken);
+ $this->assertSame('user', $unserializedOriginalToken->getUsername());
+ $this->assertSame('foo', $unserializedOriginalToken->getCredentials());
+ $this->assertSame('provider-key', $unserializedOriginalToken->getProviderKey());
+ $this->assertEquals(['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH'], $unserializedOriginalToken->getRoleNames());
+ }
+}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php
index 87dceea3d8422..ab0abaf6530c9 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/UsernamePasswordTokenTest.php
@@ -13,7 +13,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
-use Symfony\Component\Security\Core\Role\Role;
class UsernamePasswordTokenTest extends TestCase
{
@@ -23,7 +22,7 @@ public function testConstructor()
$this->assertFalse($token->isAuthenticated());
$token = new UsernamePasswordToken('foo', 'bar', 'key', ['ROLE_FOO']);
- $this->assertEquals([new Role('ROLE_FOO')], $token->getRoles());
+ $this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
$this->assertTrue($token->isAuthenticated());
$this->assertEquals('key', $token->getProviderKey());
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/AuthorizationCheckerTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/AuthorizationCheckerTest.php
index 6a07c94f2039c..f2dcb6fbc3c18 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authorization/AuthorizationCheckerTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authorization/AuthorizationCheckerTest.php
@@ -13,6 +13,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
class AuthorizationCheckerTest extends TestCase
@@ -37,10 +38,10 @@ protected function setUp()
public function testVoteAuthenticatesTokenIfNecessary()
{
- $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
+ $token = new UsernamePasswordToken('username', 'password', 'provider');
$this->tokenStorage->setToken($token);
- $newToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
+ $newToken = new UsernamePasswordToken('username', 'password', 'provider');
$this->authenticationManager
->expects($this->once())
@@ -79,11 +80,7 @@ public function testVoteWithoutAuthenticationToken()
*/
public function testIsGranted($decide)
{
- $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
- $token
- ->expects($this->once())
- ->method('isAuthenticated')
- ->will($this->returnValue(true));
+ $token = new UsernamePasswordToken('username', 'password', 'provider', ['ROLE_USER']);
$this->accessDecisionManager
->expects($this->once())
diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php
index 632c6d0ab07ca..d377718842456 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php
@@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
@@ -20,6 +21,7 @@
class ExpressionVoterTest extends TestCase
{
/**
+ * @group legacy
* @dataProvider getVoteTests
*/
public function testVote($roles, $attributes, $expected, $tokenExpectsGetRoles = true, $expressionLanguageExpectsEvaluate = true)
@@ -29,6 +31,16 @@ public function testVote($roles, $attributes, $expected, $tokenExpectsGetRoles =
$this->assertSame($expected, $voter->vote($this->getToken($roles, $tokenExpectsGetRoles), null, $attributes));
}
+ /**
+ * @dataProvider getVoteTests
+ */
+ public function testVoteWithTokenThatReturnsRoleNames($roles, $attributes, $expected, $tokenExpectsGetRoles = true, $expressionLanguageExpectsEvaluate = true)
+ {
+ $voter = new ExpressionVoter($this->createExpressionLanguage($expressionLanguageExpectsEvaluate), $this->createTrustResolver(), $this->createAuthorizationChecker());
+
+ $this->assertSame($expected, $voter->vote($this->getTokenWithRoleNames($roles, $tokenExpectsGetRoles), null, $attributes));
+ }
+
public function getVoteTests()
{
return [
@@ -58,6 +70,19 @@ protected function getToken(array $roles, $tokenExpectsGetRoles = true)
return $token;
}
+ protected function getTokenWithRoleNames(array $roles, $tokenExpectsGetRoles = true)
+ {
+ $token = $this->getMockBuilder(AbstractToken::class)->getMock();
+
+ if ($tokenExpectsGetRoles) {
+ $token->expects($this->once())
+ ->method('getRoleNames')
+ ->will($this->returnValue($roles));
+ }
+
+ return $token;
+ }
+
protected function createExpressionLanguage($expressionLanguageExpectsEvaluate = true)
{
$mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\ExpressionLanguage')->getMock();
diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleHierarchyVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleHierarchyVoterTest.php
index 14705fbf960e5..ec21779a68ea8 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleHierarchyVoterTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleHierarchyVoterTest.php
@@ -18,6 +18,7 @@
class RoleHierarchyVoterTest extends RoleVoterTest
{
/**
+ * @group legacy
* @dataProvider getVoteTests
*/
public function testVote($roles, $attributes, $expected)
@@ -27,6 +28,16 @@ public function testVote($roles, $attributes, $expected)
$this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
}
+ /**
+ * @dataProvider getVoteTests
+ */
+ public function testVoteUsingTokenThatReturnsRoleNames($roles, $attributes, $expected)
+ {
+ $voter = new RoleHierarchyVoter(new RoleHierarchy(['ROLE_FOO' => ['ROLE_FOOBAR']]));
+
+ $this->assertSame($expected, $voter->vote($this->getTokenWithRoleNames($roles), null, $attributes));
+ }
+
public function getVoteTests()
{
return array_merge(parent::getVoteTests(), [
@@ -35,6 +46,18 @@ public function getVoteTests()
}
/**
+ * @group legacy
+ * @dataProvider getLegacyVoteOnRoleObjectsTests
+ */
+ public function testVoteOnRoleObjects($roles, $attributes, $expected)
+ {
+ $voter = new RoleHierarchyVoter(new RoleHierarchy(['ROLE_FOO' => ['ROLE_FOOBAR']]));
+
+ $this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
+ }
+
+ /**
+ * @group legacy
* @dataProvider getVoteWithEmptyHierarchyTests
*/
public function testVoteWithEmptyHierarchy($roles, $attributes, $expected)
@@ -44,6 +67,16 @@ public function testVoteWithEmptyHierarchy($roles, $attributes, $expected)
$this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
}
+ /**
+ * @dataProvider getVoteWithEmptyHierarchyTests
+ */
+ public function testVoteWithEmptyHierarchyUsingTokenThatReturnsRoleNames($roles, $attributes, $expected)
+ {
+ $voter = new RoleHierarchyVoter(new RoleHierarchy([]));
+
+ $this->assertSame($expected, $voter->vote($this->getTokenWithRoleNames($roles), null, $attributes));
+ }
+
public function getVoteWithEmptyHierarchyTests()
{
return parent::getVoteTests();
diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleVoterTest.php
index 5fb45e08b3925..6a1034417c837 100644
--- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleVoterTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/RoleVoterTest.php
@@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Role\Role;
@@ -19,6 +20,7 @@
class RoleVoterTest extends TestCase
{
/**
+ * @group legacy
* @dataProvider getVoteTests
*/
public function testVote($roles, $attributes, $expected)
@@ -28,6 +30,16 @@ public function testVote($roles, $attributes, $expected)
$this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
}
+ /**
+ * @dataProvider getVoteTests
+ */
+ public function testVoteUsingTokenThatReturnsRoleNames($roles, $attributes, $expected)
+ {
+ $voter = new RoleVoter();
+
+ $this->assertSame($expected, $voter->vote($this->getTokenWithRoleNames($roles), null, $attributes));
+ }
+
public function getVoteTests()
{
return [
@@ -41,6 +53,23 @@ public function getVoteTests()
// Test mixed Types
[[], [[]], VoterInterface::ACCESS_ABSTAIN],
[[], [new \stdClass()], VoterInterface::ACCESS_ABSTAIN],
+ ];
+ }
+
+ /**
+ * @group legacy
+ * @dataProvider getLegacyVoteOnRoleObjectsTests
+ */
+ public function testVoteOnRoleObjects($roles, $attributes, $expected)
+ {
+ $voter = new RoleVoter();
+
+ $this->assertSame($expected, $voter->vote($this->getToken($roles), null, $attributes));
+ }
+
+ public function getLegacyVoteOnRoleObjectsTests()
+ {
+ return [
[['ROLE_BAR'], [new Role('ROLE_BAR')], VoterInterface::ACCESS_GRANTED],
[['ROLE_BAR'], [new Role('ROLE_FOO')], VoterInterface::ACCESS_DENIED],
];
@@ -58,4 +87,14 @@ protected function getToken(array $roles)
return $token;
}
+
+ protected function getTokenWithRoleNames(array $roles)
+ {
+ $token = $this->getMockBuilder(AbstractToken::class)->getMock();
+ $token->expects($this->once())
+ ->method('getRoleNames')
+ ->will($this->returnValue($roles));
+
+ return $token;
+ }
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Role/RoleHierarchyTest.php b/src/Symfony/Component/Security/Core/Tests/Role/RoleHierarchyTest.php
index 4451f391adb30..c33fb953a1877 100644
--- a/src/Symfony/Component/Security/Core/Tests/Role/RoleHierarchyTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Role/RoleHierarchyTest.php
@@ -17,6 +17,9 @@
class RoleHierarchyTest extends TestCase
{
+ /**
+ * @group legacy
+ */
public function testGetReachableRoles()
{
$role = new RoleHierarchy([
@@ -30,4 +33,18 @@ public function testGetReachableRoles()
$this->assertEquals([new Role('ROLE_FOO'), new Role('ROLE_ADMIN'), new Role('ROLE_USER')], $role->getReachableRoles([new Role('ROLE_FOO'), new Role('ROLE_ADMIN')]));
$this->assertEquals([new Role('ROLE_SUPER_ADMIN'), new Role('ROLE_ADMIN'), new Role('ROLE_FOO'), new Role('ROLE_USER')], $role->getReachableRoles([new Role('ROLE_SUPER_ADMIN')]));
}
+
+ public function testGetReachableRoleNames()
+ {
+ $role = new RoleHierarchy(array(
+ 'ROLE_ADMIN' => array('ROLE_USER'),
+ 'ROLE_SUPER_ADMIN' => array('ROLE_ADMIN', 'ROLE_FOO'),
+ ));
+
+ $this->assertEquals(array('ROLE_USER'), $role->getReachableRoleNames(array('ROLE_USER')));
+ $this->assertEquals(array('ROLE_FOO'), $role->getReachableRoleNames(array('ROLE_FOO')));
+ $this->assertEquals(array('ROLE_ADMIN', 'ROLE_USER'), $role->getReachableRoleNames(array('ROLE_ADMIN')));
+ $this->assertEquals(array('ROLE_FOO', 'ROLE_ADMIN', 'ROLE_USER'), $role->getReachableRoleNames(array('ROLE_FOO', 'ROLE_ADMIN')));
+ $this->assertEquals(array('ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_FOO', 'ROLE_USER'), $role->getReachableRoleNames(array('ROLE_SUPER_ADMIN')));
+ }
}
diff --git a/src/Symfony/Component/Security/Core/Tests/Role/RoleTest.php b/src/Symfony/Component/Security/Core/Tests/Role/RoleTest.php
index edf779413b636..e872a8c36b4ab 100644
--- a/src/Symfony/Component/Security/Core/Tests/Role/RoleTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Role/RoleTest.php
@@ -14,6 +14,9 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Role\Role;
+/**
+ * @group legacy
+ */
class RoleTest extends TestCase
{
public function testGetRole()
diff --git a/src/Symfony/Component/Security/Core/Tests/Role/SwitchUserRoleTest.php b/src/Symfony/Component/Security/Core/Tests/Role/SwitchUserRoleTest.php
index e40471733ce11..88f6a18abf7d1 100644
--- a/src/Symfony/Component/Security/Core/Tests/Role/SwitchUserRoleTest.php
+++ b/src/Symfony/Component/Security/Core/Tests/Role/SwitchUserRoleTest.php
@@ -14,6 +14,9 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
+/**
+ * @group legacy
+ */
class SwitchUserRoleTest extends TestCase
{
public function testGetSource()
diff --git a/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php b/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php
index 9e3646da45ccb..ac792b6a8713a 100644
--- a/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php
+++ b/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php
@@ -12,7 +12,6 @@
namespace Symfony\Component\Security\Guard\Token;
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
-use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\UserInterface;
/**
@@ -28,9 +27,9 @@ class PostAuthenticationGuardToken extends AbstractToken implements GuardTokenIn
private $providerKey;
/**
- * @param UserInterface $user The user!
- * @param string $providerKey The provider (firewall) key
- * @param (Role|string)[] $roles An array of roles
+ * @param UserInterface $user The user!
+ * @param string $providerKey The provider (firewall) key
+ * @param string[] $roles An array of roles
*
* @throws \InvalidArgumentException
*/
diff --git a/src/Symfony/Component/Security/Http/Firewall/ContextListener.php b/src/Symfony/Component/Security/Http/Firewall/ContextListener.php
index c7a6dc96c88fd..e86dc5f2302ec 100644
--- a/src/Symfony/Component/Security/Http/Firewall/ContextListener.php
+++ b/src/Symfony/Component/Security/Http/Firewall/ContextListener.php
@@ -21,6 +21,7 @@
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
@@ -189,10 +190,14 @@ protected function refreshUser(TokenInterface $token)
if (null !== $this->logger) {
$context = ['provider' => \get_class($provider), 'username' => $refreshedUser->getUsername()];
- foreach ($token->getRoles() as $role) {
- if ($role instanceof SwitchUserRole) {
- $context['impersonator_username'] = $role->getSource()->getUsername();
- break;
+ if ($token instanceof SwitchUserToken) {
+ $context['impersonator_username'] = $token->getOriginalToken()->getUsername();
+ } else {
+ foreach ($token->getRoles(false) as $role) {
+ if ($role instanceof SwitchUserRole) {
+ $context['impersonator_username'] = $role->getSource(false)->getUsername();
+ break;
+ }
}
}
diff --git a/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php b/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php
index 4aff3b45811fa..102ab30615209 100644
--- a/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php
+++ b/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php
@@ -17,8 +17,8 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
-use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
@@ -122,7 +122,7 @@ private function attemptSwitchUser(Request $request, $username)
$token = $this->tokenStorage->getToken();
$originalToken = $this->getOriginalToken($token);
- if (false !== $originalToken) {
+ if (null !== $originalToken) {
if ($token->getUsername() === $username) {
return $token;
}
@@ -146,9 +146,9 @@ private function attemptSwitchUser(Request $request, $username)
$this->userChecker->checkPostAuth($user);
$roles = $user->getRoles();
- $roles[] = new SwitchUserRole('ROLE_PREVIOUS_ADMIN', $this->tokenStorage->getToken());
+ $roles[] = new SwitchUserRole('ROLE_PREVIOUS_ADMIN', $this->tokenStorage->getToken(), false);
- $token = new UsernamePasswordToken($user, $user->getPassword(), $this->providerKey, $roles);
+ $token = new SwitchUserToken($user, $user->getPassword(), $this->providerKey, $roles, $token);
if (null !== $this->dispatcher) {
$switchEvent = new SwitchUserEvent($request, $token->getUser(), $token);
@@ -169,7 +169,7 @@ private function attemptSwitchUser(Request $request, $username)
*/
private function attemptExitUser(Request $request)
{
- if (false === $original = $this->getOriginalToken($this->tokenStorage->getToken())) {
+ if (null === ($currentToken = $this->tokenStorage->getToken()) || null === $original = $this->getOriginalToken($currentToken)) {
throw new AuthenticationCredentialsNotFoundException('Could not find original Token object.');
}
@@ -183,19 +183,18 @@ private function attemptExitUser(Request $request)
return $original;
}
- /**
- * Gets the original Token from a switched one.
- *
- * @return TokenInterface|false The original TokenInterface instance, false if the current TokenInterface is not switched
- */
- private function getOriginalToken(TokenInterface $token)
+ private function getOriginalToken(TokenInterface $token): ?TokenInterface
{
- foreach ($token->getRoles() as $role) {
+ if ($token instanceof SwitchUserToken) {
+ return $token->getOriginalToken();
+ }
+
+ foreach ($token->getRoles(false) as $role) {
if ($role instanceof SwitchUserRole) {
return $role->getSource();
}
}
- return false;
+ return null;
}
}
diff --git a/src/Symfony/Component/Security/Http/Tests/Controller/UserValueResolverTest.php b/src/Symfony/Component/Security/Http/Tests/Controller/UserValueResolverTest.php
index 25108e482d287..8bcb960aa66ed 100644
--- a/src/Symfony/Component/Security/Http/Tests/Controller/UserValueResolverTest.php
+++ b/src/Symfony/Component/Security/Http/Tests/Controller/UserValueResolverTest.php
@@ -18,6 +18,7 @@
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Controller\UserValueResolver;
@@ -35,7 +36,7 @@ public function testResolveNoToken()
public function testResolveNoUser()
{
$mock = $this->getMockBuilder(UserInterface::class)->getMock();
- $token = $this->getMockBuilder(TokenInterface::class)->getMock();
+ $token = new UsernamePasswordToken('username', 'password', 'provider');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);
@@ -57,8 +58,7 @@ public function testResolveWrongType()
public function testResolve()
{
$user = $this->getMockBuilder(UserInterface::class)->getMock();
- $token = $this->getMockBuilder(TokenInterface::class)->getMock();
- $token->expects($this->any())->method('getUser')->willReturn($user);
+ $token = new UsernamePasswordToken($user, 'password', 'provider');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);
@@ -72,8 +72,7 @@ public function testResolve()
public function testIntegration()
{
$user = $this->getMockBuilder(UserInterface::class)->getMock();
- $token = $this->getMockBuilder(TokenInterface::class)->getMock();
- $token->expects($this->any())->method('getUser')->willReturn($user);
+ $token = new UsernamePasswordToken($user, 'password', 'provider');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);
@@ -83,7 +82,7 @@ public function testIntegration()
public function testIntegrationNoUser()
{
- $token = $this->getMockBuilder(TokenInterface::class)->getMock();
+ $token = new UsernamePasswordToken('username', 'password', 'provider');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);
diff --git a/src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php b/src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php
index f26b72fc148e1..1468df66126bf 100644
--- a/src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php
+++ b/src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php
@@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use Symfony\Component\Security\Core\User\User;
@@ -93,7 +94,7 @@ public function testExitUserThrowsAuthenticationExceptionIfOriginalTokenCannotBe
public function testExitUserUpdatesToken()
{
$originalToken = new UsernamePasswordToken('username', '', 'key', []);
- $this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', [new SwitchUserRole('ROLE_PREVIOUS', $originalToken)]));
+ $this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', [new SwitchUserRole('ROLE_PREVIOUS', $originalToken, false)], $originalToken));
$this->request->query->set('_switch_user', SwitchUserListener::EXIT_VALUE);
@@ -107,6 +108,22 @@ public function testExitUserUpdatesToken()
$this->assertSame($originalToken, $this->tokenStorage->getToken());
}
+ /**
+ * @group legacy
+ */
+ public function testExitUserBasedOnSwitchUserRoleUpdatesToken()
+ {
+ $originalToken = new UsernamePasswordToken('username', '', 'key', array());
+ $this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', array(new SwitchUserRole('ROLE_PREVIOUS', $originalToken, false)), $originalToken));
+
+ $this->request->query->set('_switch_user', SwitchUserListener::EXIT_VALUE);
+
+ $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
+ $listener->handle($this->event);
+
+ $this->assertSame($originalToken, $this->tokenStorage->getToken());
+ }
+
public function testExitUserDispatchesEventWithRefreshedUser()
{
$originalUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
@@ -118,7 +135,7 @@ public function testExitUserDispatchesEventWithRefreshedUser()
->with($originalUser)
->willReturn($refreshedUser);
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
- $this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', [new SwitchUserRole('ROLE_PREVIOUS', $originalToken)]));
+ $this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', [new SwitchUserRole('ROLE_PREVIOUS', $originalToken, false)], $originalToken));
$this->request->query->set('_switch_user', SwitchUserListener::EXIT_VALUE);
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
@@ -142,7 +159,7 @@ public function testExitUserDoesNotDispatchEventWithStringUser()
->expects($this->never())
->method('refreshUser');
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
- $this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', [new SwitchUserRole('ROLE_PREVIOUS', $originalToken)]));
+ $this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', [new SwitchUserRole('ROLE_PREVIOUS', $originalToken, false)], $originalToken));
$this->request->query->set('_switch_user', SwitchUserListener::EXIT_VALUE);
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
diff --git a/src/Symfony/Component/Security/Http/Tests/Logout/LogoutUrlGeneratorTest.php b/src/Symfony/Component/Security/Http/Tests/Logout/LogoutUrlGeneratorTest.php
index 727dde0f81b30..12166602474ba 100644
--- a/src/Symfony/Component/Security/Http/Tests/Logout/LogoutUrlGeneratorTest.php
+++ b/src/Symfony/Component/Security/Http/Tests/Logout/LogoutUrlGeneratorTest.php
@@ -95,7 +95,7 @@ public function testGuessFromCurrentFirewallContext()
public function testGuessFromTokenWithoutProviderKeyFallbacksToCurrentFirewall()
{
- $this->tokenStorage->setToken($this->getMockBuilder(TokenInterface::class)->getMock());
+ $this->tokenStorage->setToken(new UsernamePasswordToken('username', 'password', 'provider'));
$this->generator->registerListener('secured_area', '/logout', null, null);
$this->generator->setCurrentFirewall('secured_area');
diff --git a/src/Symfony/Component/Security/Http/composer.json b/src/Symfony/Component/Security/Http/composer.json
index 7047eba06e9e8..7bfac0fdcf85f 100644
--- a/src/Symfony/Component/Security/Http/composer.json
+++ b/src/Symfony/Component/Security/Http/composer.json
@@ -17,7 +17,7 @@
],
"require": {
"php": "^7.1.3",
- "symfony/security-core": "~3.4|~4.0",
+ "symfony/security-core": "~4.3",
"symfony/event-dispatcher": "~3.4|~4.0",
"symfony/http-foundation": "~3.4|~4.0",
"symfony/http-kernel": "~3.4|~4.0",
diff --git a/src/Symfony/Component/Workflow/EventListener/GuardListener.php b/src/Symfony/Component/Workflow/EventListener/GuardListener.php
index 3a9b417f67978..60cd00ed2084f 100644
--- a/src/Symfony/Component/Workflow/EventListener/GuardListener.php
+++ b/src/Symfony/Component/Workflow/EventListener/GuardListener.php
@@ -14,6 +14,8 @@
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
+use Symfony\Component\Security\Core\Role\Role;
+use Symfony\Component\Security\Core\Role\RoleHierarchy;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Workflow\Event\GuardEvent;
@@ -80,19 +82,23 @@ private function getVariables(GuardEvent $event): array
throw new InvalidTokenConfigurationException(sprintf('There are no tokens available for workflow %s.', $event->getWorkflowName()));
}
- if (null !== $this->roleHierarchy) {
- $roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
+ if (method_exists($token, 'getRoleNames')) {
+ $roles = $token->getRoleNames();
} else {
- $roles = $token->getRoles();
+ $roles = array_map(function (Role $role) { return $role->getRole(); }, $token->getRoles(false));
+ }
+
+ if ($this->roleHierarchy instanceof RoleHierarchy) {
+ $roles = $this->roleHierarchy->getReachableRoleNames($roles);
+ } elseif (null !== $this->roleHierarchy) {
+ $roles = $this->roleHierarchy->getReachableRoles($token->getRoles(false));
}
$variables = [
'token' => $token,
'user' => $token->getUser(),
'subject' => $event->getSubject(),
- 'roles' => array_map(function ($role) {
- return $role->getRole();
- }, $roles),
+ 'roles' => $roles,
// needed for the is_granted expression function
'auth_checker' => $this->authorizationChecker,
// needed for the is_* expression function
diff --git a/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php b/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php
index 64df594b235b8..8e7a9cb779749 100644
--- a/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php
+++ b/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php
@@ -6,6 +6,7 @@
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -35,8 +36,7 @@ protected function setUp()
],
];
$expressionLanguage = new ExpressionLanguage();
- $token = $this->getMockBuilder(TokenInterface::class)->getMock();
- $token->expects($this->any())->method('getRoles')->willReturn([new Role('ROLE_USER')]);
+ $token = new UsernamePasswordToken('username', 'credentials', 'provider', ['ROLE_USER']);
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
$tokenStorage->expects($this->any())->method('getToken')->willReturn($token);
$this->authenticationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy