Skip to content

Commit 54563cc

Browse files
reypmalanpoulain
authored andcommitted
Fixed dependency issue when SecurityBundle is not installed
1 parent 0f91a71 commit 54563cc

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

src/Bridge/Symfony/Bundle/Resources/config/graphql.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@
5353

5454
<service id="api_platform.graphql.resolver.stage.security" class="ApiPlatform\Core\GraphQl\Resolver\Stage\SecurityStage" public="false">
5555
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
56-
<argument type="service" id="api_platform.security.resource_access_checker" />
56+
<argument type="service" id="api_platform.security.resource_access_checker" on-invalid="ignore" />
5757
</service>
5858

5959
<service id="api_platform.graphql.resolver.stage.security_post_denormalize" class="ApiPlatform\Core\GraphQl\Resolver\Stage\SecurityPostDenormalizeStage" public="false">
6060
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
61-
<argument type="service" id="api_platform.security.resource_access_checker" />
61+
<argument type="service" id="api_platform.security.resource_access_checker" on-invalid="ignore" />
6262
</service>
6363

6464
<service id="api_platform.graphql.resolver.stage.serialize" class="ApiPlatform\Core\GraphQl\Resolver\Stage\SerializeStage" public="false">

src/GraphQl/Resolver/Stage/SecurityPostDenormalizeStage.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class SecurityPostDenormalizeStage implements SecurityPostDenormalizeStage
3030
private $resourceMetadataFactory;
3131
private $resourceAccessChecker;
3232

33-
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker)
33+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ?ResourceAccessCheckerInterface $resourceAccessChecker)
3434
{
3535
$this->resourceMetadataFactory = $resourceMetadataFactory;
3636
$this->resourceAccessChecker = $resourceAccessChecker;
@@ -44,6 +44,11 @@ public function __invoke(string $resourceClass, string $operationName, array $co
4444
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
4545

4646
$isGranted = $resourceMetadata->getGraphqlAttribute($operationName, 'security_post_denormalize', null, true);
47+
48+
if (null !== $isGranted && null === $this->resourceAccessChecker) {
49+
throw new \LogicException('Cannot check security expression when SecurityBundle is not installed. Try running "composer require symfony/security-bundle".');
50+
}
51+
4752
if (null === $isGranted) {
4853
// Backward compatibility
4954
$isGranted = $resourceMetadata->getGraphqlAttribute($operationName, 'access_control', null, true);

src/GraphQl/Resolver/Stage/SecurityStage.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class SecurityStage implements SecurityStageInterface
3030
private $resourceMetadataFactory;
3131
private $resourceAccessChecker;
3232

33-
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker)
33+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ?ResourceAccessCheckerInterface $resourceAccessChecker)
3434
{
3535
$this->resourceMetadataFactory = $resourceMetadataFactory;
3636
$this->resourceAccessChecker = $resourceAccessChecker;
@@ -44,6 +44,11 @@ public function __invoke(string $resourceClass, string $operationName, array $co
4444
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
4545

4646
$isGranted = $resourceMetadata->getGraphqlAttribute($operationName, 'security', null, true);
47+
48+
if (null !== $isGranted && null === $this->resourceAccessChecker) {
49+
throw new \LogicException('Cannot check security expression when SecurityBundle is not installed. Try running "composer require symfony/security-bundle".');
50+
}
51+
4752
if (null === $isGranted || $this->resourceAccessChecker->isGranted($resourceClass, (string) $isGranted, $context['extra_variables'])) {
4853
return;
4954
}

tests/GraphQl/Resolver/Stage/SecurityPostDenormalizeStageTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,28 @@ public function testNotGranted(): void
115115
'extra_variables' => $extraVariables,
116116
]);
117117
}
118+
119+
public function testNoSecurityBundleInstalled(): void
120+
{
121+
$operationName = 'item_query';
122+
$resourceClass = 'myResource';
123+
$isGranted = 'not_granted';
124+
$extraVariables = ['extra' => false];
125+
$resourceMetadata = (new ResourceMetadata())->withGraphql(
126+
[
127+
$operationName => ['security_post_denormalize' => $isGranted],
128+
]
129+
);
130+
$this->resourceMetadataFactoryProphecy->create($resourceClass)->willReturn($resourceMetadata);
131+
132+
$this->securityPostDenormalizeStage = new SecurityPostDenormalizeStage($this->resourceMetadataFactoryProphecy->reveal(), null);
133+
134+
$info = $this->prophesize(ResolveInfo::class)->reveal();
135+
$this->expectException(\LogicException::class);
136+
137+
($this->securityPostDenormalizeStage)($resourceClass,'item_query', [
138+
'info' => $info,
139+
'extra_variables' => $extraVariables,
140+
]);
141+
}
118142
}

tests/GraphQl/Resolver/Stage/SecurityStageTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,24 @@ public function testNotGranted(): void
9696
'extra_variables' => $extraVariables,
9797
]);
9898
}
99+
100+
public function testNoSecurityBundleInstalled(): void
101+
{
102+
$this->securityStage = new SecurityStage($this->resourceMetadataFactoryProphecy->reveal(), null);
103+
104+
$operationName = 'item_query';
105+
$resourceClass = 'myResource';
106+
$isGranted = 'not_granted';
107+
$extraVariables = ['extra' => false];
108+
$resourceMetadata = (new ResourceMetadata())->withGraphql([
109+
$operationName => ['security' => $isGranted],
110+
]);
111+
$this->resourceMetadataFactoryProphecy->create($resourceClass)->willReturn($resourceMetadata);
112+
113+
$this->expectException(\LogicException::class);
114+
115+
($this->securityStage)($resourceClass, 'item_query', [
116+
'extra_variables' => $extraVariables
117+
]);
118+
}
99119
}

0 commit comments

Comments
 (0)