Skip to content

Commit 17b551e

Browse files
committed
feature #22048 [Security] deprecate the Role and SwitchUserRole classes (xabbuh)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Security] deprecate the Role and SwitchUserRole classes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #20824 | License | MIT | Doc PR | symfony/symfony-docs#11047 In #20801, we deprecated the `RoleInterface`. The next logical step would be to also deprecate the `Role` class. However, we currently have the `SwitchUserRole` class (a sub-class of `Role`) that acts as an indicator to check whether or not the authenticated user switched to another user. This PR proposes an alternative solution to the usage of the special `SwitchUserRole` class by storing the original token inside the `UsernamePasswordToken`. This PR is not complete, but rather acts as a proof of concept of how we could get rid of the `Role` and the `SwitchUserRole` classes. Please share your opinions whether you think this is a valid approach and I will be happy to finalise the PR. Commits ------- d7aaa615b9 deprecate the Role and SwitchUserRole classes
2 parents 224b496 + 6992653 commit 17b551e

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)