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

api-platform / core / 15775135891

20 Jun 2025 08:42AM UTC coverage: 22.065% (+0.2%) from 21.876%
15775135891

push

github

soyuka
Merge 4.1

13 of 103 new or added lines in 10 files covered. (12.62%)

868 existing lines in 35 files now uncovered.

11487 of 52060 relevant lines covered (22.06%)

21.72 hits per line

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

90.7
/src/State/Provider/SecurityParameterProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\Exception\AccessDeniedException as MetadataAccessDeniedException;
17
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Link;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\Metadata\Parameters;
22
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
23
use ApiPlatform\State\ParameterNotFound;
24
use ApiPlatform\State\ProviderInterface;
25
use ApiPlatform\State\Util\ParameterParserTrait;
26
use ApiPlatform\Symfony\Security\Exception\AccessDeniedException;
27
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
28

29
/**
30
 * Loops over parameters to check parameter security.
31
 * Throws an exception if security is not granted.
32
 *
33
 * @experimental
34
 *
35
 * @implements ProviderInterface<object>
36
 */
37
final class SecurityParameterProvider implements ProviderInterface
38
{
39
    use ParameterParserTrait;
40

41
    /**
42
     * @param ProviderInterface<object> $decorated
43
     */
44
    public function __construct(private readonly ProviderInterface $decorated, private readonly ?ResourceAccessCheckerInterface $resourceAccessChecker = null)
45
    {
46
    }
489✔
47

48
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
49
    {
50
        $body = $this->decorated->provide($operation, $uriVariables, $context);
461✔
51
        $request = $context['request'] ?? null;
459✔
52

53
        $operation = $request?->attributes->get('_api_operation') ?? $operation;
459✔
54

55
        $parameters = $operation->getParameters() ?? new Parameters();
459✔
56

57
        if ($operation instanceof HttpOperation) {
459✔
58
            foreach ($operation->getUriVariables() ?? [] as $key => $uriVariable) {
459✔
59
                if ($uriVariable->getValue() instanceof ParameterNotFound) {
172✔
60
                    $uriVariable->setValue($uriVariables[$key] ?? new ParameterNotFound());
72✔
61
                }
62

63
                $parameters->add($key, $uriVariable->withKey($key));
172✔
64
            }
65
        }
66

67
        foreach ($parameters as $parameter) {
459✔
68
            $extraProperties = $parameter->getExtraProperties();
398✔
69
            if (null === $security = $parameter->getSecurity()) {
398✔
70
                continue;
388✔
71
            }
72

73
            $value = $parameter->getValue();
16✔
74
            if ($parameter instanceof Link) {
16✔
75
                $targetResource = $parameter->getFromClass() ?? $parameter->getToClass() ?? null;
2✔
76
            }
77

78
            if ($value instanceof ParameterNotFound) {
16✔
79
                continue;
12✔
80
            }
81

82
            $targetResource ??= $extraProperties['resource_class'] ?? $context['resource_class'] ?? null;
16✔
83

84
            if (!$targetResource) {
16✔
UNCOV
85
                continue;
×
86
            }
87

88
            $securityObjectName = null;
16✔
89
            if ($parameter instanceof Link) {
16✔
90
                $securityObjectName = $parameter->getSecurityObjectName() ?? $parameter->getToProperty() ?? $parameter->getFromProperty() ?? null;
2✔
91
            }
92

93
            $securityContext = [
16✔
94
                'object' => $body,
16✔
95
                'operation' => $operation,
16✔
96
                'previous_object' => $request?->attributes->get('previous_data'),
16✔
97
                'request' => $request,
16✔
98
                $parameter->getKey() => $value,
16✔
99
            ];
16✔
100

101
            if ($securityObjectName) {
16✔
102
                $securityContext[$securityObjectName] = $request?->attributes->get($securityObjectName);
2✔
103
            }
104

105
            if (!$this->resourceAccessChecker->isGranted($targetResource, $security, $securityContext)) {
16✔
106
                $exception = match (true) {
6✔
107
                    class_exists(MetadataAccessDeniedException::class, true) => MetadataAccessDeniedException::class,
6✔
UNCOV
108
                    $operation instanceof GraphQlOperation => AccessDeniedHttpException::class,
×
UNCOV
109
                    class_exists(AccessDeniedException::class, true) => AccessDeniedException::class,
×
UNCOV
110
                    default => AccessDeniedHttpException::class,
×
111
                };
6✔
112

113
                throw new ($exception)($parameter->getSecurityMessage() ?? 'Access Denied.');
6✔
114
            }
115
        }
116

117
        return $body;
459✔
118
    }
119
}
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