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

api-platform / core / 8297491803

15 Mar 2024 02:07PM UTC coverage: 66.739% (-0.002%) from 66.741%
8297491803

push

github

web-flow
fix(serializer): skip symfony validation exception (#6220)

fixes #6216

2 of 2 new or added lines in 1 file covered. (100.0%)

87 existing lines in 9 files now uncovered.

16355 of 24506 relevant lines covered (66.74%)

42.35 hits per line

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

97.14
/src/Symfony/EventListener/DenyAccessListener.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\EventListener;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
18
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
19
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Event\RequestEvent;
22
use Symfony\Component\HttpKernel\Event\ViewEvent;
23
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
24

25
/**
26
 * Denies access to the current resource if the logged user doesn't have sufficient permissions.
27
 *
28
 * @author Kévin Dunglas <dunglas@gmail.com>
29
 */
30
final class DenyAccessListener
31
{
32
    use OperationRequestInitiatorTrait;
33

34
    public function __construct(?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, private readonly ?ResourceAccessCheckerInterface $resourceAccessChecker = null)
35
    {
36
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
140✔
37
    }
38

39
    public function onSecurity(RequestEvent $event): void
40
    {
41
        $this->checkSecurity($event->getRequest(), 'security');
140✔
42
    }
43

44
    public function onSecurityPostDenormalize(RequestEvent $event): void
45
    {
46
        $request = $event->getRequest();
116✔
47
        $this->checkSecurity($request, 'security_post_denormalize', [
116✔
48
            'previous_object' => $request->attributes->get('previous_data'),
116✔
49
        ]);
116✔
50
    }
51

52
    public function onSecurityPostValidation(ViewEvent $event): void
53
    {
UNCOV
54
        $request = $event->getRequest();
17✔
UNCOV
55
        $this->checkSecurity($request, 'security_post_validation', [
17✔
UNCOV
56
            'previous_object' => $request->attributes->get('previous_data'),
17✔
UNCOV
57
        ]);
17✔
58
    }
59

60
    /**
61
     * @throws AccessDeniedException
62
     */
63
    private function checkSecurity(Request $request, string $attribute, array $extraVariables = []): void
64
    {
65
        if ($request->attributes->get('_api_platform_disable_listeners') || !$this->resourceAccessChecker || !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
140✔
66
            return;
64✔
67
        }
68

69
        $operation = $this->initializeOperation($request);
87✔
70
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
87✔
71
            return;
54✔
72
        }
73

74
        if (!$operation) {
33✔
75
            return;
×
76
        }
77

78
        switch ($attribute) {
79
            case 'security_post_denormalize':
33✔
UNCOV
80
                $isGranted = $operation->getSecurityPostDenormalize();
17✔
UNCOV
81
                $message = $operation->getSecurityPostDenormalizeMessage();
17✔
UNCOV
82
                break;
17✔
83
            case 'security_post_validation':
33✔
UNCOV
84
                $isGranted = $operation->getSecurityPostValidation();
17✔
UNCOV
85
                $message = $operation->getSecurityPostValidationMessage();
17✔
UNCOV
86
                break;
17✔
87
            default:
88
                $isGranted = $operation->getSecurity();
33✔
89
                $message = $operation->getSecurityMessage();
33✔
90
        }
91

92
        if (null === $isGranted) {
33✔
93
            return;
21✔
94
        }
95

96
        $extraVariables += $request->attributes->all();
16✔
97
        $extraVariables['object'] = $request->attributes->get('data');
16✔
98
        $extraVariables['previous_object'] = $request->attributes->get('previous_data');
16✔
99
        $extraVariables['request'] = $request;
16✔
100

101
        if (!$this->resourceAccessChecker->isGranted($attributes['resource_class'], $isGranted, $extraVariables)) {
16✔
102
            throw new AccessDeniedException($message ?? 'Access Denied.');
8✔
103
        }
104
    }
105
}
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