1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/security package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2017 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Bundle\Components\Component\Security\Access\Protector\EventSubscriber; |
14
|
|
|
|
15
|
|
|
use Sauls\Bundle\Components\Component\Security\Access\Protector\AccessProtectorInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
20
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
21
|
|
|
|
22
|
|
|
class AccessProtectorSubscriber implements EventSubscriberInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var AccessProtectorInterface |
26
|
|
|
*/ |
27
|
|
|
private $accessProtector; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AccessProtectorSubscriber constructor. |
31
|
|
|
* |
32
|
|
|
* @param AccessProtectorInterface $accessProtector |
33
|
|
|
*/ |
34
|
3 |
|
public function __construct(AccessProtectorInterface $accessProtector) |
35
|
|
|
{ |
36
|
3 |
|
$this->accessProtector = $accessProtector; |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
42
|
|
|
* |
43
|
|
|
* The array keys are event names and the value can be: |
44
|
|
|
* |
45
|
|
|
* * The method name to call (priority defaults to 0) |
46
|
|
|
* * An array composed of the method name to call and the priority |
47
|
|
|
* * An array of arrays composed of the method names to call and respective |
48
|
|
|
* priorities, or 0 if unset |
49
|
|
|
* |
50
|
|
|
* For instance: |
51
|
|
|
* |
52
|
|
|
* * array('eventName' => 'methodName') |
53
|
|
|
* * array('eventName' => array('methodName', $priority)) |
54
|
|
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) |
55
|
|
|
* |
56
|
|
|
* @return array The event names to listen to |
57
|
|
|
*/ |
58
|
3 |
|
public static function getSubscribedEvents() |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
3 |
|
KernelEvents::REQUEST => [ |
62
|
|
|
'onKernelRequest', |
63
|
|
|
0, |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param GetResponseEvent $event |
70
|
|
|
* |
71
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
72
|
|
|
*/ |
73
|
3 |
|
public function onKernelRequest(GetResponseEvent $event) |
74
|
|
|
{ |
75
|
3 |
|
if (!$event->isMasterRequest()) { |
76
|
1 |
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
$request = $event->getRequest(); |
80
|
|
|
|
81
|
2 |
|
$route = $request->get('_route'); |
82
|
2 |
|
$ip = $request->getClientIp(); |
83
|
|
|
|
84
|
2 |
|
if ($this->isAccessProtected($route, $ip)) { |
85
|
1 |
|
throw new HttpException(Response::HTTP_NOT_FOUND, 'Not found'); |
86
|
|
|
} |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $route |
91
|
|
|
* @param string $ip |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
2 |
|
protected function isAccessProtected(string $route, string $ip): bool |
96
|
|
|
{ |
97
|
2 |
|
return ($this->accessProtector->isRouteAccessProtected($route) && !$this->accessProtector->isIpAccessAllowed($ip)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|