|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Voter deciding, if the provided object is |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Graviton\SecurityBundle\Voter; |
|
6
|
|
|
|
|
7
|
|
|
use GravitonDyn\ContractBundle\Document\Contract; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
|
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
15
|
|
|
* @link http://swisscom.ch |
|
16
|
|
|
*/ |
|
17
|
|
|
class OwnContextVoter extends Voter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Return an array of supported classes. This will be called by supportsClass |
|
21
|
|
|
* |
|
22
|
|
|
* @return array an array of supported classes, i.e. array('Acme\DemoBundle\Model\Product') |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function getSupportedClasses() |
|
25
|
|
|
{ |
|
26
|
|
|
return array( |
|
27
|
|
|
'GravitonDyn\AccountBundle\Document\Account', |
|
28
|
|
|
'GravitonDyn\CustomerBundle\Document\Customer', |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Return an array of supported attributes. This will be called by supportsAttribute |
|
34
|
|
|
* |
|
35
|
|
|
* @return array an array of supported attributes, i.e. array('CREATE', 'READ') |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function getSupportedAttributes() |
|
38
|
|
|
{ |
|
39
|
|
|
return array( |
|
40
|
|
|
'VIEW', |
|
41
|
|
|
'CREATE', |
|
42
|
|
|
'EDIT', |
|
43
|
|
|
'DELETE', |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Perform a single access check operation on a given attribute, object and (optionally) user |
|
49
|
|
|
* It is safe to assume that $attribute and $object's class pass supportsAttribute/supportsClass |
|
50
|
|
|
* $user can be one of the following: |
|
51
|
|
|
* a UserInterface object (fully authenticated user) |
|
52
|
|
|
* a string (anonymously authenticated user) |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $attribute The attribute to be checked against. |
|
55
|
|
|
* @param object $object The object the access shall be granted for. |
|
56
|
|
|
* @param UserInterface|string $user The user asking for permission. |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function isGranted($attribute, $object, $user = null) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
if (null === $user || !($user instanceof \Graviton\SecurityBundle\Entities\SecurityContract)) { |
|
|
|
|
|
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @var \GravitonDyn\ContractBundle\Document\Contract $contract */ |
|
67
|
|
|
$contract = $user->getContract(); |
|
68
|
|
|
|
|
69
|
|
|
return $this->grantByAccount($contract, $object) |
|
70
|
|
|
|| $this->grantByCustomer($contract, $object); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Determines, if the given object is of type Account and if it in the set of accounts related to the contract. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Contract $contract The current contract identified by provided the access token. |
|
78
|
|
|
* @param mixed $object The object to be handled |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function grantByAccount(Contract $contract, $object) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
if ($object instanceof \GravitonDyn\AccountBundle\Document\Account) { |
|
|
|
|
|
|
85
|
|
|
return $contract->getAccount()->contains($object); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Determines, if the given object is of type Customer and if it is related to the contract. |
|
93
|
|
|
* todo Remove depency on external classes not present in Graviton StandAlone. |
|
94
|
|
|
* |
|
95
|
|
|
* @param Contract $contract The current contract identified by provided the access token. |
|
96
|
|
|
* @param mixed $object The object to be handled |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function grantByCustomer(Contract $contract, $object) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
if ($object instanceof \GravitonDyn\CustomerBundle\Document\Customer) { |
|
|
|
|
|
|
103
|
|
|
return $contract->getCustomer() == $object; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* todo Implement new voter attribute since symfony 2.8 |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $attribute Object attribute |
|
113
|
|
|
* @param mixed $subject Subject to be supported |
|
114
|
|
|
* @param TokenInterface $token Token |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
|
119
|
|
|
{ |
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $attribute To be supported |
|
125
|
|
|
* @return void |
|
126
|
|
|
* @throws \BadMethodCallException |
|
127
|
|
|
*/ |
|
128
|
|
|
public function supportsAttribute($attribute) |
|
129
|
|
|
{ |
|
130
|
|
|
$msg = 'supportsAttribute method is deprecated since version 2.8, to be removed in 3.0'; |
|
131
|
|
|
throw new \BadMethodCallException($msg); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $class to be supported |
|
136
|
|
|
* @return void |
|
137
|
|
|
* @throws \BadMethodCallException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function supportsClass($class) |
|
140
|
|
|
{ |
|
141
|
|
|
$msg = 'supportsClass method is deprecated since version 2.8, to be removed in 3.0'; |
|
142
|
|
|
throw new \BadMethodCallException($msg); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Determines if the attribute and subject are supported by this voter. |
|
147
|
|
|
* todo implement voters |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $attribute An attribute |
|
150
|
|
|
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type |
|
151
|
|
|
* |
|
152
|
|
|
* @return bool True if the attribute and subject are supported, false otherwise |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function supports($attribute, $subject) |
|
155
|
|
|
{ |
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.