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\RequestEvent; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
20
|
|
|
|
21
|
|
|
class AccessProtectorSubscriber implements EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
private AccessProtectorInterface $accessProtector; |
24
|
|
|
|
25
|
3 |
|
public function __construct(AccessProtectorInterface $accessProtector) |
26
|
|
|
{ |
27
|
3 |
|
$this->accessProtector = $accessProtector; |
28
|
3 |
|
} |
29
|
|
|
|
30
|
3 |
|
public static function getSubscribedEvents(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
RequestEvent::class => [ |
34
|
3 |
|
'onKernelRequest', |
35
|
|
|
0, |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws HttpException |
42
|
|
|
*/ |
43
|
3 |
|
public function onKernelRequest(RequestEvent $event) |
44
|
|
|
{ |
45
|
3 |
|
if (!$event->isMasterRequest()) { |
46
|
1 |
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$request = $event->getRequest(); |
50
|
|
|
|
51
|
2 |
|
$route = $request->get('_route', ''); |
52
|
2 |
|
$ip = $request->getClientIp() ?? ''; |
53
|
|
|
|
54
|
2 |
|
if ($this->isAccessProtected($route, $ip)) { |
55
|
1 |
|
throw new HttpException(Response::HTTP_NOT_FOUND, 'Not found'); |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
2 |
|
protected function isAccessProtected(string $route, string $ip): bool |
60
|
|
|
{ |
61
|
2 |
|
return $this->accessProtector->isRouteAccessProtected($route) |
62
|
2 |
|
&& !$this->accessProtector->isIpAccessAllowed($ip); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|