Skip to content

Commit d1a4c3f

Browse files
committed
Fixed Symfony 4.3 deprecated messages
1 parent 55b4b4f commit d1a4c3f

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

Domain/SecurityIdentityRetrievalStrategy.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1616
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1717
use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface;
18+
use Symfony\Component\Security\Core\Role\Role;
1819
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
1920
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
2021

@@ -57,8 +58,16 @@ public function getSecurityIdentities(TokenInterface $token)
5758
}
5859

5960
// 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+
}
6271
}
6372

6473
// add built-in special roles
@@ -75,4 +84,16 @@ public function getSecurityIdentities(TokenInterface $token)
7584

7685
return $sids;
7786
}
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+
}
7899
}

Tests/Domain/SecurityIdentityRetrievalStrategyTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,21 @@ public function testGetSecurityIdentities($user, array $roles, $authenticationSt
3838
->setMockClassName($class)
3939
->getMock();
4040
}
41-
$token
42-
->expects($this->once())
43-
->method('getRoles')
44-
->will($this->returnValue(array('foo')))
45-
;
41+
42+
if (method_exists($token, 'getRoleNames')) {
43+
$token
44+
->expects($this->once())
45+
->method('getRoleNames')
46+
->will($this->returnValue(array('foo')))
47+
;
48+
} else {
49+
$token
50+
->expects($this->once())
51+
->method('getRoles')
52+
->will($this->returnValue(array('foo')))
53+
;
54+
}
55+
4656
if ('anonymous' === $authenticationStatus) {
4757
$token
4858
->expects($this->never())

0 commit comments

Comments
 (0)