Skip to content

Commit 5534d44

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 7e745f7 + 5fd228b commit 5534d44

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Processor/TokenProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ public function __invoke(array $records)
3131
{
3232
$records['extra']['token'] = null;
3333
if (null !== $token = $this->tokenStorage->getToken()) {
34+
if (method_exists($token, 'getRoleNames')) {
35+
$roles = $token->getRoleNames();
36+
} else {
37+
$roles = array_map(function ($role) { return $role->getRole(); }, $token->getRoles(false));
38+
}
39+
3440
$records['extra']['token'] = [
3541
'username' => $token->getUsername(),
3642
'authenticated' => $token->isAuthenticated(),
37-
'roles' => array_map(function ($role) { return $role->getRole(); }, $token->getRoles()),
43+
'roles' => $roles,
3844
];
3945
}
4046

Tests/Processor/TokenProcessorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function testProcessor()
3636
$this->assertArrayHasKey('token', $record['extra']);
3737
$this->assertEquals($token->getUsername(), $record['extra']['token']['username']);
3838
$this->assertEquals($token->isAuthenticated(), $record['extra']['token']['authenticated']);
39-
$roles = array_map(function ($role) { return $role->getRole(); }, $token->getRoles());
40-
$this->assertEquals($roles, $record['extra']['token']['roles']);
39+
$this->assertEquals(['ROLE_USER'], $record['extra']['token']['roles']);
4140
}
4241
}

0 commit comments

Comments
 (0)