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