1
|
|
|
<?php |
2
|
|
|
namespace UserPermissions\Controller\Component; |
3
|
|
|
|
4
|
|
|
use Cake\Controller\Component; |
5
|
|
|
use Cake\Controller\ComponentRegistry; |
6
|
|
|
use Cake\Controller\Component\FlashComponent; |
7
|
|
|
use Cake\Datasource\ConnectionManager; |
8
|
|
|
use Cake\Log\Log; |
9
|
|
|
use Cake\ORM\TableRegistry; |
10
|
|
|
use UserPermissions\Exception\MissingHandlerException; |
11
|
|
|
|
12
|
|
|
class UserPermissionsComponent extends Component { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Controller name |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $controller = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Session |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $session = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Components array |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public $components = ['Flash']; |
34
|
|
|
|
35
|
|
|
private $actions; |
36
|
|
|
|
37
|
|
|
private $allow; |
38
|
|
|
|
39
|
|
|
private $redirect; |
40
|
|
|
|
41
|
|
|
private $params; |
42
|
|
|
|
43
|
|
|
private $message; |
44
|
|
|
|
45
|
|
|
private $userType; |
46
|
|
|
|
47
|
|
|
private $action; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Boolean value which holds the configuration for the behavior in case of |
51
|
|
|
* missing handlers. |
52
|
|
|
*/ |
53
|
|
|
private $throwEx; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Boolean value true if an redirect is already invoked. |
57
|
|
|
*/ |
58
|
|
|
private $isRedirecting; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Initialization to get controller variable |
62
|
|
|
* |
63
|
|
|
* For this component available settings: |
64
|
|
|
* bool throwEx - default false - if set to true, an exception will be |
65
|
|
|
* thrown, if a handler is about to be called but does not exist. |
66
|
|
|
* |
67
|
|
|
* @param array $config Configuration array for the component. |
68
|
|
|
*/ |
69
|
7 |
|
public function initialize(array $config) |
70
|
|
|
{ |
71
|
7 |
|
parent::initialize($config); |
72
|
|
|
|
73
|
7 |
|
$this->controller = $this->_registry->getController(); |
|
|
|
|
74
|
7 |
|
$this->session = $this->controller->request->session(); |
75
|
|
|
|
76
|
7 |
|
$this->actions = array(); |
77
|
7 |
|
$this->allow = true; |
78
|
7 |
|
$this->redirect = null; |
79
|
7 |
|
$this->params = ''; |
80
|
7 |
|
$this->message = ''; |
81
|
7 |
|
$this->userType = ''; |
82
|
7 |
|
$this->action = null; |
83
|
7 |
|
$this->throwEx = isset($config["throwEx"]) && $config["throwEx"]; |
84
|
7 |
|
$this->isRedirecting = false; |
85
|
7 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Initialization to get controller variable |
89
|
|
|
* |
90
|
|
|
* @param array $rules Array of rules for permissions. |
91
|
|
|
* @return bool false if user / group doesn't have permission, true if has permission |
92
|
|
|
*/ |
93
|
7 |
|
public function allow ($rules) { |
94
|
7 |
|
$this->setUserValues(); |
95
|
7 |
|
$this->bindConfiguration($rules); |
96
|
|
|
|
97
|
7 |
|
if (!$this->applyGroupsRules($rules)) { |
98
|
7 |
|
$this->applyViewsRules($rules); |
99
|
|
|
} |
100
|
|
|
|
101
|
6 |
|
return $this->allow; |
102
|
|
|
} |
103
|
|
|
|
104
|
7 |
|
private function setUserValues() |
105
|
|
|
{ |
106
|
7 |
|
$userId = $this->session->read('Auth.User.id'); |
|
|
|
|
107
|
|
|
|
108
|
7 |
|
if (!isset($userId)) { |
109
|
7 |
|
$this->userType = 'guest'; |
110
|
|
|
} |
111
|
7 |
|
} |
112
|
|
|
|
113
|
7 |
|
private function bindConfiguration(array $rules) |
114
|
|
|
{ |
115
|
7 |
|
foreach($rules as $key => $value){ |
116
|
|
|
switch($key){ |
117
|
7 |
|
case "user_type": |
118
|
7 |
|
$this->userType = $value; |
119
|
7 |
|
break; |
120
|
7 |
|
case "redirect": |
121
|
7 |
|
$this->redirect = $value; |
122
|
7 |
|
break; |
123
|
7 |
|
case "action": |
124
|
7 |
|
$this->action = $value; |
125
|
7 |
|
break; |
126
|
7 |
|
case "controller": |
127
|
7 |
|
$this->controller = $value; |
128
|
7 |
|
if(!is_object($value)) { |
129
|
|
|
Log::write("warning", sprintf("controller is not an object (%s)", gettype($value))); |
130
|
|
|
} |
131
|
7 |
|
break; |
132
|
7 |
|
case "message": |
133
|
7 |
|
$this->message = $value; |
134
|
7 |
|
break; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
7 |
|
foreach($rules['groups'] as $key => $value){ |
139
|
7 |
|
if($key == $this->userType){ |
140
|
7 |
|
foreach($value as $v){ |
141
|
7 |
|
array_push($this->actions, $v); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
7 |
|
} |
146
|
|
|
|
147
|
7 |
|
private function applyGroupsRules(array $rules) |
148
|
|
|
{ |
149
|
7 |
|
$existRulesForGroups = false; |
150
|
|
|
|
151
|
7 |
|
if(isset($rules['groups'])){ |
152
|
7 |
|
foreach($rules['groups'] as $key => $value){ |
153
|
7 |
|
$this->searchForApplyGroupRules($key, $value); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
7 |
|
return $existRulesForGroups; |
158
|
|
|
} |
159
|
|
|
|
160
|
7 |
|
private function searchForApplyGroupRules($key) |
161
|
|
|
{ |
162
|
7 |
|
if($key == $this->userType){ |
163
|
7 |
|
if ($this->notInArrayAction()) { |
164
|
2 |
|
$this->redirectIfIsSet(); |
165
|
|
|
|
166
|
2 |
|
$this->allow = false; |
167
|
|
|
} |
168
|
|
|
} |
169
|
7 |
|
} |
170
|
|
|
|
171
|
7 |
|
private function notInArrayAction() |
172
|
|
|
{ |
173
|
7 |
|
return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
174
|
|
|
} |
175
|
|
|
|
176
|
7 |
|
private function applyViewsRules(array $rules) |
177
|
|
|
{ |
178
|
7 |
|
if(isset($rules['views'])){ |
179
|
4 |
|
foreach($rules['views'] as $key => $value){ |
180
|
4 |
|
$this->searchForApplyViewRules($key, $value); |
181
|
|
|
} |
182
|
|
|
} |
183
|
6 |
|
} |
184
|
|
|
|
185
|
4 |
|
private function searchForApplyViewRules($key, $value) |
186
|
|
|
{ |
187
|
4 |
|
if($key == $this->action) { |
188
|
4 |
|
if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){ |
|
|
|
|
189
|
2 |
|
$this->redirectIfIsSet(); |
190
|
|
|
|
191
|
2 |
|
$this->allow = false; |
192
|
|
|
} |
193
|
|
|
} |
194
|
3 |
|
} |
195
|
|
|
|
196
|
4 |
|
private function checkForHandler($controller, $handler) |
197
|
|
|
{ |
198
|
4 |
|
if(!method_exists($controller, $handler)) { |
199
|
2 |
|
$msg = sprintf( |
200
|
2 |
|
"Controller %s=%s has no method called '%s'", |
201
|
2 |
|
is_object($controller) ? "class" : "type", |
202
|
2 |
|
is_object($controller) ? get_class($controller) : gettype($controller), |
203
|
2 |
|
$handler |
204
|
|
|
); |
205
|
2 |
|
Log::write("debug", $msg); |
206
|
2 |
|
if($this->throwEx) { |
207
|
1 |
|
throw new MissingHandlerException($msg); |
208
|
|
|
} |
209
|
1 |
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
return true; |
213
|
|
|
} |
214
|
|
|
|
215
|
4 |
|
private function redirectIfIsSet() |
216
|
|
|
{ |
217
|
4 |
|
if($this->redirect && !$this->isRedirecting){ |
218
|
|
|
$this->isRedirecting = true; |
219
|
|
|
if($this->message != ''){ |
220
|
|
|
$this->Flash->set($this->message); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if(method_exists($this->controller, "redirect")) { |
224
|
|
|
$this->controller->redirect($this->redirect); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
else { |
227
|
|
|
header("Location: " . $this->redirect); |
228
|
|
|
exit; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
} |
231
|
4 |
|
} |
232
|
|
|
} |
233
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..