Skip to content

Commit dc8f10b

Browse files
bug symfony#51 Fixed Symfony 4.3 wrong role variable type (Andrej-in-ua)
This PR was merged into the 3.0-dev branch. Discussion ---------- Fixed Symfony 4.3 wrong role variable type This should fix work in symfony < 4.3 Original error from logs symfony 3.4 application: ``` Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Call to a member function getRole() on string" at vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php line 41 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function getRole() on string at vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php:41 ``` Found in symfony/security-acl#50 (comment) Commits ------- 489dc66 Fixed Symfony 4.3 wrong role variable type
2 parents 744c6d5 + 489dc66 commit dc8f10b

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

Domain/SecurityIdentityRetrievalStrategy.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ public function getSecurityIdentities(TokenInterface $token)
5858
}
5959

6060
// add all reachable roles
61-
$roles = $this->getRoleNames($token);
6261
if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) {
63-
foreach ($this->roleHierarchy->getReachableRoleNames($roles) as $role) {
62+
foreach ($this->roleHierarchy->getReachableRoleNames($this->getRoleNames($token)) as $role) {
6463
$sids[] = new RoleSecurityIdentity($role);
6564
}
6665
} else {
6766
// Symfony < 4.3 BC layer
68-
foreach ($this->roleHierarchy->getReachableRoles($roles) as $role) {
67+
foreach ($this->roleHierarchy->getReachableRoles($token->getRoles()) as $role) {
6968
$sids[] = new RoleSecurityIdentity($role);
7069
}
7170
}
@@ -85,7 +84,7 @@ public function getSecurityIdentities(TokenInterface $token)
8584
return $sids;
8685
}
8786

88-
private function getRoleNames(TokenInterface $token): array
87+
private function getRoleNames(TokenInterface $token)
8988
{
9089
if (method_exists($token, 'getRoleNames')) {
9190
return $token->getRoleNames();

Tests/Domain/SecurityIdentityRetrievalStrategyTest.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
1515
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
1616
use Symfony\Component\Security\Acl\Domain\SecurityIdentityRetrievalStrategy;
17+
use Symfony\Component\Security\Core\Role\Role;
1718

1819
class SecurityIdentityRetrievalStrategyTest extends \PHPUnit_Framework_TestCase
1920
{
@@ -22,8 +23,6 @@ class SecurityIdentityRetrievalStrategyTest extends \PHPUnit_Framework_TestCase
2223
*/
2324
public function testGetSecurityIdentities($user, array $roles, $authenticationStatus, array $sids)
2425
{
25-
$strategy = $this->getStrategy($roles, $authenticationStatus);
26-
2726
if ('anonymous' === $authenticationStatus) {
2827
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken')
2928
->disableOriginalConstructor()
@@ -40,16 +39,20 @@ public function testGetSecurityIdentities($user, array $roles, $authenticationSt
4039
}
4140

4241
if (method_exists($token, 'getRoleNames')) {
42+
$strategy = $this->getStrategy($roles, $authenticationStatus, false);
43+
4344
$token
4445
->expects($this->once())
4546
->method('getRoleNames')
4647
->will($this->returnValue(array('foo')))
4748
;
4849
} else {
50+
$strategy = $this->getStrategy($roles, $authenticationStatus, true);
51+
4952
$token
5053
->expects($this->once())
5154
->method('getRoles')
52-
->will($this->returnValue(array('foo')))
55+
->will($this->returnValue(array(new Role('foo'))))
5356
;
5457
}
5558

@@ -129,15 +132,32 @@ protected function getAccount($username, $class)
129132
return $account;
130133
}
131134

132-
protected function getStrategy(array $roles = array(), $authenticationStatus = 'fullFledged')
135+
protected function getStrategy(array $roles = array(), $authenticationStatus = 'fullFledged', $isBC = false)
133136
{
134-
$roleHierarchy = $this->getMock('Symfony\Component\Security\Core\Role\RoleHierarchyInterface');
135-
$roleHierarchy
136-
->expects($this->once())
137-
->method('getReachableRoles')
138-
->with($this->equalTo(array('foo')))
139-
->will($this->returnValue($roles))
140-
;
137+
$roleHierarchyBuilder = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface')
138+
->disableProxyingToOriginalMethods()
139+
->disableOriginalConstructor();
140+
141+
if ($isBC) {
142+
$roleHierarchy = $roleHierarchyBuilder->setMethods(['getReachableRoles'])
143+
->getMockForAbstractClass();
144+
145+
$roleHierarchy
146+
->expects($this->any())
147+
->method('getReachableRoles')
148+
->with($this->equalTo([new Role('foo')]))
149+
->will($this->returnValue($roles));
150+
} else {
151+
$roleHierarchy = $roleHierarchyBuilder->setMethods(['getReachableRoleNames'])
152+
->getMockForAbstractClass();
153+
154+
$roleHierarchy
155+
->expects($this->any())
156+
->method('getReachableRoleNames')
157+
->with($this->equalTo(['foo']))
158+
->will($this->returnValue($roles));
159+
}
160+
141161

142162
$trustResolver = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface', array(), array('', ''));
143163

0 commit comments

Comments
 (0)