Skip to content

Commit

Permalink
Fix build with Symfony 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
teohhanhui committed Jun 4, 2019
1 parent 57d6788 commit 7616890
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
},
"conflict": {
"doctrine/common": "<2.7",
"doctrine/mongodb-odm": "<2.0"
"doctrine/mongodb-odm": "<2.0",
"symfony/messenger": ">=4.3"
},
"suggest": {
"doctrine/mongodb-odm-bundle": "To support MongoDB. Only versions 4.0 and later are supported.",
Expand Down
26 changes: 21 additions & 5 deletions src/Security/ResourceAccessChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,33 @@ public function isGranted(string $resourceClass, string $expression, array $extr
*/
private function getVariables(TokenInterface $token): array
{
$roles = $this->roleHierarchy ? $this->roleHierarchy->getReachableRoles($token->getRoles()) : $token->getRoles();

return [
'token' => $token,
'user' => $token->getUser(),
'roles' => array_map(function (Role $role) {
return $role->getRole();
}, $roles),
'roles' => $this->getEffectiveRoles($token),
'trust_resolver' => $this->authenticationTrustResolver,
// needed for the is_granted expression function
'auth_checker' => $this->authorizationChecker,
];
}

/**
* @return string[]
*/
private function getEffectiveRoles(TokenInterface $token): array
{
$explicitRoles = $token->getRoles();

if (null === $this->roleHierarchy) {
return $explicitRoles;
}

if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) {
return $this->roleHierarchy->getReachableRoleNames($explicitRoles);
}

return array_map(function (Role $role): string {
return $role->getRole();
}, $this->roleHierarchy->getReachableRoles($explicitRoles));
}
}
2 changes: 1 addition & 1 deletion tests/Security/EventListener/DenyAccessListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private function getLegacyListener(ResourceMetadataFactoryInterface $resourceMet
$authenticationTrustResolverProphecy = $this->prophesize(AuthenticationTrustResolverInterface::class);

$roleHierarchyInterfaceProphecy = $this->prophesize(RoleHierarchyInterface::class);
$roleHierarchyInterfaceProphecy->getReachableRoles(Argument::type('array'))->willReturn([]);
$roleHierarchyInterfaceProphecy->{method_exists(RoleHierarchyInterface::class, 'getReachableRoleNames') ? 'getReachableRoleNames' : 'getReachableRoles'}(Argument::type('array'))->willReturn([]);

$tokenProphecy = $this->prophesize(TokenInterface::class);
$tokenProphecy->getUser()->willReturn('anon.');
Expand Down

0 comments on commit 7616890

Please sign in to comment.