Total Complexity | 58 |
Total Lines | 498 |
Duplicated Lines | 0 % |
Changes | 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_name = 'UsersController'; |
||
17 | |||
18 | public function render() |
||
1 ignored issue
–
show
|
|||
19 | { |
||
20 | $this->printHeader($lang['strusers']); |
||
21 | $this->printBody(); |
||
22 | |||
23 | switch ($action) { |
||
24 | case 'changepassword': |
||
25 | if (isset($_REQUEST['ok'])) { |
||
26 | $this->doChangePassword(false); |
||
27 | } else { |
||
28 | $this->doAccount(); |
||
29 | } |
||
30 | |||
31 | break; |
||
32 | case 'confchangepassword': |
||
33 | $this->doChangePassword(true); |
||
34 | |||
35 | break; |
||
36 | case 'account': |
||
37 | $this->doAccount(); |
||
38 | |||
39 | break; |
||
40 | case 'save_create': |
||
41 | if (isset($_REQUEST['cancel'])) { |
||
42 | $this->doDefault(); |
||
43 | } else { |
||
44 | $this->doSaveCreate(); |
||
45 | } |
||
46 | |||
47 | break; |
||
48 | case 'create': |
||
49 | $this->doCreate(); |
||
50 | |||
51 | break; |
||
52 | case 'drop': |
||
53 | if (isset($_REQUEST['cancel'])) { |
||
54 | $this->doDefault(); |
||
55 | } else { |
||
56 | $this->doDrop(false); |
||
57 | } |
||
58 | |||
59 | break; |
||
60 | case 'confirm_drop': |
||
61 | $this->doDrop(true); |
||
62 | |||
63 | break; |
||
64 | case 'save_edit': |
||
65 | if (isset($_REQUEST['cancel'])) { |
||
66 | $this->doDefault(); |
||
67 | } else { |
||
68 | $this->doSaveEdit(); |
||
69 | } |
||
70 | |||
71 | break; |
||
72 | case 'edit': |
||
73 | $this->doEdit(); |
||
74 | |||
75 | break; |
||
76 | default: |
||
77 | $this->doDefault(); |
||
78 | |||
79 | break; |
||
80 | } |
||
81 | |||
82 | $this->printFooter(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Show default list of users in the database |
||
87 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
88 | */ |
||
89 | public function doDefault($msg = '') |
||
90 | { |
||
91 | $conf = $this->conf; |
||
92 | |||
93 | $lang = $this->lang; |
||
94 | $data = $this->misc->getDatabaseAccessor(); |
||
95 | |||
96 | $renderUseExpires = function ($val) use ($lang) { |
||
97 | return 'infinity' == $val ? $lang['strnever'] : htmlspecialchars($val); |
||
98 | }; |
||
99 | |||
100 | $this->printTrail('server'); |
||
101 | $this->printTabs('server', 'users'); |
||
102 | $this->printMsg($msg); |
||
103 | |||
104 | $users = $data->getUsers(); |
||
105 | |||
106 | $columns = [ |
||
107 | 'user' => [ |
||
108 | 'title' => $lang['strusername'], |
||
109 | 'field' => Decorator::field('usename'), |
||
110 | ], |
||
111 | 'superuser' => [ |
||
112 | 'title' => $lang['strsuper'], |
||
113 | 'field' => Decorator::field('usesuper'), |
||
114 | 'type' => 'yesno', |
||
115 | ], |
||
116 | 'createdb' => [ |
||
117 | 'title' => $lang['strcreatedb'], |
||
118 | 'field' => Decorator::field('usecreatedb'), |
||
119 | 'type' => 'yesno', |
||
120 | ], |
||
121 | 'expires' => [ |
||
122 | 'title' => $lang['strexpires'], |
||
123 | 'field' => Decorator::field('useexpires'), |
||
124 | 'type' => 'callback', |
||
125 | 'params' => ['function' => $renderUseExpires, 'null' => $lang['strnever']], |
||
126 | ], |
||
127 | 'defaults' => [ |
||
128 | 'title' => $lang['strsessiondefaults'], |
||
129 | 'field' => Decorator::field('useconfig'), |
||
130 | ], |
||
131 | 'actions' => [ |
||
132 | 'title' => $lang['stractions'], |
||
133 | ], |
||
134 | ]; |
||
135 | |||
136 | $actions = [ |
||
137 | 'alter' => [ |
||
138 | 'content' => $lang['stralter'], |
||
139 | 'attr' => [ |
||
140 | 'href' => [ |
||
141 | 'url' => 'users.php', |
||
142 | 'urlvars' => [ |
||
143 | 'action' => 'edit', |
||
144 | 'username' => Decorator::field('usename'), |
||
145 | ], |
||
146 | ], |
||
147 | ], |
||
148 | ], |
||
149 | 'drop' => [ |
||
150 | 'content' => $lang['strdrop'], |
||
151 | 'attr' => [ |
||
152 | 'href' => [ |
||
153 | 'url' => 'users.php', |
||
154 | 'urlvars' => [ |
||
155 | 'action' => 'confirm_drop', |
||
156 | 'username' => Decorator::field('usename'), |
||
157 | ], |
||
158 | ], |
||
159 | ], |
||
160 | ], |
||
161 | ]; |
||
162 | |||
163 | echo $this->printTable($users, $columns, $actions, 'users-users', $lang['strnousers']); |
||
164 | |||
165 | $this->printNavLinks(['create' => [ |
||
1 ignored issue
–
show
|
|||
166 | 'attr' => [ |
||
167 | 'href' => [ |
||
168 | 'url' => 'users.php', |
||
169 | 'urlvars' => [ |
||
170 | 'action' => 'create', |
||
171 | 'server' => $_REQUEST['server'], |
||
172 | ], |
||
173 | ], |
||
174 | ], |
||
175 | 'content' => $lang['strcreateuser'], |
||
176 | ]], 'users-users', get_defined_vars()); |
||
1 ignored issue
–
show
|
|||
177 | } |
||
178 | |||
179 | /** |
||
180 | * If a user is not a superuser, then we have an 'account management' page |
||
181 | * where they can change their password, etc. We don't prevent them from |
||
182 | * messing with the URL to gain access to other user admin stuff, because |
||
183 | * the PostgreSQL permissions will prevent them changing anything anyway. |
||
184 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
185 | */ |
||
186 | public function doAccount($msg = '') |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Show confirmation of change password and actually change password |
||
235 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
236 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
237 | */ |
||
238 | public function doChangePassword($confirm, $msg = '') |
||
239 | { |
||
240 | $conf = $this->conf; |
||
241 | |||
242 | $lang = $this->lang; |
||
243 | $data = $this->misc->getDatabaseAccessor(); |
||
244 | |||
245 | $server_info = $this->misc->getServerInfo(); |
||
246 | |||
247 | if ($confirm) { |
||
248 | $_REQUEST['user'] = $server_info['username']; |
||
249 | $this->printTrail('user'); |
||
250 | $this->printTitle($lang['strchangepassword'], 'pg.user.alter'); |
||
251 | $this->printMsg($msg); |
||
252 | |||
253 | if (!isset($_POST['password'])) { |
||
254 | $_POST['password'] = ''; |
||
255 | } |
||
256 | |||
257 | if (!isset($_POST['confirm'])) { |
||
258 | $_POST['confirm'] = ''; |
||
259 | } |
||
260 | |||
261 | echo '<form action="' . SUBFOLDER . "/src/views/users.php\" method=\"post\">\n"; |
||
262 | echo "<table>\n"; |
||
263 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strpassword']}</th>\n"; |
||
264 | echo "\t\t<td><input type=\"password\" name=\"password\" size=\"32\" value=\"", |
||
265 | htmlspecialchars($_POST['password']), "\" /></td>\n\t</tr>\n"; |
||
266 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strconfirm']}</th>\n"; |
||
267 | echo "\t\t<td><input type=\"password\" name=\"confirm\" size=\"32\" value=\"\" /></td>\n\t</tr>\n"; |
||
268 | echo "</table>\n"; |
||
269 | echo "<p><input type=\"hidden\" name=\"action\" value=\"changepassword\" />\n"; |
||
270 | echo $this->misc->form; |
||
271 | echo "<input type=\"submit\" name=\"ok\" value=\"{$lang['strok']}\" />\n"; |
||
272 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
||
273 | echo "</p></form>\n"; |
||
274 | } else { |
||
275 | // Check that password is minimum length |
||
276 | if (strlen($_POST['password']) < $conf['min_password_length']) { |
||
277 | $this->doChangePassword(true, $lang['strpasswordshort']); |
||
278 | } |
||
279 | |||
280 | // Check that password matches confirmation password |
||
281 | elseif ($_POST['password'] != $_POST['confirm']) { |
||
282 | $this->doChangePassword(true, $lang['strpasswordconfirm']); |
||
283 | } else { |
||
284 | $status = $data->changePassword( |
||
285 | $server_info['username'], |
||
286 | $_POST['password'] |
||
287 | ); |
||
288 | if (0 == $status) { |
||
289 | $this->doAccount($lang['strpasswordchanged']); |
||
290 | } else { |
||
291 | $this->doAccount($lang['strpasswordchangedbad']); |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Function to allow editing of a user |
||
299 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
300 | */ |
||
301 | public function doEdit($msg = '') |
||
302 | { |
||
303 | $conf = $this->conf; |
||
304 | |||
305 | $lang = $this->lang; |
||
306 | $data = $this->misc->getDatabaseAccessor(); |
||
307 | |||
308 | $this->printTrail('user'); |
||
309 | $this->printTitle($lang['stralter'], 'pg.user.alter'); |
||
310 | $this->printMsg($msg); |
||
311 | |||
312 | $userdata = $data->getUser($_REQUEST['username']); |
||
313 | |||
314 | if ($userdata->recordCount() > 0) { |
||
315 | $server_info = $this->misc->getServerInfo(); |
||
316 | $canRename = $data->hasUserRename() && ($_REQUEST['username'] != $server_info['username']); |
||
317 | $userdata->fields['usesuper'] = $data->phpBool($userdata->fields['usesuper']); |
||
318 | $userdata->fields['usecreatedb'] = $data->phpBool($userdata->fields['usecreatedb']); |
||
319 | |||
320 | if (!isset($_POST['formExpires'])) { |
||
321 | if ($canRename) { |
||
322 | $_POST['newname'] = $userdata->fields['usename']; |
||
323 | } |
||
324 | |||
325 | if ($userdata->fields['usesuper']) { |
||
326 | $_POST['formSuper'] = ''; |
||
327 | } |
||
328 | |||
329 | if ($userdata->fields['usecreatedb']) { |
||
330 | $_POST['formCreateDB'] = ''; |
||
331 | } |
||
332 | |||
333 | $_POST['formExpires'] = 'infinity' == $userdata->fields['useexpires'] ? '' : $userdata->fields['useexpires']; |
||
334 | $_POST['formPassword'] = ''; |
||
335 | } |
||
336 | |||
337 | echo '<form action="' . SUBFOLDER . "/src/views/users.php\" method=\"post\">\n"; |
||
338 | echo "<table>\n"; |
||
339 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strusername']}</th>\n"; |
||
340 | echo "\t\t<td class=\"data1\">", ($canRename ? "<input name=\"newname\" size=\"15\" maxlength=\"{$data->_maxNameLen}\" value=\"" . htmlspecialchars($_POST['newname']) . '" />' : $this->misc->printVal($userdata->fields['usename'])), "</td>\n\t</tr>\n"; |
||
341 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$lang['strsuper']}</label></th>\n"; |
||
342 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"", |
||
343 | (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
344 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$lang['strcreatedb']}</label></th>\n"; |
||
345 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"", |
||
346 | (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
347 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strexpires']}</th>\n"; |
||
348 | echo "\t\t<td class=\"data1\"><input size=\"16\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n"; |
||
349 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strpassword']}</th>\n"; |
||
350 | echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n"; |
||
351 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strconfirm']}</th>\n"; |
||
352 | echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"16\" name=\"formConfirm\" value=\"\" /></td>\n\t</tr>\n"; |
||
353 | echo "</table>\n"; |
||
354 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n"; |
||
355 | echo '<input type="hidden" name="username" value="', htmlspecialchars($_REQUEST['username']), "\" />\n"; |
||
356 | echo $this->misc->form; |
||
357 | echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n"; |
||
358 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
359 | echo "</form>\n"; |
||
360 | } else { |
||
361 | echo "<p>{$lang['strnodata']}</p>\n"; |
||
362 | } |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Function to save after editing a user |
||
367 | */ |
||
368 | public function doSaveEdit() |
||
369 | { |
||
370 | $conf = $this->conf; |
||
371 | |||
372 | $lang = $this->lang; |
||
373 | $data = $this->misc->getDatabaseAccessor(); |
||
374 | |||
375 | // Check name and password |
||
376 | if (isset($_POST['newname']) && '' == $_POST['newname']) { |
||
377 | $this->doEdit($lang['struserneedsname']); |
||
378 | } elseif ($_POST['formPassword'] != $_POST['formConfirm']) { |
||
379 | $this->doEdit($lang['strpasswordconfirm']); |
||
380 | } else { |
||
381 | if (isset($_POST['newname'])) { |
||
382 | $status = $data->setRenameUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires'], $_POST['newname']); |
||
383 | } else { |
||
384 | $status = $data->setUser($_POST['username'], $_POST['formPassword'], isset($_POST['formCreateDB']), isset($_POST['formSuper']), $_POST['formExpires']); |
||
385 | } |
||
386 | |||
387 | if (0 == $status) { |
||
388 | $this->doDefault($lang['struserupdated']); |
||
389 | } else { |
||
390 | $this->doEdit($lang['struserupdatedbad']); |
||
391 | } |
||
392 | } |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Show confirmation of drop and perform actual drop |
||
397 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
398 | */ |
||
399 | public function doDrop($confirm) |
||
400 | { |
||
401 | $conf = $this->conf; |
||
402 | |||
403 | $lang = $this->lang; |
||
404 | $data = $this->misc->getDatabaseAccessor(); |
||
405 | |||
406 | if ($confirm) { |
||
407 | $this->printTrail('user'); |
||
408 | $this->printTitle($lang['strdrop'], 'pg.user.drop'); |
||
409 | |||
410 | echo '<p>', sprintf($lang['strconfdropuser'], $this->misc->printVal($_REQUEST['username'])), "</p>\n"; |
||
411 | |||
412 | echo '<form action="' . SUBFOLDER . "/src/views/users.php\" method=\"post\">\n"; |
||
413 | echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
||
414 | echo '<input type="hidden" name="username" value="', htmlspecialchars($_REQUEST['username']), "\" />\n"; |
||
415 | echo $this->misc->form; |
||
416 | echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n"; |
||
417 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
418 | echo "</form>\n"; |
||
419 | } else { |
||
420 | $status = $data->dropUser($_REQUEST['username']); |
||
421 | if (0 == $status) { |
||
422 | $this->doDefault($lang['struserdropped']); |
||
423 | } else { |
||
424 | $this->doDefault($lang['struserdroppedbad']); |
||
425 | } |
||
426 | } |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Displays a screen where they can enter a new user |
||
431 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
432 | */ |
||
433 | public function doCreate($msg = '') |
||
434 | { |
||
435 | $conf = $this->conf; |
||
436 | |||
437 | $lang = $this->lang; |
||
438 | $data = $this->misc->getDatabaseAccessor(); |
||
439 | |||
440 | if (!isset($_POST['formUsername'])) { |
||
441 | $_POST['formUsername'] = ''; |
||
442 | } |
||
443 | |||
444 | if (!isset($_POST['formPassword'])) { |
||
445 | $_POST['formPassword'] = ''; |
||
446 | } |
||
447 | |||
448 | if (!isset($_POST['formConfirm'])) { |
||
449 | $_POST['formConfirm'] = ''; |
||
450 | } |
||
451 | |||
452 | if (!isset($_POST['formExpires'])) { |
||
453 | $_POST['formExpires'] = ''; |
||
454 | } |
||
455 | |||
456 | $this->printTrail('server'); |
||
457 | $this->printTitle($lang['strcreateuser'], 'pg.user.create'); |
||
458 | $this->printMsg($msg); |
||
459 | |||
460 | echo '<form action="' . SUBFOLDER . "/src/views/users.php\" method=\"post\">\n"; |
||
461 | echo "<table>\n"; |
||
462 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strusername']}</th>\n"; |
||
463 | echo "\t\t<td class=\"data1\"><input size=\"15\" maxlength=\"{$data->_maxNameLen}\" name=\"formUsername\" value=\"", htmlspecialchars($_POST['formUsername']), "\" /></td>\n\t</tr>\n"; |
||
464 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strpassword']}</th>\n"; |
||
465 | echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n"; |
||
466 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strconfirm']}</th>\n"; |
||
467 | echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formConfirm\" value=\"", htmlspecialchars($_POST['formConfirm']), "\" /></td>\n\t</tr>\n"; |
||
468 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$lang['strsuper']}</label></th>\n"; |
||
469 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"", |
||
470 | (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
471 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$lang['strcreatedb']}</label></th>\n"; |
||
472 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"", |
||
473 | (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
474 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strexpires']}</th>\n"; |
||
475 | echo "\t\t<td class=\"data1\"><input size=\"30\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n"; |
||
476 | echo "</table>\n"; |
||
477 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
478 | echo $this->misc->form; |
||
479 | echo "<input type=\"submit\" name=\"create\" value=\"{$lang['strcreate']}\" />\n"; |
||
480 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
481 | echo "</form>\n"; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Actually creates the new user in the database |
||
486 | */ |
||
487 | public function doSaveCreate() |
||
512 | } |
||
513 | } |
||
514 | } |
||
515 | } |
||
516 |