1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the OpenapiBundle package. |
7
|
|
|
* |
8
|
|
|
* (c) Niels Nijens <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nijens\OpenapiBundle\Validation\EventSubscriber; |
15
|
|
|
|
16
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\Exception\RequestProblemExceptionInterface; |
17
|
|
|
use Nijens\OpenapiBundle\Routing\RouteContext; |
18
|
|
|
use Nijens\OpenapiBundle\Validation\RequestValidator\ValidatorInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
22
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Validates a request for routes loaded through the OpenAPI specification. |
26
|
|
|
* |
27
|
|
|
* @author Niels Nijens <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class RequestValidationSubscriber implements EventSubscriberInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ValidatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $requestValidator; |
35
|
|
|
|
36
|
|
|
public static function getSubscribedEvents(): array |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
KernelEvents::REQUEST => [ |
40
|
|
|
['validateRequestBeforeFirewall', 10], |
41
|
|
|
['validateRequest', 7], |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function __construct(ValidatorInterface $requestValidator) |
47
|
|
|
{ |
48
|
|
|
$this->requestValidator = $requestValidator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function validateRequestBeforeFirewall(RequestEvent $event): void |
52
|
|
|
{ |
53
|
|
|
$request = $event->getRequest(); |
54
|
|
|
if ($this->isManagedRoute($request) === false || $this->isPreFirewallRequestValidationEnabled($request) === false) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->validateRequest($event); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function validateRequest(RequestEvent $event): void |
62
|
|
|
{ |
63
|
|
|
$request = $event->getRequest(); |
64
|
|
|
if ($this->isManagedRoute($request) === false) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$exception = $this->requestValidator->validate($request); |
69
|
|
|
if ($exception instanceof RequestProblemExceptionInterface) { |
70
|
|
|
throw $exception; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function isManagedRoute(Request $request): bool |
75
|
|
|
{ |
76
|
|
|
return $request->attributes->has(RouteContext::REQUEST_ATTRIBUTE); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function isPreFirewallRequestValidationEnabled(Request $request): bool |
80
|
|
|
{ |
81
|
|
|
$routeContext = $request->attributes->get(RouteContext::REQUEST_ATTRIBUTE); |
82
|
|
|
|
83
|
|
|
return $routeContext[RouteContext::REQUEST_VALIDATE_BEFORE_FIREWALL] ?? false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|