|
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; |
|
14
|
|
|
|
|
15
|
|
|
use function Sauls\Component\Helper\array_get_value; |
|
16
|
|
|
use Sauls\Bundle\Components\Component\Security\Access\Granter\Ip\IpGranterInterface; |
|
17
|
|
|
use Sauls\Bundle\Components\Component\Security\Access\Granter\String\StringGranterInterface; |
|
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
19
|
|
|
|
|
20
|
|
|
class AccessProtector implements AccessProtectorInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $options; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var StringGranterInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $stringGranter; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var IpGranterInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $ipGranter; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* RouteProtector constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $options |
|
39
|
|
|
* @param StringGranterInterface $stringGranter |
|
40
|
|
|
* @param IpGranterInterface $ipGranter |
|
41
|
|
|
*/ |
|
42
|
8 |
|
public function __construct( |
|
43
|
|
|
array $options = [], |
|
44
|
|
|
StringGranterInterface $stringGranter, |
|
45
|
|
|
IpGranterInterface $ipGranter |
|
46
|
|
|
) { |
|
47
|
8 |
|
$resolver = new OptionsResolver; |
|
48
|
8 |
|
$this->configureDefaults($resolver); |
|
49
|
|
|
|
|
50
|
8 |
|
$this->options = $resolver->resolve($options); |
|
51
|
8 |
|
$this->stringGranter = $stringGranter; |
|
52
|
8 |
|
$this->ipGranter = $ipGranter; |
|
53
|
8 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param OptionsResolver $resolver |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException |
|
59
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException |
|
60
|
|
|
*/ |
|
61
|
8 |
|
protected function configureDefaults(OptionsResolver $resolver): void |
|
62
|
|
|
{ |
|
63
|
8 |
|
$resolver->setRequired( |
|
64
|
|
|
[ |
|
65
|
8 |
|
'protected_routes', |
|
66
|
|
|
'allowed_ips', |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
8 |
|
$resolver->setAllowedTypes('protected_routes', ['null', 'array']); |
|
71
|
8 |
|
$resolver->setAllowedTypes('allowed_ips', ['null', 'array']); |
|
72
|
8 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $route |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
* @throws \Exception |
|
79
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
|
80
|
|
|
* @internal param string $value |
|
81
|
|
|
*/ |
|
82
|
5 |
|
public function isRouteAccessProtected(string $route): bool |
|
83
|
|
|
{ |
|
84
|
5 |
|
$routes = array_get_value($this->options, 'protected_routes'); |
|
85
|
|
|
|
|
86
|
5 |
|
if (!$routes) { |
|
87
|
1 |
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
return $this->stringGranter->isGranted($route, $routes); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $ip |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
* @throws \Exception |
|
98
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public function isIpAccessAllowed(string $ip): bool |
|
101
|
|
|
{ |
|
102
|
5 |
|
$ips = array_get_value($this->options, 'allowed_ips'); |
|
103
|
|
|
|
|
104
|
5 |
|
if (!$ips) { |
|
105
|
1 |
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
return $this->ipGranter->isGranted($ip, $ips); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|