@@ -11,40 +11,40 @@ discard block |
||
11 | 11 | |
12 | 12 | class UserPermissionsComponent extends Component { |
13 | 13 | |
14 | - /** |
|
15 | - * Controller name |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
14 | + /** |
|
15 | + * Controller name |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | 19 | public $controller = null; |
20 | 20 | |
21 | - /** |
|
22 | - * Session |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
21 | + /** |
|
22 | + * Session |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | 26 | public $session = null; |
27 | 27 | |
28 | - /** |
|
29 | - * Components array |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
28 | + /** |
|
29 | + * Components array |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | 33 | public $components = ['Flash']; |
34 | 34 | |
35 | - private $actions; |
|
35 | + private $actions; |
|
36 | 36 | |
37 | - private $allow; |
|
37 | + private $allow; |
|
38 | 38 | |
39 | - private $redirect; |
|
39 | + private $redirect; |
|
40 | 40 | |
41 | - private $params; |
|
41 | + private $params; |
|
42 | 42 | |
43 | - private $message; |
|
43 | + private $message; |
|
44 | 44 | |
45 | - private $userType; |
|
45 | + private $userType; |
|
46 | 46 | |
47 | - private $action; |
|
47 | + private $action; |
|
48 | 48 | |
49 | 49 | /** |
50 | 50 | * Boolean value which holds the configuration for the behavior in case of |
@@ -57,23 +57,23 @@ discard block |
||
57 | 57 | */ |
58 | 58 | private $isRedirecting; |
59 | 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 | - public function initialize(array $config) |
|
70 | - { |
|
71 | - parent::initialize($config); |
|
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 | + public function initialize(array $config) |
|
70 | + { |
|
71 | + parent::initialize($config); |
|
72 | 72 | |
73 | - $this->controller = $this->_registry->getController(); |
|
74 | - $this->session = $this->controller->request->session(); |
|
73 | + $this->controller = $this->_registry->getController(); |
|
74 | + $this->session = $this->controller->request->session(); |
|
75 | 75 | |
76 | - $this->actions = array(); |
|
76 | + $this->actions = array(); |
|
77 | 77 | $this->allow = true; |
78 | 78 | $this->redirect = null; |
79 | 79 | $this->params = ''; |
@@ -82,56 +82,56 @@ discard block |
||
82 | 82 | $this->action = null; |
83 | 83 | $this->throwEx = isset($config["throwEx"]) && $config["throwEx"]; |
84 | 84 | $this->isRedirecting = false; |
85 | - } |
|
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 | - public function allow ($rules) { |
|
94 | - $this->setUserValues(); |
|
95 | - $this->bindConfiguration($rules); |
|
85 | + } |
|
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 | + public function allow ($rules) { |
|
94 | + $this->setUserValues(); |
|
95 | + $this->bindConfiguration($rules); |
|
96 | 96 | |
97 | 97 | if (!$this->applyGroupsRules($rules)) { |
98 | 98 | $this->applyViewsRules($rules); |
99 | 99 | } |
100 | 100 | |
101 | 101 | return $this->allow; |
102 | - } |
|
102 | + } |
|
103 | 103 | |
104 | - private function setUserValues() |
|
105 | - { |
|
106 | - $userId = $this->session->read('Auth.User.id'); |
|
104 | + private function setUserValues() |
|
105 | + { |
|
106 | + $userId = $this->session->read('Auth.User.id'); |
|
107 | 107 | |
108 | - if (!isset($userId)) { |
|
108 | + if (!isset($userId)) { |
|
109 | 109 | $this->userType = 'guest'; |
110 | 110 | } |
111 | - } |
|
111 | + } |
|
112 | 112 | |
113 | - private function bindConfiguration(array $rules) |
|
114 | - { |
|
115 | - foreach($rules as $key => $value){ |
|
113 | + private function bindConfiguration(array $rules) |
|
114 | + { |
|
115 | + foreach($rules as $key => $value){ |
|
116 | 116 | switch($key){ |
117 | 117 | case "user_type": |
118 | - $this->userType = $value; |
|
119 | - break; |
|
120 | - case "redirect": |
|
121 | - $this->redirect = $value; |
|
122 | - break; |
|
123 | - case "action": |
|
124 | - $this->action = $value; |
|
125 | - break; |
|
126 | - case "controller": |
|
127 | - $this->controller = $value; |
|
118 | + $this->userType = $value; |
|
119 | + break; |
|
120 | + case "redirect": |
|
121 | + $this->redirect = $value; |
|
122 | + break; |
|
123 | + case "action": |
|
124 | + $this->action = $value; |
|
125 | + break; |
|
126 | + case "controller": |
|
127 | + $this->controller = $value; |
|
128 | 128 | if(!is_object($value)) { |
129 | 129 | Log::write("warning", sprintf("controller is not an object (%s)", gettype($value))); |
130 | 130 | } |
131 | - break; |
|
132 | - case "message": |
|
133 | - $this->message = $value; |
|
134 | - break; |
|
131 | + break; |
|
132 | + case "message": |
|
133 | + $this->message = $value; |
|
134 | + break; |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
@@ -142,56 +142,56 @@ discard block |
||
142 | 142 | } |
143 | 143 | } |
144 | 144 | } |
145 | - } |
|
145 | + } |
|
146 | 146 | |
147 | - private function applyGroupsRules(array $rules) |
|
148 | - { |
|
149 | - $existRulesForGroups = false; |
|
147 | + private function applyGroupsRules(array $rules) |
|
148 | + { |
|
149 | + $existRulesForGroups = false; |
|
150 | 150 | |
151 | - if(isset($rules['groups'])){ |
|
151 | + if(isset($rules['groups'])){ |
|
152 | 152 | foreach($rules['groups'] as $key => $value){ |
153 | 153 | $this->searchForApplyGroupRules($key, $value); |
154 | 154 | } |
155 | 155 | } |
156 | 156 | |
157 | 157 | return $existRulesForGroups; |
158 | - } |
|
158 | + } |
|
159 | 159 | |
160 | - private function searchForApplyGroupRules($key) |
|
161 | - { |
|
162 | - if($key == $this->userType){ |
|
163 | - if ($this->notInArrayAction()) { |
|
160 | + private function searchForApplyGroupRules($key) |
|
161 | + { |
|
162 | + if($key == $this->userType){ |
|
163 | + if ($this->notInArrayAction()) { |
|
164 | 164 | $this->redirectIfIsSet(); |
165 | 165 | |
166 | 166 | $this->allow = false; |
167 | 167 | } |
168 | 168 | } |
169 | - } |
|
169 | + } |
|
170 | 170 | |
171 | - private function notInArrayAction() |
|
172 | - { |
|
173 | - return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
174 | - } |
|
171 | + private function notInArrayAction() |
|
172 | + { |
|
173 | + return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
174 | + } |
|
175 | 175 | |
176 | - private function applyViewsRules(array $rules) |
|
177 | - { |
|
178 | - if(isset($rules['views'])){ |
|
176 | + private function applyViewsRules(array $rules) |
|
177 | + { |
|
178 | + if(isset($rules['views'])){ |
|
179 | 179 | foreach($rules['views'] as $key => $value){ |
180 | 180 | $this->searchForApplyViewRules($key, $value); |
181 | 181 | } |
182 | 182 | } |
183 | - } |
|
183 | + } |
|
184 | 184 | |
185 | - private function searchForApplyViewRules($key, $value) |
|
186 | - { |
|
187 | - if($key == $this->action) { |
|
185 | + private function searchForApplyViewRules($key, $value) |
|
186 | + { |
|
187 | + if($key == $this->action) { |
|
188 | 188 | if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){ |
189 | 189 | $this->redirectIfIsSet(); |
190 | 190 | |
191 | 191 | $this->allow = false; |
192 | 192 | } |
193 | 193 | } |
194 | - } |
|
194 | + } |
|
195 | 195 | |
196 | 196 | private function checkForHandler($controller, $handler) |
197 | 197 | { |
@@ -212,9 +212,9 @@ discard block |
||
212 | 212 | return true; |
213 | 213 | } |
214 | 214 | |
215 | - private function redirectIfIsSet() |
|
216 | - { |
|
217 | - if($this->redirect && !$this->isRedirecting){ |
|
215 | + private function redirectIfIsSet() |
|
216 | + { |
|
217 | + if($this->redirect && !$this->isRedirecting){ |
|
218 | 218 | $this->isRedirecting = true; |
219 | 219 | if($this->message != ''){ |
220 | 220 | $this->Flash->set($this->message); |
@@ -74,9 +74,9 @@ discard block |
||
74 | 74 | $this->session = $this->controller->request->session(); |
75 | 75 | |
76 | 76 | $this->actions = array(); |
77 | - $this->allow = true; |
|
77 | + $this->allow = true; |
|
78 | 78 | $this->redirect = null; |
79 | - $this->params = ''; |
|
79 | + $this->params = ''; |
|
80 | 80 | $this->message = ''; |
81 | 81 | $this->userType = ''; |
82 | 82 | $this->action = null; |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @param array $rules Array of rules for permissions. |
91 | 91 | * @return bool false if user / group doesn't have permission, true if has permission |
92 | 92 | */ |
93 | - public function allow ($rules) { |
|
93 | + public function allow($rules) { |
|
94 | 94 | $this->setUserValues(); |
95 | 95 | $this->bindConfiguration($rules); |
96 | 96 | |
@@ -112,8 +112,8 @@ discard block |
||
112 | 112 | |
113 | 113 | private function bindConfiguration(array $rules) |
114 | 114 | { |
115 | - foreach($rules as $key => $value){ |
|
116 | - switch($key){ |
|
115 | + foreach ($rules as $key => $value) { |
|
116 | + switch ($key) { |
|
117 | 117 | case "user_type": |
118 | 118 | $this->userType = $value; |
119 | 119 | break; |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | break; |
126 | 126 | case "controller": |
127 | 127 | $this->controller = $value; |
128 | - if(!is_object($value)) { |
|
128 | + if (!is_object($value)) { |
|
129 | 129 | Log::write("warning", sprintf("controller is not an object (%s)", gettype($value))); |
130 | 130 | } |
131 | 131 | break; |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
138 | - foreach($rules['groups'] as $key => $value){ |
|
139 | - if($key == $this->userType){ |
|
140 | - foreach($value as $v){ |
|
138 | + foreach ($rules['groups'] as $key => $value) { |
|
139 | + if ($key == $this->userType) { |
|
140 | + foreach ($value as $v) { |
|
141 | 141 | array_push($this->actions, $v); |
142 | 142 | } |
143 | 143 | } |
@@ -148,8 +148,8 @@ discard block |
||
148 | 148 | { |
149 | 149 | $existRulesForGroups = false; |
150 | 150 | |
151 | - if(isset($rules['groups'])){ |
|
152 | - foreach($rules['groups'] as $key => $value){ |
|
151 | + if (isset($rules['groups'])) { |
|
152 | + foreach ($rules['groups'] as $key => $value) { |
|
153 | 153 | $this->searchForApplyGroupRules($key, $value); |
154 | 154 | } |
155 | 155 | } |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | |
160 | 160 | private function searchForApplyGroupRules($key) |
161 | 161 | { |
162 | - if($key == $this->userType){ |
|
162 | + if ($key == $this->userType) { |
|
163 | 163 | if ($this->notInArrayAction()) { |
164 | 164 | $this->redirectIfIsSet(); |
165 | 165 | |
@@ -175,8 +175,8 @@ discard block |
||
175 | 175 | |
176 | 176 | private function applyViewsRules(array $rules) |
177 | 177 | { |
178 | - if(isset($rules['views'])){ |
|
179 | - foreach($rules['views'] as $key => $value){ |
|
178 | + if (isset($rules['views'])) { |
|
179 | + foreach ($rules['views'] as $key => $value) { |
|
180 | 180 | $this->searchForApplyViewRules($key, $value); |
181 | 181 | } |
182 | 182 | } |
@@ -184,8 +184,8 @@ discard block |
||
184 | 184 | |
185 | 185 | private function searchForApplyViewRules($key, $value) |
186 | 186 | { |
187 | - if($key == $this->action) { |
|
188 | - if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){ |
|
187 | + if ($key == $this->action) { |
|
188 | + if (!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()) { |
|
189 | 189 | $this->redirectIfIsSet(); |
190 | 190 | |
191 | 191 | $this->allow = false; |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | |
196 | 196 | private function checkForHandler($controller, $handler) |
197 | 197 | { |
198 | - if(!method_exists($controller, $handler)) { |
|
198 | + if (!method_exists($controller, $handler)) { |
|
199 | 199 | $msg = sprintf( |
200 | 200 | "Controller %s=%s has no method called '%s'", |
201 | 201 | is_object($controller) ? "class" : "type", |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | $handler |
204 | 204 | ); |
205 | 205 | Log::write("debug", $msg); |
206 | - if($this->throwEx) { |
|
206 | + if ($this->throwEx) { |
|
207 | 207 | throw new MissingHandlerException($msg); |
208 | 208 | } |
209 | 209 | return false; |
@@ -214,13 +214,13 @@ discard block |
||
214 | 214 | |
215 | 215 | private function redirectIfIsSet() |
216 | 216 | { |
217 | - if($this->redirect && !$this->isRedirecting){ |
|
217 | + if ($this->redirect && !$this->isRedirecting) { |
|
218 | 218 | $this->isRedirecting = true; |
219 | - if($this->message != ''){ |
|
219 | + if ($this->message != '') { |
|
220 | 220 | $this->Flash->set($this->message); |
221 | 221 | } |
222 | 222 | |
223 | - if(method_exists($this->controller, "redirect")) { |
|
223 | + if (method_exists($this->controller, "redirect")) { |
|
224 | 224 | $this->controller->redirect($this->redirect); |
225 | 225 | } |
226 | 226 | else { |
@@ -222,8 +222,7 @@ |
||
222 | 222 | |
223 | 223 | if(method_exists($this->controller, "redirect")) { |
224 | 224 | $this->controller->redirect($this->redirect); |
225 | - } |
|
226 | - else { |
|
225 | + } else { |
|
227 | 226 | header("Location: " . $this->redirect); |
228 | 227 | exit; |
229 | 228 | } |