15
15
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
16
16
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
17
17
use Symfony \Component \Security \Acl \Model \SecurityIdentityRetrievalStrategyInterface ;
18
+ use Symfony \Component \Security \Core \Role \Role ;
18
19
use Symfony \Component \Security \Core \Role \RoleHierarchyInterface ;
19
20
use Symfony \Component \Security \Core \Authorization \Voter \AuthenticatedVoter ;
20
21
@@ -57,8 +58,16 @@ public function getSecurityIdentities(TokenInterface $token)
57
58
}
58
59
59
60
// add all reachable roles
60
- foreach ($ this ->roleHierarchy ->getReachableRoles ($ token ->getRoles ()) as $ role ) {
61
- $ sids [] = new RoleSecurityIdentity ($ role );
61
+ $ roles = $ this ->getRoleNames ($ token );
62
+ if (method_exists ($ this ->roleHierarchy , 'getReachableRoleNames ' )) {
63
+ foreach ($ this ->roleHierarchy ->getReachableRoleNames ($ roles ) as $ role ) {
64
+ $ sids [] = new RoleSecurityIdentity ($ role );
65
+ }
66
+ } else {
67
+ // Symfony < 4.3 BC layer
68
+ foreach ($ this ->roleHierarchy ->getReachableRoles ($ roles ) as $ role ) {
69
+ $ sids [] = new RoleSecurityIdentity ($ role );
70
+ }
62
71
}
63
72
64
73
// add built-in special roles
@@ -75,4 +84,16 @@ public function getSecurityIdentities(TokenInterface $token)
75
84
76
85
return $ sids ;
77
86
}
87
+
88
+ private function getRoleNames (TokenInterface $ token ): array
89
+ {
90
+ if (method_exists ($ token , 'getRoleNames ' )) {
91
+ return $ token ->getRoleNames ();
92
+ }
93
+
94
+ // Symfony < 4.3 BC layer
95
+ return array_map (function (Role $ role ) {
96
+ return $ role ->getRole ();
97
+ }, $ token ->getRoles ());
98
+ }
78
99
}
0 commit comments