• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 20135591630

11 Dec 2025 01:58PM UTC coverage: 25.294% (+0.002%) from 25.292%
20135591630

push

github

web-flow
feat(symfony): isGranted before provider (#7500)

35 of 41 new or added lines in 3 files covered. (85.37%)

193 existing lines in 6 files now uncovered.

14672 of 58005 relevant lines covered (25.29%)

29.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/src/Symfony/Security/State/AccessCheckerProvider.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\Security\State;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
18
use ApiPlatform\Metadata\GraphQl\QueryCollection;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
22
use ApiPlatform\State\ProviderInterface;
23
use ApiPlatform\Symfony\Security\Exception\AccessDeniedException;
24
use ApiPlatform\Symfony\Security\ObjectVariableCheckerInterface;
25
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
26

27
/**
28
 * Allows access based on the ApiPlatform\Metadata\ResourceAccessCheckerInterface.
29
 * This implementation covers GraphQl and HTTP.
30
 *
31
 * @see ResourceAccessCheckerInterface
32
 */
33
final class AccessCheckerProvider implements ProviderInterface
34
{
35
    public function __construct(private readonly ProviderInterface $decorated, private readonly ResourceAccessCheckerInterface $resourceAccessChecker, private readonly ?string $event = null)
36
    {
37
    }
768✔
38

39
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
40
    {
41
        switch ($this->event) {
735✔
42
            case 'post_denormalize':
735✔
43
                $isGranted = $operation->getSecurityPostDenormalize();
416✔
44
                $message = $operation->getSecurityPostDenormalizeMessage();
416✔
45
                break;
416✔
46
            case 'post_validate':
733✔
47
                $isGranted = $operation->getSecurityPostValidation();
695✔
48
                $message = $operation->getSecurityPostValidationMessage();
695✔
49
                break;
695✔
50
            case 'after_resolver':
731✔
51
                if (!$operation instanceof GraphQlOperation) {
26✔
52
                    throw new RuntimeException('Not a graphql operation');
×
53
                }
54

55
                $isGranted = $operation->getSecurityAfterResolver();
26✔
56
                $message = $operation->getSecurityMessageAfterResolver();
26✔
57
                break;
26✔
58
            default:
59
                $isGranted = $operation->getSecurity();
731✔
60
                $message = $operation->getSecurityMessage();
731✔
61
        }
62

63
        if (
64
            null === $isGranted
735✔
65
            // On a GraphQl QueryCollection we want to perform security stage only on the top-level query
66
            || ($operation instanceof QueryCollection && null !== ($context['source'] ?? null))
735✔
67
        ) {
68
            return $this->decorated->provide($operation, $uriVariables, $context);
723✔
69
        }
70

71
        $body = 'pre_read' === $this->event ? null : $this->decorated->provide($operation, $uriVariables, $context);
36✔
72

73
        if ($operation instanceof HttpOperation) {
36✔
74
            $request = $context['request'] ?? null;
26✔
75

76
            $resourceAccessCheckerContext = [
26✔
77
                'object' => $body,
26✔
78
                'previous_object' => $request?->attributes->get('previous_data'),
26✔
79
                'request' => $request,
26✔
80
            ];
26✔
81
        } else {
82
            $resourceAccessCheckerContext = [
10✔
83
                'object' => $body,
10✔
84
                'previous_object' => $context['graphql_context']['previous_object'] ?? null,
10✔
85
            ];
10✔
86
        }
87

88
        if ('pre_read' === $this->event && $this->resourceAccessChecker instanceof ObjectVariableCheckerInterface && $this->resourceAccessChecker->usesObjectVariable($isGranted, $resourceAccessCheckerContext)) {
36✔
NEW
89
            return $this->decorated->provide($operation, $uriVariables, $context);
×
90
        }
91

92
        if (!$this->resourceAccessChecker->isGranted($operation->getClass(), $isGranted, $resourceAccessCheckerContext)) {
36✔
93
            $operation instanceof GraphQlOperation ? throw new AccessDeniedHttpException($message ?? 'Access Denied.') : throw new AccessDeniedException($message ?? 'Access Denied.');
18✔
94
        }
95

96
        return 'pre_read' === $this->event ? $this->decorated->provide($operation, $uriVariables, $context) : $body;
18✔
97
    }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc