Skip to content

Commit a5f9f61

Browse files
authored
Merge pull request #2841 from teohhanhui/fix/build-symfony-4.3
Fix build with Symfony 4.3
2 parents 57d6788 + 061067e commit a5f9f61

File tree

7 files changed

+37
-16
lines changed

7 files changed

+37
-16
lines changed

.circleci/config.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,10 @@ jobs:
530530

531531
workflows:
532532
version: 2
533-
lint:
533+
lint-and-coverage:
534534
jobs:
535535
- php-cs-fixer
536536
- phpstan
537-
test-with-coverage:
538-
jobs:
539537
- phpunit-coverage
540538
- behat-coverage
541539
- phpunit-mongodb-coverage

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"symfony/framework-bundle": "^4.2",
7171
"symfony/mercure-bundle": "*",
7272
"symfony/messenger": "^4.2",
73-
"symfony/phpunit-bridge": "^3.4.5 || ^4.0.5",
73+
"symfony/phpunit-bridge": "^4.3",
7474
"symfony/routing": "^3.4 || ^4.0",
7575
"symfony/security-bundle": "^3.4 || ^4.0",
7676
"symfony/security-core": "^3.4 || ^4.0",
@@ -82,7 +82,8 @@
8282
},
8383
"conflict": {
8484
"doctrine/common": "<2.7",
85-
"doctrine/mongodb-odm": "<2.0"
85+
"doctrine/mongodb-odm": "<2.0",
86+
"symfony/messenger": ">=4.3"
8687
},
8788
"suggest": {
8889
"doctrine/mongodb-odm-bundle": "To support MongoDB. Only versions 4.0 and later are supported.",

phpstan.neon.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ parameters:
7474
-
7575
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and '(removeBindings|addRemovedBindingIds)' will always evaluate to false\\.#"
7676
path: %currentWorkingDirectory%/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php
77+
-
78+
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'getReachableRoleNam.+' will always evaluate to false\\.#"
79+
path: %currentWorkingDirectory%/tests/Security/EventListener/DenyAccessListenerTest.php
80+
-
81+
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'getRoleNames' will always evaluate to false\\.#"
82+
path: %currentWorkingDirectory%/tests/Security/EventListener/DenyAccessListenerTest.php
7783
- "#Call to method PHPUnit\\\\Framework\\\\Assert::assertSame\\(\\) with array\\('(collection_context|item_context|subresource_context)'\\) and array<Symfony\\\\Component\\\\VarDumper\\\\Cloner\\\\Data>\\|bool\\|float\\|int\\|string\\|null will always evaluate to false\\.#"
7884
# https://github.com/doctrine/doctrine2/pull/7298/files
7985
- '#Strict comparison using === between null and int will always evaluate to false\.#'

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<php>
1111
<ini name="error_reporting" value="-1" />
1212
<ini name="memory_limit" value="-1" />
13-
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak_vendors" />
13+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
1414
<server name="KERNEL_DIR" value="tests/Fixtures/app/" />
1515
<server name="KERNEL_CLASS" value="AppKernel" />
1616
<server name="APP_ENV" value="test" />

phpunit_mongodb.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<php>
1111
<ini name="error_reporting" value="-1" />
1212
<ini name="memory_limit" value="-1" />
13-
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak_vendors" />
13+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
1414
<server name="KERNEL_DIR" value="tests/Fixtures/app/" />
1515
<server name="KERNEL_CLASS" value="AppKernel" />
1616
<server name="APP_ENV" value="mongodb" />

src/Security/ResourceAccessChecker.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,31 @@ public function isGranted(string $resourceClass, string $expression, array $extr
6767
*/
6868
private function getVariables(TokenInterface $token): array
6969
{
70-
$roles = $this->roleHierarchy ? $this->roleHierarchy->getReachableRoles($token->getRoles()) : $token->getRoles();
71-
7270
return [
7371
'token' => $token,
7472
'user' => $token->getUser(),
75-
'roles' => array_map(function (Role $role) {
76-
return $role->getRole();
77-
}, $roles),
73+
'roles' => $this->getEffectiveRoles($token),
7874
'trust_resolver' => $this->authenticationTrustResolver,
7975
// needed for the is_granted expression function
8076
'auth_checker' => $this->authorizationChecker,
8177
];
8278
}
79+
80+
/**
81+
* @return string[]
82+
*/
83+
private function getEffectiveRoles(TokenInterface $token): array
84+
{
85+
if (null === $this->roleHierarchy) {
86+
return method_exists($token, 'getRoleNames') ? $token->getRoleNames() : array_map('strval', $token->getRoles());
87+
}
88+
89+
if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) {
90+
return $this->roleHierarchy->getReachableRoleNames($token->getRoleNames());
91+
}
92+
93+
return array_map(function (Role $role): string {
94+
return $role->getRole();
95+
}, $this->roleHierarchy->getReachableRoles($token->getRoles()));
96+
}
8397
}

tests/Security/EventListener/DenyAccessListenerTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
use Symfony\Component\HttpFoundation\Request;
2424
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
2525
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
26+
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
2627
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2728
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2829
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
2930
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
31+
use Symfony\Component\Security\Core\Role\RoleHierarchy;
3032
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
3133

3234
/**
@@ -269,14 +271,14 @@ private function getLegacyListener(ResourceMetadataFactoryInterface $resourceMet
269271
$authenticationTrustResolverProphecy = $this->prophesize(AuthenticationTrustResolverInterface::class);
270272

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

274-
$tokenProphecy = $this->prophesize(TokenInterface::class);
276+
$tokenProphecy = $this->prophesize(AbstractToken::class);
275277
$tokenProphecy->getUser()->willReturn('anon.');
276-
$tokenProphecy->getRoles()->willReturn([]);
278+
$tokenProphecy->{method_exists(AbstractToken::class, 'getRoleNames') ? 'getRoleNames' : 'getRoles'}()->willReturn([]);
277279

278280
$tokenStorageProphecy = $this->prophesize(TokenStorageInterface::class);
279-
$tokenStorageProphecy->getToken()->willReturn($tokenProphecy->reveal())->shouldBeCalled();
281+
$tokenStorageProphecy->getToken()->willReturn($tokenProphecy)->shouldBeCalled();
280282

281283
$authorizationCheckerInterface = $this->prophesize(AuthorizationCheckerInterface::class);
282284

0 commit comments

Comments
 (0)