@@ -189,6 +189,9 @@ |
||
189 | 189 | } |
190 | 190 | } |
191 | 191 | |
192 | + /** |
|
193 | + * @param string $controller |
|
194 | + */ |
|
192 | 195 | private function checkForHandler($controller, $handler) |
193 | 196 | { |
194 | 197 | if(!method_exists($controller, $handler)) { |
@@ -2,11 +2,7 @@ |
||
2 | 2 | namespace UserPermissions\Controller\Component; |
3 | 3 | |
4 | 4 | use Cake\Controller\Component; |
5 | -use Cake\Controller\ComponentRegistry; |
|
6 | -use Cake\Controller\Component\FlashComponent; |
|
7 | -use Cake\Datasource\ConnectionManager; |
|
8 | 5 | use Cake\Log\Log; |
9 | -use Cake\ORM\TableRegistry; |
|
10 | 6 | use UserPermissions\Exception\MissingHandlerException; |
11 | 7 | |
12 | 8 | class UserPermissionsComponent extends Component { |
@@ -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 |
@@ -52,23 +52,23 @@ discard block |
||
52 | 52 | */ |
53 | 53 | private $throwEx; |
54 | 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 | - public function initialize(array $config) |
|
65 | - { |
|
66 | - parent::initialize($config); |
|
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 | + public function initialize(array $config) |
|
65 | + { |
|
66 | + parent::initialize($config); |
|
67 | 67 | |
68 | - $this->controller = $this->_registry->getController(); |
|
69 | - $this->session = $this->controller->request->session(); |
|
68 | + $this->controller = $this->_registry->getController(); |
|
69 | + $this->session = $this->controller->request->session(); |
|
70 | 70 | |
71 | - $this->actions = array(); |
|
71 | + $this->actions = array(); |
|
72 | 72 | $this->allow = true; |
73 | 73 | $this->redirect = ''; |
74 | 74 | $this->params = ''; |
@@ -76,36 +76,36 @@ discard block |
||
76 | 76 | $this->userType = ''; |
77 | 77 | $this->action = null; |
78 | 78 | $this->throwEx = isset($config["throwEx"]) && $config["throwEx"]; |
79 | - } |
|
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 | - public function allow ($rules) { |
|
88 | - $this->setUserValues(); |
|
89 | - $this->bindConfiguration($rules); |
|
79 | + } |
|
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 | + public function allow ($rules) { |
|
88 | + $this->setUserValues(); |
|
89 | + $this->bindConfiguration($rules); |
|
90 | 90 | |
91 | 91 | if (!$this->applyGroupsRules($rules)) { |
92 | 92 | $this->applyViewsRules($rules); |
93 | 93 | } |
94 | 94 | |
95 | 95 | return $this->allow; |
96 | - } |
|
96 | + } |
|
97 | 97 | |
98 | - private function setUserValues() |
|
99 | - { |
|
100 | - $userId = $this->session->read('Auth.User.id'); |
|
98 | + private function setUserValues() |
|
99 | + { |
|
100 | + $userId = $this->session->read('Auth.User.id'); |
|
101 | 101 | |
102 | - if (!isset($userId)) { |
|
102 | + if (!isset($userId)) { |
|
103 | 103 | $this->userType = 'guest'; |
104 | 104 | } |
105 | - } |
|
105 | + } |
|
106 | 106 | |
107 | - private function bindConfiguration(array $rules) |
|
108 | - { |
|
107 | + private function bindConfiguration(array $rules) |
|
108 | + { |
|
109 | 109 | foreach(array("user_type", "redirect", "action", "controller", "message") as $key) { |
110 | 110 | if(isset($rules[$key])) { |
111 | 111 | $this->$key = $value; |
@@ -122,56 +122,56 @@ discard block |
||
122 | 122 | } |
123 | 123 | } |
124 | 124 | } |
125 | - } |
|
125 | + } |
|
126 | 126 | |
127 | - private function applyGroupsRules(array $rules) |
|
128 | - { |
|
129 | - $existRulesForGroups = false; |
|
127 | + private function applyGroupsRules(array $rules) |
|
128 | + { |
|
129 | + $existRulesForGroups = false; |
|
130 | 130 | |
131 | - if(isset($rules['groups'])){ |
|
131 | + if(isset($rules['groups'])){ |
|
132 | 132 | foreach($rules['groups'] as $key => $value){ |
133 | 133 | $this->searchForApplyGroupRules($key, $value); |
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
137 | 137 | return $existRulesForGroups; |
138 | - } |
|
138 | + } |
|
139 | 139 | |
140 | - private function searchForApplyGroupRules($key) |
|
141 | - { |
|
142 | - if($key == $this->userType){ |
|
143 | - if ($this->notInArrayAction()) { |
|
140 | + private function searchForApplyGroupRules($key) |
|
141 | + { |
|
142 | + if($key == $this->userType){ |
|
143 | + if ($this->notInArrayAction()) { |
|
144 | 144 | $this->redirectIfIsSet(); |
145 | 145 | |
146 | 146 | $this->allow = false; |
147 | 147 | } |
148 | 148 | } |
149 | - } |
|
149 | + } |
|
150 | 150 | |
151 | - private function notInArrayAction() |
|
152 | - { |
|
153 | - return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
154 | - } |
|
151 | + private function notInArrayAction() |
|
152 | + { |
|
153 | + return ((!in_array('*', $this->actions)) && (!in_array($this->action, $this->actions))); |
|
154 | + } |
|
155 | 155 | |
156 | - private function applyViewsRules(array $rules) |
|
157 | - { |
|
158 | - if(isset($rules['views'])){ |
|
156 | + private function applyViewsRules(array $rules) |
|
157 | + { |
|
158 | + if(isset($rules['views'])){ |
|
159 | 159 | foreach($rules['views'] as $key => $value){ |
160 | 160 | $this->searchForApplyViewRules($key, $value); |
161 | 161 | } |
162 | 162 | } |
163 | - } |
|
163 | + } |
|
164 | 164 | |
165 | - private function searchForApplyViewRules($key, $value) |
|
166 | - { |
|
167 | - if($key == $this->action) { |
|
165 | + private function searchForApplyViewRules($key, $value) |
|
166 | + { |
|
167 | + if($key == $this->action) { |
|
168 | 168 | if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){ |
169 | 169 | $this->redirectIfIsSet(); |
170 | 170 | |
171 | 171 | $this->allow = false; |
172 | 172 | } |
173 | 173 | } |
174 | - } |
|
174 | + } |
|
175 | 175 | |
176 | 176 | private function checkForHandler($controller, $handler) |
177 | 177 | { |
@@ -192,9 +192,9 @@ discard block |
||
192 | 192 | return true; |
193 | 193 | } |
194 | 194 | |
195 | - private function redirectIfIsSet() |
|
196 | - { |
|
197 | - if($this->redirect != ''){ |
|
195 | + private function redirectIfIsSet() |
|
196 | + { |
|
197 | + if($this->redirect != ''){ |
|
198 | 198 | if($this->message != ''){ |
199 | 199 | $this->Flash->set($this->message); |
200 | 200 | } |
@@ -68,14 +68,14 @@ discard block |
||
68 | 68 | $this->controller = $this->_registry->getController(); |
69 | 69 | $this->session = $this->controller->request->session(); |
70 | 70 | |
71 | - $this->actions = array(); |
|
72 | - $this->allow = true; |
|
71 | + $this->actions = array(); |
|
72 | + $this->allow = true; |
|
73 | 73 | $this->redirect = ''; |
74 | - $this->params = ''; |
|
74 | + $this->params = ''; |
|
75 | 75 | $this->message = ''; |
76 | 76 | $this->userType = ''; |
77 | 77 | $this->action = null; |
78 | - $this->throwEx = isset($config["throwEx"]) && $config["throwEx"]; |
|
78 | + $this->throwEx = isset($config["throwEx"]) && $config["throwEx"]; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | /** |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | * @param array $rules Array of rules for permissions. |
85 | 85 | * @return bool false if user / group doesn't have permission, true if has permission |
86 | 86 | */ |
87 | - public function allow ($rules) { |
|
87 | + public function allow($rules) { |
|
88 | 88 | $this->setUserValues(); |
89 | 89 | $this->bindConfiguration($rules); |
90 | 90 | |
@@ -106,18 +106,18 @@ discard block |
||
106 | 106 | |
107 | 107 | private function bindConfiguration(array $rules) |
108 | 108 | { |
109 | - foreach(array("user_type", "redirect", "action", "controller", "message") as $key) { |
|
110 | - if(isset($rules[$key])) { |
|
109 | + foreach (array("user_type", "redirect", "action", "controller", "message") as $key) { |
|
110 | + if (isset($rules[$key])) { |
|
111 | 111 | $this->$key = $value; |
112 | 112 | } |
113 | 113 | } |
114 | - if(!is_object($value)) { |
|
114 | + if (!is_object($value)) { |
|
115 | 115 | Log::write("warning", sprintf("controller is not an object (%s)", gettype($value))); |
116 | 116 | } |
117 | 117 | |
118 | - foreach($rules['groups'] as $key => $value){ |
|
119 | - if($key == $this->userType){ |
|
120 | - foreach($value as $v){ |
|
118 | + foreach ($rules['groups'] as $key => $value) { |
|
119 | + if ($key == $this->userType) { |
|
120 | + foreach ($value as $v) { |
|
121 | 121 | array_push($this->actions, $v); |
122 | 122 | } |
123 | 123 | } |
@@ -128,8 +128,8 @@ discard block |
||
128 | 128 | { |
129 | 129 | $existRulesForGroups = false; |
130 | 130 | |
131 | - if(isset($rules['groups'])){ |
|
132 | - foreach($rules['groups'] as $key => $value){ |
|
131 | + if (isset($rules['groups'])) { |
|
132 | + foreach ($rules['groups'] as $key => $value) { |
|
133 | 133 | $this->searchForApplyGroupRules($key, $value); |
134 | 134 | } |
135 | 135 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | |
140 | 140 | private function searchForApplyGroupRules($key) |
141 | 141 | { |
142 | - if($key == $this->userType){ |
|
142 | + if ($key == $this->userType) { |
|
143 | 143 | if ($this->notInArrayAction()) { |
144 | 144 | $this->redirectIfIsSet(); |
145 | 145 | |
@@ -155,8 +155,8 @@ discard block |
||
155 | 155 | |
156 | 156 | private function applyViewsRules(array $rules) |
157 | 157 | { |
158 | - if(isset($rules['views'])){ |
|
159 | - foreach($rules['views'] as $key => $value){ |
|
158 | + if (isset($rules['views'])) { |
|
159 | + foreach ($rules['views'] as $key => $value) { |
|
160 | 160 | $this->searchForApplyViewRules($key, $value); |
161 | 161 | } |
162 | 162 | } |
@@ -164,8 +164,8 @@ discard block |
||
164 | 164 | |
165 | 165 | private function searchForApplyViewRules($key, $value) |
166 | 166 | { |
167 | - if($key == $this->action) { |
|
168 | - if(!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()){ |
|
167 | + if ($key == $this->action) { |
|
168 | + if (!$this->checkForHandler($this->controller, $value) || !$this->controller->$value()) { |
|
169 | 169 | $this->redirectIfIsSet(); |
170 | 170 | |
171 | 171 | $this->allow = false; |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | |
176 | 176 | private function checkForHandler($controller, $handler) |
177 | 177 | { |
178 | - if(!method_exists($controller, $handler)) { |
|
178 | + if (!method_exists($controller, $handler)) { |
|
179 | 179 | $msg = sprintf( |
180 | 180 | "Controller %s=%s has no method called '%s'", |
181 | 181 | is_object($controller) ? "class" : "type", |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | $handler |
184 | 184 | ); |
185 | 185 | Log::write("debug", $msg); |
186 | - if($this->throwEx) { |
|
186 | + if ($this->throwEx) { |
|
187 | 187 | throw new MissingHandlerException($msg); |
188 | 188 | } |
189 | 189 | return false; |
@@ -194,8 +194,8 @@ discard block |
||
194 | 194 | |
195 | 195 | private function redirectIfIsSet() |
196 | 196 | { |
197 | - if($this->redirect != ''){ |
|
198 | - if($this->message != ''){ |
|
197 | + if ($this->redirect != '') { |
|
198 | + if ($this->message != '') { |
|
199 | 199 | $this->Flash->set($this->message); |
200 | 200 | } |
201 | 201 |