Completed
Push — master ( aa6f89...863f7f )
by John
08:07
created

RequestAuthorizationListener::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\SwaggerBundle\Security;
9
10
11
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
12
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
15
16
/**
17
 * @author John Kleijn <[email protected]>
18
 */
19
class RequestAuthorizationListener implements ListenerInterface
20
{
21
    const ATTRIBUTE = 'swagger.request';
22
23
    /**
24
     * @var AuthorizationCheckerInterface
25
     */
26
    private $authorizationChecker;
27
28
    /**
29
     * @param AuthorizationCheckerInterface $authorizationChecker
30
     */
31
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
32
    {
33
        $this->authorizationChecker = $authorizationChecker;
34
    }
35
36
    /**
37
     * This interface must be implemented by firewall listeners.
38
     *
39
     * @param GetResponseEvent $event
40
     */
41
    public function handle(GetResponseEvent $event)
42
    {
43
        if (!$event->isMasterRequest()) {
44
            return;
45
        }
46
47
        if (!$this->authorizationChecker->isGranted(self::ATTRIBUTE, $event->getRequest())) {
48
            throw new AccessDeniedException();
49
        }
50
    }
51
}