Skip to content

Commit da0aa94

Browse files
committed
undeprecate the RoleHierarchyInterface
Instead of deprecating the interface it is sufficient to deprecate its getReachableRoles() method and add a new getReachableRoleNames() method in Symfony 5.
1 parent b94ea96 commit da0aa94

File tree

4 files changed

+29
-43
lines changed

4 files changed

+29
-43
lines changed

Authorization/Voter/ExpressionVoter.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
2020
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
2121
use Symfony\Component\Security\Core\Role\Role;
22-
use Symfony\Component\Security\Core\Role\RoleHierarchy;
2322
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
2423

2524
/**
@@ -43,6 +42,10 @@ public function __construct(ExpressionLanguage $expressionLanguage, Authenticati
4342
@trigger_error(sprintf('Passing a RoleHierarchyInterface to "%s()" is deprecated since Symfony 4.2. Pass an AuthorizationCheckerInterface instead.', __METHOD__), E_USER_DEPRECATED);
4443
$roleHierarchy = $authChecker;
4544
$authChecker = null;
45+
46+
if (!method_exists($roleHierarchy, 'getReachableRoleNames')) {
47+
@trigger_error(sprintf('Not implementing the getReachableRoleNames() method in %s which implements %s is deprecated since Symfony 4.3.', \get_class($this->roleHierarchy), RoleHierarchyInterface::class), E_USER_DEPRECATED);
48+
}
4649
} elseif (null === $authChecker) {
4750
@trigger_error(sprintf('Argument 3 passed to "%s()" should be an instance of AuthorizationCheckerInterface, not passing it is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
4851
} elseif (!$authChecker instanceof AuthorizationCheckerInterface) {
@@ -92,34 +95,31 @@ public function vote(TokenInterface $token, $subject, array $attributes)
9295

9396
private function getVariables(TokenInterface $token, $subject)
9497
{
95-
if ($this->roleHierarchy instanceof RoleHierarchy) {
96-
if (method_exists($token, 'getRoleNames')) {
97-
$rolesFromToken = $token->getRoleNames();
98-
} else {
99-
@trigger_error(sprintf('Not implementing the getRoleNames() method in %s which implements %s is deprecated since Symfony 4.3.', \get_class($token), TokenInterface::class), E_USER_DEPRECATED);
100-
101-
$rolesFromToken = $token->getRoles(false);
102-
}
103-
104-
$roles = $this->roleHierarchy->getReachableRoleNames($rolesFromToken);
105-
} elseif (null !== $this->roleHierarchy) {
106-
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles(false));
107-
} elseif (method_exists($token, 'getRoleNames')) {
108-
$roles = $token->getRoleNames();
98+
if (method_exists($token, 'getRoleNames')) {
99+
$roleNames = $token->getRoleNames();
100+
$roles = array_map(function (string $role) { return new Role($role, false); }, $roleNames);
109101
} else {
110102
@trigger_error(sprintf('Not implementing the getRoleNames() method in %s which implements %s is deprecated since Symfony 4.3.', \get_class($token), TokenInterface::class), E_USER_DEPRECATED);
111103

112104
$roles = $token->getRoles(false);
105+
$roleNames = array_map(function (Role $role) { return $role->getRole(); }, $roles);
113106
}
114107

115-
$roles = array_map(function ($role) { return $role instanceof Role ? $role->getRole() : $role; }, $roles);
108+
if (null !== $this->roleHierarchy && method_exists($this->roleHierarchy, 'getReachableRoleNames')) {
109+
$roleNames = $this->roleHierarchy->getReachableRoleNames($roleNames);
110+
$roles = array_map(function (string $role) { return new Role($role, false); }, $roleNames);
111+
} elseif (null !== $this->roleHierarchy) {
112+
$roles = $this->roleHierarchy->getReachableRoles($roles);
113+
$roleNames = array_map(function (Role $role) { return $role->getRole(); }, $roles);
114+
}
116115

117116
$variables = [
118117
'token' => $token,
119118
'user' => $token->getUser(),
120119
'object' => $subject,
121120
'subject' => $subject,
122121
'roles' => $roles,
122+
'role_names' => $roleNames,
123123
'trust_resolver' => $this->trustResolver,
124124
'auth_checker' => $this->authChecker,
125125
];

Authorization/Voter/RoleHierarchyVoter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Authorization\Voter;
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\Role\Role;
1516
use Symfony\Component\Security\Core\Role\RoleHierarchy;
1617
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
1718

@@ -27,8 +28,8 @@ class RoleHierarchyVoter extends RoleVoter
2728

2829
public function __construct(RoleHierarchyInterface $roleHierarchy, string $prefix = 'ROLE_')
2930
{
30-
if (!$roleHierarchy instanceof RoleHierarchy) {
31-
@trigger_error(sprintf('Passing a role hierarchy to "%s" that is not an instance of "%s" is deprecated since Symfony 4.3 and support for it will be dropped in Symfony 5.0 ("%s" given).', __CLASS__, RoleHierarchy::class, \get_class($roleHierarchy)), E_USER_DEPRECATED);
31+
if (!method_exists($roleHierarchy, 'getReachableRoleNames')) {
32+
@trigger_error(sprintf('Not implementing the getReachableRoleNames() method in %s which implements %s is deprecated since Symfony 4.3.', \get_class($roleHierarchy), RoleHierarchyInterface::class), E_USER_DEPRECATED);
3233
}
3334

3435
$this->roleHierarchy = $roleHierarchy;
@@ -41,13 +42,13 @@ public function __construct(RoleHierarchyInterface $roleHierarchy, string $prefi
4142
*/
4243
protected function extractRoles(TokenInterface $token)
4344
{
44-
if ($this->roleHierarchy instanceof RoleHierarchy) {
45+
if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) {
4546
if (method_exists($token, 'getRoleNames')) {
4647
$roles = $token->getRoleNames();
4748
} else {
4849
@trigger_error(sprintf('Not implementing the getRoleNames() method in %s which implements %s is deprecated since Symfony 4.3.', \get_class($token), TokenInterface::class), E_USER_DEPRECATED);
4950

50-
$roles = $token->getRoles(false);
51+
$roles = array_map(function (Role $role) { return $role->getRole(); }, $token->getRoles(false));
5152
}
5253

5354
return $this->roleHierarchy->getReachableRoleNames($roles);

Role/RoleHierarchy.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public function __construct(array $hierarchy)
3636
*/
3737
public function getReachableRoles(array $roles)
3838
{
39-
@trigger_error(sprintf('The %s() method is deprecated since Symfony 4.3 and will be removed in 5.0. Use roles as strings and the getReachableRoleNames() method instead.', __METHOD__), E_USER_DEPRECATED);
39+
if (0 === \func_num_args() || func_get_arg(0)) {
40+
@trigger_error(sprintf('The %s() method is deprecated since Symfony 4.3 and will be removed in 5.0. Use roles as strings and the getReachableRoleNames() method instead.', __METHOD__), E_USER_DEPRECATED);
41+
}
4042

4143
$reachableRoles = $roles;
4244
foreach ($roles as $role) {
@@ -59,16 +61,7 @@ public function getReachableRoles(array $roles)
5961
*/
6062
public function getReachableRoleNames(array $roles): array
6163
{
62-
$reachableRoles = $roles = array_map(
63-
function ($role) {
64-
if ($role instanceof Role) {
65-
return $role->getRole();
66-
}
67-
68-
return $role;
69-
},
70-
$roles
71-
);
64+
$reachableRoles = $roles;
7265

7366
foreach ($roles as $role) {
7467
if (!isset($this->map[$role])) {

Role/RoleHierarchyInterface.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@
1414
/**
1515
* RoleHierarchyInterface is the interface for a role hierarchy.
1616
*
17+
* The getReachableRoles(Role[] $roles) method that returns an array of all reachable Role objects is deprecated
18+
* since Symfony 4.3.
19+
*
1720
* @author Fabien Potencier <[email protected]>
1821
*
19-
* @deprecated since Symfony 4.3, to be removed in 5.0.
22+
* @method string[] getReachableRoleNames(string[] $roles) The associated roles - not implementing it is deprecated since Symfony 4.3
2023
*/
2124
interface RoleHierarchyInterface
2225
{
23-
/**
24-
* Returns an array of all reachable roles by the given ones.
25-
*
26-
* Reachable roles are the roles directly assigned but also all roles that
27-
* are transitively reachable from them in the role hierarchy.
28-
*
29-
* @param Role[] $roles An array of directly assigned roles
30-
*
31-
* @return Role[] An array of all reachable roles
32-
*/
33-
public function getReachableRoles(array $roles);
3426
}

0 commit comments

Comments
 (0)