|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Voter deciding, if the provided object is |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Graviton\SecurityBundle\Voter; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
|
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
13
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
14
|
|
|
* @link http://swisscom.ch |
|
15
|
|
|
*/ |
|
16
|
|
|
class ServiceAllowedVoter extends Voter |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array List of services always allowed to be called. */ |
|
19
|
|
|
private $whitelist = array(); |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $whiteList Set of services to be allowed to be called. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($whiteList = array()) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->whitelist = $whiteList; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return an array of supported classes. This will be called by supportsClass |
|
32
|
|
|
* |
|
33
|
|
|
* @return array an array of supported classes, i.e. array('Acme\DemoBundle\Model\Product') |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getSupportedClasses() |
|
36
|
|
|
{ |
|
37
|
|
|
return array( |
|
38
|
|
|
'Symfony\Component\HttpFoundation\Request' |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Return an array of supported attributes. This will be called by supportsAttribute |
|
44
|
|
|
* |
|
45
|
|
|
* @return array an array of supported attributes, i.e. array('CREATE', 'READ') |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getSupportedAttributes() |
|
48
|
|
|
{ |
|
49
|
|
|
return array( |
|
50
|
|
|
'VIEW' |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Perform a single access check operation on a given attribute, object and (optionally) user |
|
56
|
|
|
* It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass |
|
57
|
|
|
* $user can be one of the following: |
|
58
|
|
|
* a UserInterface object (fully authenticated user) |
|
59
|
|
|
* a string (anonymously authenticated user) |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $attribute The attribute to be checked against. |
|
62
|
|
|
* @param object $object The object the access shall be granted for. |
|
63
|
|
|
* @param UserInterface|string $user The user asking for permission. |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function isGranted($attribute, $object, $user = null) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
return in_array($object->getPathInfo(), $this->whitelist); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* todo Implement new voter attribute since symfony 2.8 |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $attribute Object attribute |
|
76
|
|
|
* @param mixed $subject Subject to be supported |
|
77
|
|
|
* @param TokenInterface $token Token |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
|
82
|
|
|
{ |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $attribute To be supported |
|
88
|
|
|
* @return void |
|
89
|
|
|
* @throws \BadMethodCallException |
|
90
|
|
|
*/ |
|
91
|
|
|
public function supportsAttribute($attribute) |
|
92
|
|
|
{ |
|
93
|
|
|
$msg = 'supportsAttribute method is deprecated since version 2.8, to be removed in 3.0'; |
|
94
|
|
|
throw new \BadMethodCallException($msg); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $class to be supported |
|
99
|
|
|
* @return void |
|
100
|
|
|
* @throws \BadMethodCallException |
|
101
|
|
|
*/ |
|
102
|
|
|
public function supportsClass($class) |
|
103
|
|
|
{ |
|
104
|
|
|
$msg = 'supportsClass method is deprecated since version 2.8, to be removed in 3.0'; |
|
105
|
|
|
throw new \BadMethodCallException($msg); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Determines if the attribute and subject are supported by this voter. |
|
110
|
|
|
* todo implement voters |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $attribute An attribute |
|
113
|
|
|
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool True if the attribute and subject are supported, false otherwise |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function supports($attribute, $subject) |
|
118
|
|
|
{ |
|
119
|
|
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.