Skip to content

fix(graphql): security after resolver #6444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions features/graphql/query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,16 @@ Feature: GraphQL query support
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.dummyDifferentGraphQlSerializationGroup.name" should be equal to "Name #1"
And the JSON node "data.dummyDifferentGraphQlSerializationGroup.title" should be equal to "Title #1"

Scenario: Call security after resolver
When I send the following GraphQL request:
"""
{
getSecurityAfterResolver(id: "/security_after_resolvers/1") {
name
}
}
"""
Then the response status code should be 200
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.getSecurityAfterResolver.name" should be equal to "test"
28 changes: 28 additions & 0 deletions src/Metadata/GraphQl/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function __construct(
protected ?array $extraArgs = null,
protected ?array $links = null,
protected ?bool $validateAfterResolver = null,
protected ?string $securityAfterResolver = null,
protected ?string $securityMessageAfterResolver = null,

?string $shortName = null,
?string $class = null,
Expand Down Expand Up @@ -209,4 +211,30 @@ public function withValidateAfterResolver(bool $validateAfterResolver = true): s

return $self;
}

public function getSecurityAfterResolver(): ?string
{
return $this->securityAfterResolver;
}

public function withSecurityAfterResolver(string $securityAfterResolver): self
{
$self = clone $this;
$self->securityAfterResolver = $securityAfterResolver;

return $self;
}

public function getSecurityMessageAfterResolver(): ?string
{
return $this->securityMessageAfterResolver;
}

public function withSecurityMessageAfterResolver(string $securityMessageAfterResolver): self
{
$self = clone $this;
$self->securityMessageAfterResolver = $securityMessageAfterResolver;

return $self;
}
}
4 changes: 4 additions & 0 deletions src/Metadata/GraphQl/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function __construct(
?array $args = null,
?array $extraArgs = null,
?array $links = null,
?string $securityAfterResolver = null,
?string $securityMessageAfterResolver = null,

?string $shortName = null,
?string $class = null,
Expand Down Expand Up @@ -79,6 +81,8 @@ public function __construct(
args: $args,
extraArgs: $extraArgs,
links: $links,
securityAfterResolver: $securityAfterResolver,
securityMessageAfterResolver: $securityMessageAfterResolver,
shortName: $shortName,
class: $class,
paginationEnabled: $paginationEnabled,
Expand Down
4 changes: 4 additions & 0 deletions src/Metadata/GraphQl/QueryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function __construct(
?array $args = null,
?array $extraArgs = null,
?array $links = null,
?string $securityAfterResolver = null,
?string $securityMessageAfterResolver = null,

?string $shortName = null,
?string $class = null,
Expand Down Expand Up @@ -80,6 +82,8 @@ public function __construct(
args: $args,
extraArgs: $extraArgs,
links: $links,
securityAfterResolver: $securityAfterResolver,
securityMessageAfterResolver: $securityMessageAfterResolver,
shortName: $shortName,
class: $class,
paginationEnabled: $paginationEnabled,
Expand Down
4 changes: 4 additions & 0 deletions src/Metadata/GraphQl/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function __construct(
?array $args = null,
?array $extraArgs = null,
?array $links = null,
?string $securityAfterResolver = null,
?string $securityMessageAfterResolver = null,

?string $shortName = null,
?string $class = null,
Expand Down Expand Up @@ -77,6 +79,8 @@ public function __construct(
args: $args,
extraArgs: $extraArgs,
links: $links,
securityAfterResolver: $securityAfterResolver,
securityMessageAfterResolver: $securityMessageAfterResolver,
shortName: $shortName,
class: $class,
paginationEnabled: $paginationEnabled,
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/Resources/config/graphql/security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@
<argument type="service" id="api_platform.security.resource_access_checker" />
<argument>post_validate</argument>
</service>

<service id="api_platform.graphql.state_provider.access_checker.after_resolver" class="ApiPlatform\Symfony\Security\State\AccessCheckerProvider" decorates="api_platform.graphql.state_provider" decoration-priority="170">
<argument type="service" id="api_platform.graphql.state_provider.access_checker.after_resolver.inner" />
<argument type="service" id="api_platform.security.resource_access_checker" />
<argument>after_resolver</argument>
</service>
</services>
</container>
9 changes: 9 additions & 0 deletions src/Symfony/Security/State/AccessCheckerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Symfony\Security\State;

use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use ApiPlatform\Metadata\HttpOperation;
Expand Down Expand Up @@ -45,6 +46,14 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$isGranted = $operation->getSecurityPostValidation();
$message = $operation->getSecurityPostValidationMessage();
break;
case 'after_resolver':
if (!$operation instanceof GraphQlOperation) {
throw new RuntimeException('Not a graphql operation');
}

$isGranted = $operation->getSecurityAfterResolver();
$message = $operation->getSecurityMessageAfterResolver();
// no break
default:
$isGranted = $operation->getSecurity();
$message = $operation->getSecurityMessage();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6427;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\Query;

#[ApiResource(
provider: [self::class, 'provide'],
graphQlOperations: [
new Query(
resolver: 'app.graphql.query_resolver.security_after_resolver',
securityAfterResolver: "object.name == 'test'",
name: 'get'
),
]
)]
class SecurityAfterResolver
{
public function __construct(public ?string $id, public ?string $name)
{
}

public static function provide()
{
return new self('1', '1');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6427;

use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;

final class SecurityAfterResolverResolver implements QueryItemResolverInterface
{
/**
* @param object|null $item
* @param mixed[] $context
*/
public function __invoke($item, array $context): SecurityAfterResolver
{
return new SecurityAfterResolver('1', 'test');
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,8 @@ services:
class: 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6354\CreateActivityLogResolver'
tags:
- name: 'api_platform.graphql.resolver'

app.graphql.query_resolver.security_after_resolver:
class: 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6427\SecurityAfterResolverResolver'
tags:
- name: 'api_platform.graphql.resolver'
Loading