1
|
|
|
<?php |
2
|
|
|
namespace UserPermissions\Controller\Component; |
3
|
|
|
|
4
|
|
|
use Cake\Controller\Component; |
5
|
|
|
use Cake\Controller\ComponentRegistry; |
6
|
|
|
use Cake\Datasource\ConnectionManager; |
7
|
|
|
use Cake\ORM\TableRegistry; |
8
|
|
|
use Cake\Controller\Component\FlashComponent; |
9
|
|
|
|
10
|
|
|
class UserPermissionsComponent extends Component { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Controller name |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public $controller = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Session |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $session = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Components array |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $components = ['Flash']; |
32
|
|
|
|
33
|
|
|
private $actions; |
34
|
|
|
|
35
|
|
|
private $allow; |
36
|
|
|
|
37
|
|
|
private $redirect; |
38
|
|
|
|
39
|
|
|
private $params; |
40
|
|
|
|
41
|
|
|
private $message; |
42
|
|
|
|
43
|
|
|
private $userType; |
44
|
|
|
|
45
|
|
|
private $action; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialization to get controller variable |
49
|
|
|
* |
50
|
|
|
* @param string $event The event to use. |
|
|
|
|
51
|
|
|
*/ |
52
|
5 |
|
public function initialize(array $config) |
53
|
|
|
{ |
54
|
5 |
|
parent::initialize($config); |
55
|
|
|
|
56
|
5 |
|
$this->controller = $this->_registry->getController(); |
|
|
|
|
57
|
5 |
|
$this->session = $this->controller->request->session(); |
58
|
|
|
|
59
|
5 |
|
$this->actions = array(); |
60
|
5 |
|
$this->allow = true; |
61
|
5 |
|
$this->redirect = ''; |
62
|
5 |
|
$this->params = ''; |
63
|
5 |
|
$this->message = ''; |
64
|
5 |
|
$this->userType = ''; |
65
|
5 |
|
$this->action = null; |
66
|
5 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initialization to get controller variable |
70
|
|
|
* |
71
|
|
|
* @param array $rules Array of rules for permissions. |
72
|
|
|
* @return string '0' if user / group doesn't have permission, 1 if has permission |
73
|
|
|
*/ |
74
|
5 |
|
public function allow ($rules) { |
75
|
5 |
|
$this->setUserValues(); |
76
|
5 |
|
$this->bindConfiguration($rules); |
77
|
|
|
|
78
|
5 |
|
if (!$this->applyGroupsRules($rules)) { |
79
|
5 |
|
$this->applyViewsRules($rules); |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
return $this->allow; |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
private function setUserValues() |
86
|
|
|
{ |
87
|
5 |
|
$userId = $this->session->read('Auth.User.id'); |
|
|
|
|
88
|
|
|
|
89
|
5 |
|
if (!isset($userId)) { |
90
|
5 |
|
$this->userType = 'guest'; |
91
|
|
|
} |
92
|
5 |
|
} |
93
|
|
|
|
94
|
5 |
|
private function bindConfiguration(array $rules) |
95
|
|
|
{ |
96
|
5 |
|
foreach($rules as $key => $value){ |
97
|
|
|
switch($key){ |
98
|
5 |
|
case "user_type": |
99
|
5 |
|
$this->userType = $value; |
100
|
5 |
|
break; |
101
|
5 |
|
case "redirect": |
102
|
5 |
|
$this->redirect = $value; |
103
|
5 |
|
break; |
104
|
5 |
|
case "action": |
105
|
5 |
|
$this->action = $value; |
106
|
5 |
|
break; |
107
|
5 |
|
case "controller": |
108
|
5 |
|
$this->controller = $value; |
109
|
5 |
|
break; |
110
|
5 |
|
case "message": |
111
|
5 |
|
$this->message = $value; |
112
|
5 |
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
foreach($rules['groups'] as $key => $value){ |
117
|
5 |
|
if($key == $this->userType){ |
118
|
5 |
|
foreach($value as $v){ |
119
|
5 |
|
array_push($this->actions, $v); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
5 |
|
} |
124
|
|
|
|
125
|
5 |
|
private function applyGroupsRules(array $rules) |
126
|
|
|
{ |
127
|
5 |
|
$existRulesForGroups = false; |
128
|
|
|
|
129
|
5 |
|
if(isset($rules['groups'])){ |
130
|
5 |
|
foreach($rules['groups'] as $key => $value){ |
131
|
5 |
|
$this->searchForApplyGroupRules($key, $value); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
5 |
|
return $existRulesForGroups; |
136
|
|
|
} |
137
|
|
|
|
138
|
5 |
|
private function searchForApplyGroupRules($key) |
139
|
|
|
{ |
140
|
5 |
|
if($key == $this->userType){ |
141
|
5 |
|
if ($this->notInArrayAction()) { |
142
|
2 |
|
$this->redirectIfIsSet(); |
143
|
|
|
|
144
|
2 |
|
$this->allow = false; |
145
|
|
|
} |
146
|
|
|
} |
147
|
5 |
|
} |
148
|
|
|
|
149
|
5 |
|
private function notInArrayAction() |
150
|
|
|
{ |
151
|
5 |
|
return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
152
|
|
|
} |
153
|
|
|
|
154
|
5 |
|
private function applyViewsRules(array $rules) |
155
|
|
|
{ |
156
|
5 |
|
if(isset($rules['views'])){ |
157
|
2 |
|
foreach($rules['views'] as $key => $value){ |
158
|
2 |
|
$this->searchForApplyViewRules($key, $value); |
159
|
|
|
} |
160
|
|
|
} |
161
|
5 |
|
} |
162
|
|
|
|
163
|
2 |
|
private function searchForApplyViewRules($key, $value) |
164
|
|
|
{ |
165
|
2 |
|
if($key == $this->action){ |
166
|
2 |
|
if(!$this->controller->$value()){ |
|
|
|
|
167
|
1 |
|
$this->redirectIfIsSet(); |
168
|
|
|
|
169
|
1 |
|
$this->allow = false; |
170
|
|
|
} |
171
|
|
|
} |
172
|
2 |
|
} |
173
|
|
|
|
174
|
3 |
|
private function redirectIfIsSet() |
175
|
|
|
{ |
176
|
3 |
|
if($this->redirect != ''){ |
177
|
|
|
if($this->message != ''){ |
178
|
|
|
$this->Flash->set($this->message); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
header("Location: " . $this->redirect); |
182
|
|
|
exit; |
|
|
|
|
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.