|
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
|
|
|
* Initialization to get controller variable |
|
57
|
|
|
* |
|
58
|
|
|
* For this component available settings: |
|
59
|
|
|
* bool throwEx - default false - if set to true, an exception will be |
|
60
|
|
|
* thrown, if a handler is about to be called but does not exist. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $config Configuration array for the component. |
|
63
|
|
|
*/ |
|
64
|
6 |
|
public function initialize(array $config) |
|
65
|
|
|
{ |
|
66
|
6 |
|
parent::initialize($config); |
|
67
|
|
|
|
|
68
|
6 |
|
$this->controller = $this->_registry->getController(); |
|
|
|
|
|
|
69
|
6 |
|
$this->session = $this->controller->request->session(); |
|
70
|
|
|
|
|
71
|
6 |
|
$this->actions = array(); |
|
72
|
6 |
|
$this->allow = true; |
|
73
|
6 |
|
$this->redirect = ''; |
|
74
|
6 |
|
$this->params = ''; |
|
75
|
6 |
|
$this->message = ''; |
|
76
|
6 |
|
$this->userType = ''; |
|
77
|
6 |
|
$this->action = null; |
|
78
|
6 |
|
$this->throwEx = isset($config["throwEx"]) && $config["throwEx"]; |
|
79
|
6 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Initialization to get controller variable |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $rules Array of rules for permissions. |
|
85
|
|
|
* @return bool false if user / group doesn't have permission, true if has permission |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function allow ($rules) { |
|
88
|
6 |
|
$this->setUserValues(); |
|
89
|
6 |
|
$this->bindConfiguration($rules); |
|
90
|
|
|
|
|
91
|
6 |
|
if (!$this->applyGroupsRules($rules)) { |
|
92
|
6 |
|
$this->applyViewsRules($rules); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
return $this->allow; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
6 |
|
private function setUserValues() |
|
99
|
|
|
{ |
|
100
|
6 |
|
$userId = $this->session->read('Auth.User.id'); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
6 |
|
if (!isset($userId)) { |
|
103
|
6 |
|
$this->userType = 'guest'; |
|
104
|
|
|
} |
|
105
|
6 |
|
} |
|
106
|
|
|
|
|
107
|
6 |
|
private function bindConfiguration(array $rules) |
|
108
|
|
|
{ |
|
109
|
6 |
|
foreach($rules as $key => $value){ |
|
110
|
|
|
switch($key){ |
|
111
|
6 |
|
case "user_type": |
|
112
|
6 |
|
$this->userType = $value; |
|
113
|
6 |
|
break; |
|
114
|
6 |
|
case "redirect": |
|
115
|
6 |
|
$this->redirect = $value; |
|
116
|
6 |
|
break; |
|
117
|
6 |
|
case "action": |
|
118
|
6 |
|
$this->action = $value; |
|
119
|
6 |
|
break; |
|
120
|
6 |
|
case "controller": |
|
121
|
6 |
|
$this->controller = $value; |
|
122
|
6 |
|
if(!is_object($value)) { |
|
123
|
|
|
Log::write("warning", sprintf("controller is not an object (%s)", gettype($value))); |
|
124
|
|
|
} |
|
125
|
6 |
|
break; |
|
126
|
6 |
|
case "message": |
|
127
|
6 |
|
$this->message = $value; |
|
128
|
6 |
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
6 |
|
foreach($rules['groups'] as $key => $value){ |
|
133
|
6 |
|
if($key == $this->userType){ |
|
134
|
6 |
|
foreach($value as $v){ |
|
135
|
6 |
|
array_push($this->actions, $v); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
6 |
|
} |
|
140
|
|
|
|
|
141
|
6 |
|
private function applyGroupsRules(array $rules) |
|
142
|
|
|
{ |
|
143
|
6 |
|
$existRulesForGroups = false; |
|
144
|
|
|
|
|
145
|
6 |
|
if(isset($rules['groups'])){ |
|
146
|
6 |
|
foreach($rules['groups'] as $key => $value){ |
|
147
|
6 |
|
$this->searchForApplyGroupRules($key, $value); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
6 |
|
return $existRulesForGroups; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
6 |
|
private function searchForApplyGroupRules($key) |
|
155
|
|
|
{ |
|
156
|
6 |
|
if($key == $this->userType){ |
|
157
|
6 |
|
if ($this->notInArrayAction()) { |
|
158
|
2 |
|
$this->redirectIfIsSet(); |
|
159
|
|
|
|
|
160
|
2 |
|
$this->allow = false; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
6 |
|
} |
|
164
|
|
|
|
|
165
|
6 |
|
private function notInArrayAction() |
|
166
|
|
|
{ |
|
167
|
6 |
|
return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
6 |
|
private function applyViewsRules(array $rules) |
|
171
|
|
|
{ |
|
172
|
6 |
|
if(isset($rules['views'])){ |
|
173
|
3 |
|
foreach($rules['views'] as $key => $value){ |
|
174
|
3 |
|
$this->searchForApplyViewRules($key, $value); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
6 |
|
} |
|
178
|
|
|
|
|
179
|
3 |
|
private function searchForApplyViewRules($key, $value) |
|
180
|
|
|
{ |
|
181
|
3 |
|
if($key == $this->action) { |
|
182
|
3 |
|
if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){ |
|
|
|
|
|
|
183
|
2 |
|
$this->redirectIfIsSet(); |
|
184
|
|
|
|
|
185
|
2 |
|
$this->allow = false; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
3 |
|
} |
|
189
|
|
|
|
|
190
|
3 |
|
private function checkForHandler($controller, $handler) |
|
191
|
|
|
{ |
|
192
|
3 |
|
if(!method_exists($controller, $handler)) { |
|
193
|
1 |
|
$msg = sprintf( |
|
194
|
1 |
|
"Controller %s=%s has no method called '%s'", |
|
195
|
1 |
|
is_object($controller) ? "class" : "type", |
|
196
|
1 |
|
is_object($controller) ? get_class($controller) : gettype($controller), |
|
197
|
1 |
|
$handler |
|
198
|
|
|
); |
|
199
|
1 |
|
Log::write("debug", $msg); |
|
200
|
1 |
|
if($this->throwEx) { |
|
201
|
|
|
throw new MissingHandlerException($msg); |
|
202
|
|
|
} |
|
203
|
1 |
|
return false; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
2 |
|
return true; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
4 |
|
private function redirectIfIsSet() |
|
210
|
|
|
{ |
|
211
|
4 |
|
if($this->redirect != ''){ |
|
212
|
|
|
if($this->message != ''){ |
|
213
|
|
|
$this->Flash->set($this->message); |
|
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
header("Location: " . $this->redirect); |
|
217
|
|
|
exit; |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
} |
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..