Skip to content

Commit 6992653

Browse files
committed
deprecate the Role and SwitchUserRole classes
1 parent 29ba17b commit 6992653

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

EventListener/GuardListener.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
1515
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1616
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
17+
use Symfony\Component\Security\Core\Role\Role;
18+
use Symfony\Component\Security\Core\Role\RoleHierarchy;
1719
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
1820
use Symfony\Component\Validator\Validator\ValidatorInterface;
1921
use Symfony\Component\Workflow\Event\GuardEvent;
@@ -80,19 +82,23 @@ private function getVariables(GuardEvent $event): array
8082
throw new InvalidTokenConfigurationException(sprintf('There are no tokens available for workflow %s.', $event->getWorkflowName()));
8183
}
8284

83-
if (null !== $this->roleHierarchy) {
84-
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
85+
if (method_exists($token, 'getRoleNames')) {
86+
$roles = $token->getRoleNames();
8587
} else {
86-
$roles = $token->getRoles();
88+
$roles = array_map(function (Role $role) { return $role->getRole(); }, $token->getRoles(false));
89+
}
90+
91+
if ($this->roleHierarchy instanceof RoleHierarchy) {
92+
$roles = $this->roleHierarchy->getReachableRoleNames($roles);
93+
} elseif (null !== $this->roleHierarchy) {
94+
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles(false));
8795
}
8896

8997
$variables = [
9098
'token' => $token,
9199
'user' => $token->getUser(),
92100
'subject' => $event->getSubject(),
93-
'roles' => array_map(function ($role) {
94-
return $role->getRole();
95-
}, $roles),
101+
'roles' => $roles,
96102
// needed for the is_granted expression function
97103
'auth_checker' => $this->authorizationChecker,
98104
// needed for the is_* expression function

Tests/EventListener/GuardListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
77
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
88
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
910
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1011
use Symfony\Component\Security\Core\Role\Role;
1112
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -35,8 +36,7 @@ protected function setUp()
3536
],
3637
];
3738
$expressionLanguage = new ExpressionLanguage();
38-
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
39-
$token->expects($this->any())->method('getRoles')->willReturn([new Role('ROLE_USER')]);
39+
$token = new UsernamePasswordToken('username', 'credentials', 'provider', ['ROLE_USER']);
4040
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
4141
$tokenStorage->expects($this->any())->method('getToken')->willReturn($token);
4242
$this->authenticationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();

0 commit comments

Comments
 (0)