|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2015 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SpomkyLabs\IpFilterBundle\Voter; |
|
13
|
|
|
|
|
14
|
|
|
use SpomkyLabs\IpFilterBundle\Tool\IpConverter; |
|
15
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
|
17
|
|
|
|
|
18
|
|
|
abstract class BaseIpVoter implements VoterInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @return \Symfony\Component\HttpFoundation\Request |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract protected function getRequest(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract protected function getEnvironment(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return \SpomkyLabs\IpFilterBundle\Model\IpManagerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract protected function getIpManager(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return \SpomkyLabs\IpFilterBundle\Model\RangeManagerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract protected function getRangeManager(); |
|
39
|
|
|
|
|
40
|
|
|
public function supportsAttribute($attribute) |
|
41
|
|
|
{ |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function supportsClass($class) |
|
46
|
|
|
{ |
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function vote(TokenInterface $token, $object, array $attributes) |
|
51
|
|
|
{ |
|
52
|
|
|
$request = $this->getRequest(); |
|
53
|
|
|
if (!$request) { |
|
54
|
|
|
throw new \Exception('No request found.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$from = IpConverter::fromIpToHex($request->getClientIp()); |
|
58
|
|
|
|
|
59
|
|
|
$env = $this->getEnvironment(); |
|
60
|
|
|
|
|
61
|
|
|
$ips = $this->getIpManager()->findIpAddress($from, $env); |
|
62
|
|
|
$ranges = $this->getRangeManager()->findByIpAddress($from, $env); |
|
63
|
|
|
|
|
64
|
|
|
if (count($ips) === 0 && count($ranges) === 0) { |
|
65
|
|
|
return VoterInterface::ACCESS_ABSTAIN; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($ips as $ip) { |
|
69
|
|
|
if ($ip->isAuthorized()) { |
|
70
|
|
|
return VoterInterface::ACCESS_GRANTED; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
foreach ($ranges as $range) { |
|
75
|
|
|
if ($range->isAuthorized()) { |
|
76
|
|
|
return VoterInterface::ACCESS_GRANTED; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return VoterInterface::ACCESS_DENIED; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|