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