Total Complexity | 68 |
Total Lines | 368 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class User extends \O2System\Security\Authentication\User |
||
27 | { |
||
28 | /** |
||
29 | * User::$app |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $app = 'app'; |
||
34 | |||
35 | /** |
||
36 | * User::__construct |
||
37 | * |
||
38 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
39 | */ |
||
40 | public function __construct() |
||
41 | { |
||
42 | parent::__construct(); |
||
43 | |||
44 | if ($config = config()->loadFile('AccessControl', true)) { |
||
|
|||
45 | $this->setConfig($config->getArrayCopy()); |
||
46 | } |
||
47 | |||
48 | if ( ! models('users')) { |
||
49 | throw new RuntimeException('ACL_E_UNDEFINED_USERS_MODEL'); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | // ------------------------------------------------------------------------ |
||
54 | |||
55 | /** |
||
56 | * User::setApp |
||
57 | * |
||
58 | * @param string $app |
||
59 | * |
||
60 | * @return static |
||
61 | */ |
||
62 | public function setApp($app) |
||
69 | } |
||
70 | |||
71 | // ------------------------------------------------------------------------ |
||
72 | |||
73 | /** |
||
74 | * User::authenticate |
||
75 | * |
||
76 | * @param string $username |
||
77 | * @param string $password |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function authenticate($username, $password) |
||
82 | { |
||
83 | if ($user = $this->find($username)) { |
||
84 | if ($user->account) { |
||
85 | if ($this->passwordVerify($password, $user->account->password)) { |
||
86 | if ($this->passwordRehash($password)) { |
||
87 | $user->account->update([ |
||
88 | 'id' => $user->id, |
||
89 | 'password' => $this->passwordHash($password), |
||
90 | ]); |
||
91 | } |
||
92 | |||
93 | $account = $user->account->getArrayCopy(); |
||
94 | } |
||
95 | } elseif ($this->passwordVerify($password, $user->password)) { |
||
96 | $account = $user->getArrayCopy(); |
||
97 | } |
||
98 | |||
99 | if (isset($account)) { |
||
100 | foreach ($account as $key => $value) { |
||
101 | if (strpos($key, 'record') !== false) { |
||
102 | unset($account[ $key ]); |
||
103 | } elseif (in_array($key, |
||
104 | ['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) { |
||
105 | unset($account[ $key ]); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | $this->login($account); |
||
110 | |||
111 | return true; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return false; |
||
116 | } |
||
117 | |||
118 | // ------------------------------------------------------------------------ |
||
119 | |||
120 | /** |
||
121 | * User::find |
||
122 | * |
||
123 | * @param string $username |
||
124 | * |
||
125 | * @return bool|mixed|\O2System\Database\DataObjects\Result|\O2System\Reactor\Models\Sql\DataObjects\Result |
||
126 | */ |
||
127 | public function find($username) |
||
128 | { |
||
129 | $column = 'username'; |
||
130 | if (is_numeric($username)) { |
||
131 | $column = 'id'; |
||
132 | } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
||
133 | $column = 'email'; |
||
134 | } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
||
135 | $column = 'msisdn'; |
||
136 | } |
||
137 | |||
138 | if ($user = models('users')->findWhere([ |
||
139 | $column => $username, |
||
140 | ], 1)) { |
||
141 | return $user; |
||
142 | } |
||
143 | |||
144 | return false; |
||
145 | } |
||
146 | |||
147 | // ------------------------------------------------------------------------ |
||
148 | |||
149 | /** |
||
150 | * User::loggedIn |
||
151 | * |
||
152 | * @return bool |
||
153 | * @throws \Psr\Cache\InvalidArgumentException |
||
154 | */ |
||
155 | public function loggedIn() |
||
156 | { |
||
157 | if (parent::loggedIn()) { |
||
158 | if(is_object($_SESSION['account'])) { |
||
159 | $account = new Account($_SESSION['account']->getArrayCopy()); |
||
160 | $username = $account->user->username; |
||
161 | } else { |
||
162 | $account = new Account(); |
||
163 | $username = $_SESSION['account']['username']; |
||
164 | } |
||
165 | |||
166 | if ($user = models('users')->findWhere(['username' => $username], 1)) { |
||
167 | if ($profile = $user->profile) { |
||
168 | $account->store('profile', $profile); |
||
169 | } |
||
170 | |||
171 | if ($employee = $user->employee) { |
||
172 | $account->store('employee', $employee); |
||
173 | } |
||
174 | |||
175 | if ($role = $user->role) { |
||
176 | $user->store('role', $role); |
||
177 | } |
||
178 | |||
179 | $account->store('user', $user); |
||
180 | |||
181 | session()->set('account', $account); |
||
182 | } |
||
183 | |||
184 | // Store Globals Account |
||
185 | globals()->store('account', $account); |
||
186 | |||
187 | // Store Presenter Account |
||
188 | if (services()->has('view')) { |
||
189 | presenter()->store('account', $account); |
||
190 | } |
||
191 | |||
192 | return true; |
||
193 | } |
||
194 | |||
195 | return false; |
||
196 | } |
||
197 | |||
198 | // ------------------------------------------------------------------------ |
||
199 | |||
200 | /** |
||
201 | * User::forceLogin |
||
202 | * |
||
203 | * @param string $username |
||
204 | * @param string $column |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function forceLogin($username, $column = 'username') |
||
209 | { |
||
210 | if (is_numeric($username)) { |
||
211 | $column = 'id'; |
||
212 | } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
||
213 | $column = 'email'; |
||
214 | } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
||
215 | $column = 'msisdn'; |
||
216 | } elseif (strpos($username, 'token-') !== false) { |
||
217 | $username = str_replace('token-', '', $username); |
||
218 | $column = 'token'; |
||
219 | } elseif (strpos($username, 'sso-') !== false) { |
||
220 | $username = str_replace('sso-', '', $username); |
||
221 | $column = 'sso'; |
||
222 | } |
||
223 | |||
224 | if ($account = models('users')->findWhere([$column => $username], 1)) { |
||
225 | $account = $account->getArrayCopy(); |
||
226 | |||
227 | foreach ($account as $key => $value) { |
||
228 | if (strpos($key, 'record') !== false) { |
||
229 | unset($account[ $key ]); |
||
230 | } elseif (in_array($key, ['password', 'pin', 'token', 'sso'])) { |
||
231 | unset($account[ $key ]); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | if ($column === 'token') { |
||
236 | models('users')->update([ |
||
237 | 'id' => $account[ 'id' ], |
||
238 | 'token' => null, |
||
239 | ]); |
||
240 | } |
||
241 | |||
242 | $this->login($account); |
||
243 | |||
244 | return true; |
||
245 | } |
||
246 | |||
247 | return false; |
||
248 | } |
||
249 | |||
250 | // ------------------------------------------------------------------------ |
||
251 | |||
252 | /** |
||
253 | * User::hasAccess |
||
254 | * |
||
255 | * @param array $segments |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function hasAccess(array $segments) |
||
260 | { |
||
261 | if ($this->loggedIn()) { |
||
262 | if ($account = globals()->offsetGet('account')) { |
||
263 | if (in_array($account->user->role->id, [1, 2])) { |
||
264 | return true; |
||
265 | } |
||
266 | |||
267 | if ($model = models('modules')) { |
||
268 | if ($segment = $model->segments->find(implode('/', $segments), 'segments')) { |
||
269 | if ($authority = $model->segments->authorities->users->findWhere([ |
||
270 | 'id_sys_module_segment' => $segment->id, |
||
271 | 'id_sys_module_user' => $account->user->moduleUser->id, |
||
272 | ])) { |
||
273 | if ($authority->first()->permission === 'WRITE') { |
||
274 | return true; |
||
275 | } elseif ($authority->first()->permission === 'GRANTED') { |
||
276 | // Access only granted cannot do modifier access |
||
277 | foreach ([ |
||
278 | 'form', |
||
279 | 'add', |
||
280 | 'add-as-new', |
||
281 | 'edit', |
||
282 | 'update', |
||
283 | 'insert', |
||
284 | 'create', |
||
285 | 'delete', |
||
286 | ] as $segment |
||
287 | ) { |
||
288 | if (in_array($segment, $segments)) { |
||
289 | return false; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | return true; |
||
294 | } |
||
295 | } |
||
296 | |||
297 | if ($authority = $model->segments->authorities->roles->findWhere([ |
||
298 | 'id_sys_module_segment' => $segment->id, |
||
299 | 'id_sys_module_role' => $account->user->role->id, |
||
300 | ])) { |
||
301 | if ($authority->first()->permission === 'WRITE') { |
||
302 | return true; |
||
303 | } elseif ($authority->first()->permission === 'GRANTED') { |
||
304 | // Access only granted cannot do modifier access |
||
305 | foreach ([ |
||
306 | 'form', |
||
307 | 'add', |
||
308 | 'add-as-new', |
||
309 | 'edit', |
||
310 | 'update', |
||
311 | 'insert', |
||
312 | 'create', |
||
313 | 'delete', |
||
314 | ] as $segment |
||
315 | ) { |
||
316 | if (in_array($segment, $segments)) { |
||
317 | return false; |
||
318 | } |
||
319 | } |
||
320 | |||
321 | return true; |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 | } |
||
328 | |||
329 | return false; |
||
330 | } |
||
331 | |||
332 | // ------------------------------------------------------------------------ |
||
333 | |||
334 | /** |
||
335 | * User::hasWriteAccess |
||
336 | * |
||
337 | * @return bool |
||
338 | * @throws \Psr\Cache\InvalidArgumentException |
||
339 | */ |
||
340 | public function hasWriteAccess() |
||
341 | { |
||
342 | $segments = server_request()->getUri()->segments->getArrayCopy(); |
||
343 | |||
344 | if ($this->loggedIn()) { |
||
345 | if ($account = globals()->offsetGet('account')) { |
||
346 | if (in_array($account->user->role->id, [1, 2])) { |
||
347 | return true; |
||
348 | } |
||
349 | |||
350 | if ($model = models('modules')) { |
||
351 | |||
352 | if ($segment = $model->segments->find(implode('/', $segments), 'segments')) { |
||
353 | if ($authority = $model->segments->authorities->users->findWhere([ |
||
354 | 'id_sys_module_segment' => $segment->id, |
||
355 | 'id_sys_module_user' => $account->user->moduleUser->id, |
||
356 | ])) { |
||
357 | if ($authority->first()->permission === 'WRITE') { |
||
358 | return true; |
||
359 | } |
||
360 | } |
||
361 | |||
362 | if ($authority = $model->segments->authorities->roles->findWhere([ |
||
363 | 'id_sys_module_segment' => $segment->id, |
||
364 | 'id_sys_module_role' => $account->user->role->id, |
||
365 | ])) { |
||
366 | if ($authority->first()->permission === 'WRITE') { |
||
367 | return true; |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | } |
||
373 | } |
||
374 | |||
375 | return false; |
||
376 | } |
||
377 | |||
378 | // ------------------------------------------------------------------------ |
||
379 | |||
380 | /** |
||
381 | * User::getIframeCode |
||
382 | * |
||
383 | * @return string |
||
384 | * @throws \Psr\Cache\InvalidArgumentException |
||
385 | */ |
||
386 | public function getIframeCode() |
||
394 | } |
||
395 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.