|
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
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Initialization to get controller variable |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $event The event to use. |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
5 |
|
public function initialize(array $config) |
|
40
|
|
|
{ |
|
41
|
5 |
|
parent::initialize($config); |
|
42
|
|
|
|
|
43
|
5 |
|
$this->controller = $this->_registry->getController(); |
|
|
|
|
|
|
44
|
5 |
|
$this->session = $this->controller->request->session(); |
|
45
|
|
|
|
|
46
|
5 |
|
$this->actions = array(); |
|
|
|
|
|
|
47
|
5 |
|
$this->allow = true; |
|
|
|
|
|
|
48
|
5 |
|
$this->redirect = ''; |
|
|
|
|
|
|
49
|
5 |
|
$this->params = ''; |
|
|
|
|
|
|
50
|
5 |
|
$this->message = ''; |
|
|
|
|
|
|
51
|
5 |
|
$this->userType = ''; |
|
|
|
|
|
|
52
|
5 |
|
$this->action = null; |
|
|
|
|
|
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Initialization to get controller variable |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $rules Array of rules for permissions. |
|
59
|
|
|
* @return string '0' if user / group doesn't have permission, 1 if has permission |
|
60
|
|
|
*/ |
|
61
|
5 |
|
public function allow ($rules) { |
|
62
|
5 |
|
$this->setUserValues(); |
|
63
|
5 |
|
$this->bindConfiguration($rules); |
|
64
|
|
|
|
|
65
|
5 |
|
if (!$this->applyGroupsRules($rules)) { |
|
66
|
5 |
|
$this->applyViewsRules($rules); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
return $this->allow; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
private function setUserValues() |
|
73
|
|
|
{ |
|
74
|
5 |
|
$userId = $this->session->read('Auth.User.id'); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
5 |
|
if (!isset($userId)) { |
|
77
|
5 |
|
$this->userType = 'guest'; |
|
78
|
|
|
} |
|
79
|
5 |
|
} |
|
80
|
|
|
|
|
81
|
5 |
|
private function bindConfiguration(array $rules) |
|
82
|
|
|
{ |
|
83
|
5 |
|
foreach($rules as $key => $value){ |
|
84
|
|
|
switch($key){ |
|
85
|
5 |
|
case "user_type": |
|
86
|
5 |
|
$this->userType = $value; |
|
87
|
5 |
|
break; |
|
88
|
5 |
|
case "redirect": |
|
89
|
5 |
|
$this->redirect = $value; |
|
90
|
5 |
|
break; |
|
91
|
5 |
|
case "action": |
|
92
|
5 |
|
$this->action = $value; |
|
|
|
|
|
|
93
|
5 |
|
break; |
|
94
|
5 |
|
case "controller": |
|
95
|
5 |
|
$this->controller = $value; |
|
96
|
5 |
|
break; |
|
97
|
5 |
|
case "message": |
|
98
|
5 |
|
$this->message = $value; |
|
99
|
5 |
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
foreach($rules['groups'] as $key => $value){ |
|
104
|
5 |
|
if($key == $this->userType){ |
|
105
|
5 |
|
foreach($value as $v){ |
|
106
|
5 |
|
array_push($this->actions, $v); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
5 |
|
} |
|
111
|
|
|
|
|
112
|
5 |
|
private function applyGroupsRules(array $rules) : bool |
|
113
|
|
|
{ |
|
114
|
5 |
|
$existRulesForGroups = false; |
|
115
|
|
|
|
|
116
|
5 |
|
if(isset($rules['groups'])){ |
|
117
|
5 |
|
foreach($rules['groups'] as $key => $value){ |
|
118
|
5 |
|
$this->searchForApplyGroupRules($key, $value); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
5 |
|
return $existRulesForGroups; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
5 |
|
private function searchForApplyGroupRules($key, $value) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
5 |
|
if($key == $this->userType){ |
|
128
|
5 |
|
if ($this->notInArrayAction()) { |
|
129
|
2 |
|
$existRulesForGroups = true; |
|
|
|
|
|
|
130
|
2 |
|
$this->redirectIfIsSet(); |
|
131
|
|
|
|
|
132
|
2 |
|
$this->allow = false; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
5 |
|
} |
|
136
|
|
|
|
|
137
|
5 |
|
private function notInArrayAction() |
|
138
|
|
|
{ |
|
139
|
5 |
|
return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
5 |
|
private function applyViewsRules(array $rules) |
|
143
|
|
|
{ |
|
144
|
5 |
|
if(isset($rules['views'])){ |
|
145
|
2 |
|
foreach($rules['views'] as $key => $value){ |
|
146
|
2 |
|
$this->searchForApplyViewRules($key, $value); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
5 |
|
} |
|
150
|
|
|
|
|
151
|
2 |
|
private function searchForApplyViewRules($key, $value) |
|
152
|
|
|
{ |
|
153
|
2 |
|
if($key == $this->action){ |
|
|
|
|
|
|
154
|
2 |
|
if(!$this->controller->$value()){ |
|
|
|
|
|
|
155
|
1 |
|
$this->redirectIfIsSet(); |
|
156
|
|
|
|
|
157
|
1 |
|
$this->allow = false; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
2 |
|
} |
|
161
|
|
|
|
|
162
|
3 |
|
private function redirectIfIsSet() |
|
163
|
|
|
{ |
|
164
|
3 |
|
if($this->redirect != ''){ |
|
165
|
|
|
if($this->message != ''){ |
|
166
|
|
|
$this->Flash->set($this->message); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
header("Location: " . $this->redirect); |
|
170
|
|
|
exit; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.