Total Complexity | 53 |
Total Lines | 590 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like UsersController 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 UsersController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class UsersController extends BaseController |
||
15 | { |
||
16 | public $controller_title = 'strusers'; |
||
17 | |||
18 | /** |
||
19 | * Default method to render the controller according to the action parameter. |
||
20 | */ |
||
21 | public function render(): void |
||
22 | { |
||
23 | $this->printHeader(); |
||
24 | $this->printBody(); |
||
25 | |||
26 | switch ($this->action) { |
||
27 | case 'changepassword': |
||
28 | if (isset($_REQUEST['ok'])) { |
||
29 | $this->doChangePassword(false); |
||
30 | } else { |
||
31 | $this->doAccount(); |
||
32 | } |
||
33 | |||
34 | break; |
||
35 | case 'confchangepassword': |
||
36 | $this->doChangePassword(true); |
||
37 | |||
38 | break; |
||
39 | case 'account': |
||
40 | $this->doAccount(); |
||
41 | |||
42 | break; |
||
43 | case 'save_create': |
||
44 | if (isset($_REQUEST['cancel'])) { |
||
45 | $this->doDefault(); |
||
46 | } else { |
||
47 | $this->doSaveCreate(); |
||
48 | } |
||
49 | |||
50 | break; |
||
51 | case 'create': |
||
52 | $this->doCreate(); |
||
53 | |||
54 | break; |
||
55 | case 'drop': |
||
56 | if (isset($_REQUEST['cancel'])) { |
||
57 | $this->doDefault(); |
||
58 | } else { |
||
59 | $this->doDrop(false); |
||
60 | } |
||
61 | |||
62 | break; |
||
63 | case 'confirm_drop': |
||
64 | $this->doDrop(true); |
||
65 | |||
66 | break; |
||
67 | case 'save_edit': |
||
68 | if (isset($_REQUEST['cancel'])) { |
||
69 | $this->doDefault(); |
||
70 | } else { |
||
71 | $this->doSaveEdit(); |
||
72 | } |
||
73 | |||
74 | break; |
||
75 | case 'edit': |
||
76 | $this->doEdit(); |
||
77 | |||
78 | break; |
||
79 | |||
80 | default: |
||
81 | $this->doDefault(); |
||
82 | |||
83 | break; |
||
84 | } |
||
85 | |||
86 | $this->printFooter(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Show default list of users in the database. |
||
91 | * |
||
92 | * @param mixed $msg |
||
93 | */ |
||
94 | public function doDefault($msg = ''): void |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * If a user is not a superuser, then we have an 'account management' page |
||
188 | * where they can change their password, etc. We don't prevent them from |
||
189 | * messing with the URL to gain access to other user admin stuff, because |
||
190 | * the PostgreSQL permissions will prevent them changing anything anyway. |
||
191 | * |
||
192 | * @param mixed $msg |
||
193 | */ |
||
194 | public function doAccount($msg = ''): void |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Show confirmation of change password and actually change password. |
||
252 | * |
||
253 | * @param mixed $confirm |
||
254 | * @param mixed $msg |
||
255 | */ |
||
256 | public function doChangePassword($confirm, $msg = ''): void |
||
257 | { |
||
258 | $data = $this->misc->getDatabaseAccessor(); |
||
259 | |||
260 | $server_info = $this->misc->getServerInfo(); |
||
261 | |||
262 | if ($confirm) { |
||
263 | $_REQUEST['user'] = $server_info['username']; |
||
264 | $this->printTrail('user'); |
||
265 | $this->printTitle($this->lang['strchangepassword'], 'pg.user.alter'); |
||
266 | $this->printMsg($msg); |
||
267 | |||
268 | $this->coalesceArr($_POST, 'password', ''); |
||
269 | |||
270 | $this->coalesceArr($_POST, 'confirm', ''); |
||
271 | |||
272 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/users" method="post">' . \PHP_EOL; |
||
273 | echo '<table>' . \PHP_EOL; |
||
274 | echo \sprintf( |
||
275 | ' <tr> |
||
276 | <th class="data left required">%s</th>', |
||
277 | $this->lang['strpassword'] |
||
278 | ) . \PHP_EOL; |
||
279 | echo "\t\t<td><input type=\"password\" name=\"password\" size=\"32\" value=\"", |
||
280 | \htmlspecialchars($_POST['password']), |
||
281 | "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
282 | echo \sprintf( |
||
283 | ' <tr> |
||
284 | <th class="data left required">%s</th>', |
||
285 | $this->lang['strconfirm'] |
||
286 | ) . \PHP_EOL; |
||
287 | echo "\t\t<td><input type=\"password\" name=\"confirm\" size=\"32\" value=\"\" /></td>\n\t</tr>" . \PHP_EOL; |
||
288 | echo '</table>' . \PHP_EOL; |
||
289 | echo '<p><input type="hidden" name="action" value="changepassword" />' . \PHP_EOL; |
||
290 | echo $this->view->form; |
||
291 | echo \sprintf( |
||
292 | '<input type="submit" name="ok" value="%s" />', |
||
293 | $this->lang['strok'] |
||
294 | ) . \PHP_EOL; |
||
295 | echo \sprintf( |
||
296 | '<input type="submit" name="cancel" value="%s" />', |
||
297 | $this->lang['strcancel'] |
||
298 | ) . \PHP_EOL; |
||
299 | echo '</p></form>' . \PHP_EOL; |
||
300 | } else { |
||
301 | // Check that password is minimum length |
||
302 | if (\mb_strlen($_POST['password']) < $this->conf['min_password_length']) { |
||
303 | $this->doChangePassword(true, $this->lang['strpasswordshort']); |
||
304 | } elseif ($_POST['password'] !== $_POST['confirm']) { |
||
305 | // Check that password matches confirmation password |
||
306 | $this->doChangePassword(true, $this->lang['strpasswordconfirm']); |
||
307 | } else { |
||
308 | $status = $data->changePassword( |
||
309 | $server_info['username'], |
||
310 | $_POST['password'] |
||
311 | ); |
||
312 | |||
313 | if (0 === $status) { |
||
314 | $this->doAccount($this->lang['strpasswordchanged']); |
||
315 | } else { |
||
316 | $this->doAccount($this->lang['strpasswordchangedbad']); |
||
317 | } |
||
318 | } |
||
319 | } |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Function to allow editing of a user. |
||
324 | * |
||
325 | * @param mixed $msg |
||
326 | */ |
||
327 | public function doEdit($msg = ''): void |
||
328 | { |
||
329 | $data = $this->misc->getDatabaseAccessor(); |
||
330 | |||
331 | $this->printTrail('user'); |
||
332 | $this->printTitle($this->lang['stralter'], 'pg.user.alter'); |
||
333 | $this->printMsg($msg); |
||
334 | |||
335 | $userdata = $data->getUser($_REQUEST['username']); |
||
336 | |||
337 | if (!\is_object($userdata) || 0 < $userdata->recordCount()) { |
||
338 | $server_info = $this->misc->getServerInfo(); |
||
339 | $canRename = $data->hasUserRename() && ($_REQUEST['username'] !== $server_info['username']); |
||
340 | $userdata->fields['usesuper'] = $data->phpBool($userdata->fields['usesuper'] ?? false); |
||
341 | $userdata->fields['usecreatedb'] = $data->phpBool($userdata->fields['usecreatedb'] ?? false); |
||
342 | |||
343 | if (!isset($_POST['formExpires'])) { |
||
344 | if ($canRename) { |
||
345 | $_POST['newname'] = $userdata->fields['usename']; |
||
346 | } |
||
347 | |||
348 | if ($userdata->fields['usesuper']) { |
||
349 | $_POST['formSuper'] = ''; |
||
350 | } |
||
351 | |||
352 | if ($userdata->fields['usecreatedb']) { |
||
353 | $_POST['formCreateDB'] = ''; |
||
354 | } |
||
355 | |||
356 | $_POST['formExpires'] = 'infinity' === $userdata->fields['useexpires'] ? '' : $userdata->fields['useexpires']; |
||
357 | $_POST['formPassword'] = ''; |
||
358 | } |
||
359 | |||
360 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/users" method="post">' . \PHP_EOL; |
||
361 | echo '<table>' . \PHP_EOL; |
||
362 | echo \sprintf( |
||
363 | ' <tr> |
||
364 | <th class="data left">%s</th>', |
||
365 | $this->lang['strusername'] |
||
366 | ) . \PHP_EOL; |
||
367 | echo "\t\t<td class=\"data1\">", ($canRename ? \sprintf( |
||
368 | '<input name="newname" size="15" maxlength="%s" value="', |
||
369 | $data->_maxNameLen |
||
370 | ) . \htmlspecialchars($_POST['newname']) . '" />' : $this->misc->printVal($userdata->fields['usename'])), "</td>\n\t</tr>" . \PHP_EOL; |
||
371 | echo \sprintf( |
||
372 | ' <tr> |
||
373 | <th class="data left"><label for="formSuper">%s</label></th>', |
||
374 | $this->lang['strsuper'] |
||
375 | ) . \PHP_EOL; |
||
376 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"", |
||
377 | (isset($_POST['formSuper'])) ? ' checked="checked"' : '', |
||
378 | " /></td>\n\t</tr>" . \PHP_EOL; |
||
379 | echo \sprintf( |
||
380 | ' <tr> |
||
381 | <th class="data left"><label for="formCreateDB">%s</label></th>', |
||
382 | $this->lang['strcreatedb'] |
||
383 | ) . \PHP_EOL; |
||
384 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"", |
||
385 | (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', |
||
386 | " /></td>\n\t</tr>" . \PHP_EOL; |
||
387 | echo \sprintf( |
||
388 | ' <tr> |
||
389 | <th class="data left">%s</th>', |
||
390 | $this->lang['strexpires'] |
||
391 | ) . \PHP_EOL; |
||
392 | echo "\t\t<td class=\"data1\"><input size=\"16\" name=\"formExpires\" value=\"", \htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
393 | echo \sprintf( |
||
394 | ' <tr> |
||
395 | <th class="data left">%s</th>', |
||
396 | $this->lang['strpassword'] |
||
397 | ) . \PHP_EOL; |
||
398 | echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formPassword\" value=\"", \htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
399 | echo \sprintf( |
||
400 | ' <tr> |
||
401 | <th class="data left">%s</th>', |
||
402 | $this->lang['strconfirm'] |
||
403 | ) . \PHP_EOL; |
||
404 | echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formConfirm\" value=\"\" /></td>\n\t</tr>" . \PHP_EOL; |
||
405 | echo '</table>' . \PHP_EOL; |
||
406 | echo '<p><input type="hidden" name="action" value="save_edit" />' . \PHP_EOL; |
||
407 | echo '<input type="hidden" name="username" value="', \htmlspecialchars($_REQUEST['username']), '" />' . \PHP_EOL; |
||
408 | echo $this->view->form; |
||
409 | echo \sprintf( |
||
410 | '<input type="submit" name="alter" value="%s" />', |
||
411 | $this->lang['stralter'] |
||
412 | ) . \PHP_EOL; |
||
413 | echo \sprintf( |
||
414 | '<input type="submit" name="cancel" value="%s" /></p>%s', |
||
415 | $this->lang['strcancel'], |
||
416 | \PHP_EOL |
||
417 | ); |
||
418 | echo '</form>' . \PHP_EOL; |
||
419 | } else { |
||
420 | echo \sprintf( |
||
421 | '<p>%s</p>', |
||
422 | $this->lang['strnodata'] |
||
423 | ) . \PHP_EOL; |
||
424 | } |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Function to save after editing a user. |
||
429 | */ |
||
430 | public function doSaveEdit(): void |
||
431 | { |
||
432 | $data = $this->misc->getDatabaseAccessor(); |
||
433 | |||
434 | // Check name and password |
||
435 | if (isset($_POST['newname']) && '' === $_POST['newname']) { |
||
436 | $this->doEdit($this->lang['struserneedsname']); |
||
437 | } elseif ($_POST['formPassword'] !== $_POST['formConfirm']) { |
||
438 | $this->doEdit($this->lang['strpasswordconfirm']); |
||
439 | } else { |
||
440 | if (isset($_POST['newname'])) { |
||
441 | $status = $data->setRenameUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires'], $_POST['newname']); |
||
442 | } else { |
||
443 | $status = $data->setUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires']); |
||
444 | } |
||
445 | |||
446 | if (0 === $status) { |
||
447 | $this->doDefault($this->lang['struserupdated']); |
||
448 | } else { |
||
449 | $this->doEdit($this->lang['struserupdatedbad']); |
||
450 | } |
||
451 | } |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Show confirmation of drop and perform actual drop. |
||
456 | * |
||
457 | * @param mixed $confirm |
||
458 | */ |
||
459 | public function doDrop($confirm): void |
||
460 | { |
||
461 | $data = $this->misc->getDatabaseAccessor(); |
||
462 | |||
463 | if ($confirm) { |
||
464 | $this->printTrail('user'); |
||
465 | $this->printTitle($this->lang['strdrop'], 'pg.user.drop'); |
||
466 | |||
467 | echo '<p>', \sprintf( |
||
468 | $this->lang['strconfdropuser'], |
||
469 | $this->misc->printVal($_REQUEST['username']) |
||
470 | ), '</p>' . \PHP_EOL; |
||
471 | |||
472 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/users" method="post">' . \PHP_EOL; |
||
473 | echo '<p><input type="hidden" name="action" value="drop" />' . \PHP_EOL; |
||
474 | echo '<input type="hidden" name="username" value="', \htmlspecialchars($_REQUEST['username']), '" />' . \PHP_EOL; |
||
475 | echo $this->view->form; |
||
476 | echo \sprintf( |
||
477 | '<input type="submit" name="drop" value="%s" />', |
||
478 | $this->lang['strdrop'] |
||
479 | ) . \PHP_EOL; |
||
480 | echo \sprintf( |
||
481 | '<input type="submit" name="cancel" value="%s" /></p>%s', |
||
482 | $this->lang['strcancel'], |
||
483 | \PHP_EOL |
||
484 | ); |
||
485 | echo '</form>' . \PHP_EOL; |
||
486 | } else { |
||
487 | $status = $data->dropUser($_REQUEST['username']); |
||
488 | |||
489 | if (0 === $status) { |
||
490 | $this->doDefault($this->lang['struserdropped']); |
||
491 | } else { |
||
492 | $this->doDefault($this->lang['struserdroppedbad']); |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Displays a screen where they can enter a new user. |
||
499 | * |
||
500 | * @param mixed $msg |
||
501 | */ |
||
502 | public function doCreate($msg = ''): void |
||
503 | { |
||
504 | $data = $this->misc->getDatabaseAccessor(); |
||
505 | |||
506 | $this->coalesceArr($_POST, 'formUsername', ''); |
||
507 | |||
508 | $this->coalesceArr($_POST, 'formPassword', ''); |
||
509 | |||
510 | $this->coalesceArr($_POST, 'formConfirm', ''); |
||
511 | |||
512 | $this->coalesceArr($_POST, 'formExpires', ''); |
||
513 | |||
514 | $this->printTrail('server'); |
||
515 | $this->printTitle($this->lang['strcreateuser'], 'pg.user.create'); |
||
516 | $this->printMsg($msg); |
||
517 | |||
518 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/users" method="post">' . \PHP_EOL; |
||
519 | echo '<table>' . \PHP_EOL; |
||
520 | echo \sprintf( |
||
521 | ' <tr> |
||
522 | <th class="data left required">%s</th>', |
||
523 | $this->lang['strusername'] |
||
524 | ) . \PHP_EOL; |
||
525 | echo \sprintf( |
||
526 | ' <td class="data1"><input size="15" maxlength="%s" name="formUsername" value="', |
||
527 | $data->_maxNameLen |
||
528 | ), \htmlspecialchars($_POST['formUsername']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
529 | echo \sprintf( |
||
530 | ' <tr> |
||
531 | <th class="data left">%s</th>', |
||
532 | $this->lang['strpassword'] |
||
533 | ) . \PHP_EOL; |
||
534 | echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formPassword\" value=\"", \htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
535 | echo \sprintf( |
||
536 | ' <tr> |
||
537 | <th class="data left">%s</th>', |
||
538 | $this->lang['strconfirm'] |
||
539 | ) . \PHP_EOL; |
||
540 | echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formConfirm\" value=\"", \htmlspecialchars($_POST['formConfirm']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
541 | echo \sprintf( |
||
542 | ' <tr> |
||
543 | <th class="data left"><label for="formSuper">%s</label></th>', |
||
544 | $this->lang['strsuper'] |
||
545 | ) . \PHP_EOL; |
||
546 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"", |
||
547 | (isset($_POST['formSuper'])) ? ' checked="checked"' : '', |
||
548 | " /></td>\n\t</tr>" . \PHP_EOL; |
||
549 | echo \sprintf( |
||
550 | ' <tr> |
||
551 | <th class="data left"><label for="formCreateDB">%s</label></th>', |
||
552 | $this->lang['strcreatedb'] |
||
553 | ) . \PHP_EOL; |
||
554 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"", |
||
555 | (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', |
||
556 | " /></td>\n\t</tr>" . \PHP_EOL; |
||
557 | echo \sprintf( |
||
558 | ' <tr> |
||
559 | <th class="data left">%s</th>', |
||
560 | $this->lang['strexpires'] |
||
561 | ) . \PHP_EOL; |
||
562 | echo "\t\t<td class=\"data1\"><input size=\"30\" name=\"formExpires\" value=\"", \htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
563 | echo '</table>' . \PHP_EOL; |
||
564 | echo '<p><input type="hidden" name="action" value="save_create" />' . \PHP_EOL; |
||
565 | echo $this->view->form; |
||
566 | echo \sprintf( |
||
567 | '<input type="submit" name="create" value="%s" />', |
||
568 | $this->lang['strcreate'] |
||
569 | ) . \PHP_EOL; |
||
570 | echo \sprintf( |
||
571 | '<input type="submit" name="cancel" value="%s" /></p>%s', |
||
572 | $this->lang['strcancel'], |
||
573 | \PHP_EOL |
||
574 | ); |
||
575 | echo '</form>' . \PHP_EOL; |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Actually creates the new user in the database. |
||
580 | */ |
||
581 | public function doSaveCreate(): void |
||
604 | } |
||
605 | } |
||
606 | } |
||
607 | } |
||
608 |