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

api-platform / core / 14375359694

10 Apr 2025 08:01AM UTC coverage: 8.491% (+0.07%) from 8.425%
14375359694

push

github

web-flow
fix(hydra): use correctly enable_docs (#7062)

18 of 118 new or added lines in 5 files covered. (15.25%)

508 existing lines in 190 files now uncovered.

13401 of 157825 relevant lines covered (8.49%)

22.87 hits per line

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

97.3
/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\State\ProviderInterface;
22
use ApiPlatform\Symfony\Security\Exception\AccessDeniedException;
23
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
24
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
25

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

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

54
                $isGranted = $operation->getSecurityAfterResolver();
261✔
55
                $message = $operation->getSecurityMessageAfterResolver();
261✔
56
                break;
261✔
57
            default:
58
                $isGranted = $operation->getSecurity();
1,952✔
59
                $message = $operation->getSecurityMessage();
1,952✔
60
        }
61

62
        $body = $this->decorated->provide($operation, $uriVariables, $context);
1,956✔
63
        if (null === $isGranted) {
1,948✔
64
            return $body;
1,910✔
65
        }
66

67
        // On a GraphQl QueryCollection we want to perform security stage only on the top-level query
68
        if ($operation instanceof QueryCollection && null !== ($context['source'] ?? null)) {
145✔
UNCOV
69
            return $body;
4✔
70
        }
71

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

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

87
        if (!$this->resourceAccessChecker->isGranted($operation->getClass(), $isGranted, $resourceAccessCheckerContext)) {
145✔
88
            $operation instanceof GraphQlOperation ? throw new AccessDeniedHttpException($message ?? 'Access Denied.') : throw new AccessDeniedException($message ?? 'Access Denied.');
48✔
89
        }
90

91
        return $body;
101✔
92
    }
93
}
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

© 2025 Coveralls, Inc