AccessControlListener   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 22
c 2
b 0
f 0
dl 0
loc 46
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B onKernelRequest() 0 29 7
1
<?php
2
3
/**
4
 * User: Simon Libaud
5
 * Date: 19/03/2017
6
 * Email: [email protected].
7
 */
8
namespace Sil\RouteSecurityBundle\Listener;
9
10
use Sil\RouteSecurityBundle\Event\AccessDeniedToRouteEvent;
11
use Sil\RouteSecurityBundle\Exception\LogicException;
12
use Sil\RouteSecurityBundle\Security\AccessControl;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
use Symfony\Component\HttpKernel\Event\RequestEvent;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
19
/**
20
 * Class AccessControlListener.
21
 */
22
class AccessControlListener
23
{
24
    private $accessControl;
25
    private $tokenStorage;
26
    private $eventDispatcher;
27
28 10
    public function __construct(AccessControl $accessControl, TokenStorageInterface $tokenStorage, EventDispatcherInterface $eventDispatcher)
29
    {
30 10
        $this->accessControl = $accessControl;
31 10
        $this->tokenStorage = $tokenStorage;
32 10
        $this->eventDispatcher = $eventDispatcher;
33 10
    }
34
35
    /**
36
     * @param RequestEvent $event
37
     * @return RequestEvent|void
38
     */
39 10
    public function onKernelRequest(RequestEvent $event)
40
    {
41 10
        $route = $event->getRequest()->attributes->get('_route');
42
43 10
        if (false === $this->accessControl->isEnable() || false === $this->accessControl->isRouteSecure($route)) {
44 5
            return;
45
        }
46
47 5
        if (null === $this->tokenStorage->getToken()) {
48 1
            throw new LogicException('Unable to retrive the current user. The token storage does not contain security token.');
49
        }
50
51 4
        if (false === $this->tokenStorage->getToken()->getUser() instanceof UserInterface) {
52 1
            throw new LogicException(sprintf('The security token must containt an User object that implements %s', UserInterface::class));
53
        }
54
55 3
        $user = $this->tokenStorage->getToken()->getUser();
56
57 3
        if (false === $this->accessControl->hasUserAccessToRoute($user, $route)) {
58 2
            $access_denied_event = new AccessDeniedToRouteEvent($user, $event->getRequest());
59 2
            $this->eventDispatcher->dispatch($access_denied_event, AccessDeniedToRouteEvent::ON_ACCESS_DENIED_TO_ROUTE);
60
61 2
            if (true === $access_denied_event->hasResponse()) {
62 1
                $event->setResponse($access_denied_event->getResponse());
0 ignored issues
show
Bug introduced by
It seems like $access_denied_event->getResponse() can also be of type null; however, parameter $response of Symfony\Component\HttpKe...estEvent::setResponse() does only seem to accept Symfony\Component\HttpFoundation\Response, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
                $event->setResponse(/** @scrutinizer ignore-type */ $access_denied_event->getResponse());
Loading history...
63
64 1
                return $event;
65
            }
66
67 1
            throw new AccessDeniedException();
68
        }
69 1
    }
70
}
71