| Total Complexity | 107 |
| Total Lines | 821 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RolesController 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 RolesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class RolesController extends BaseController |
||
| 17 | { |
||
| 18 | public $controller_title = 'strroles'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default method to render the controller according to the action parameter. |
||
| 22 | */ |
||
| 23 | public function render() |
||
| 24 | { |
||
| 25 | $this->printHeader(); |
||
| 26 | $this->printBody(); |
||
| 27 | |||
| 28 | switch ($this->action) { |
||
| 29 | case 'create': |
||
| 30 | $this->doCreate(); |
||
| 31 | |||
| 32 | break; |
||
| 33 | case 'save_create': |
||
| 34 | if (isset($_POST['create'])) { |
||
| 35 | $this->doSaveCreate(); |
||
| 36 | } else { |
||
| 37 | $this->doDefault(); |
||
| 38 | } |
||
| 39 | |||
| 40 | break; |
||
| 41 | case 'alter': |
||
| 42 | $this->doAlter(); |
||
| 43 | |||
| 44 | break; |
||
| 45 | case 'save_alter': |
||
| 46 | if (isset($_POST['alter'])) { |
||
| 47 | $this->doSaveAlter(); |
||
| 48 | } else { |
||
| 49 | $this->doDefault(); |
||
| 50 | } |
||
| 51 | |||
| 52 | break; |
||
| 53 | case 'confirm_drop': |
||
| 54 | $this->doDrop(true); |
||
| 55 | |||
| 56 | break; |
||
| 57 | case 'drop': |
||
| 58 | if (isset($_POST['drop'])) { |
||
| 59 | $this->doDrop(false); |
||
| 60 | } else { |
||
| 61 | $this->doDefault(); |
||
| 62 | } |
||
| 63 | |||
| 64 | break; |
||
| 65 | case 'properties': |
||
| 66 | $this->doProperties(); |
||
| 67 | |||
| 68 | break; |
||
| 69 | case 'confchangepassword': |
||
| 70 | $this->doChangePassword(true); |
||
| 71 | |||
| 72 | break; |
||
| 73 | case 'changepassword': |
||
| 74 | if (isset($_REQUEST['ok'])) { |
||
| 75 | $this->doChangePassword(false); |
||
| 76 | } else { |
||
| 77 | $this->doAccount(); |
||
| 78 | } |
||
| 79 | |||
| 80 | break; |
||
| 81 | case 'account': |
||
| 82 | $this->doAccount(); |
||
| 83 | |||
| 84 | break; |
||
| 85 | default: |
||
| 86 | $this->doDefault(); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->printFooter(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Show default list of roles in the database. |
||
| 94 | * |
||
| 95 | * @param mixed $msg |
||
| 96 | */ |
||
| 97 | public function doDefault($msg = '') |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Displays a screen for create a new role. |
||
| 213 | * |
||
| 214 | * @param mixed $msg |
||
| 215 | */ |
||
| 216 | public function doCreate($msg = '') |
||
| 217 | { |
||
| 218 | $data = $this->misc->getDatabaseAccessor(); |
||
| 219 | |||
| 220 | $this->coalesceArr($_POST, 'formRolename', ''); |
||
| 221 | |||
| 222 | $this->coalesceArr($_POST, 'formPassword', ''); |
||
| 223 | |||
| 224 | $this->coalesceArr($_POST, 'formConfirm', ''); |
||
| 225 | |||
| 226 | $this->coalesceArr($_POST, 'formConnLimit', ''); |
||
| 227 | |||
| 228 | $this->coalesceArr($_POST, 'formExpires', ''); |
||
| 229 | |||
| 230 | $this->coalesceArr($_POST, 'memberof', []); |
||
| 231 | |||
| 232 | $this->coalesceArr($_POST, 'members', []); |
||
| 233 | |||
| 234 | $this->coalesceArr($_POST, 'adminmembers', []); |
||
| 235 | |||
| 236 | $this->printTrail('role'); |
||
| 237 | $this->printTitle($this->lang['strcreaterole'], 'pg.role.create'); |
||
| 238 | $this->printMsg($msg); |
||
| 239 | |||
| 240 | echo '<form action="' . \SUBFOLDER . "/src/views/roles\" method=\"post\">\n"; |
||
| 241 | echo "<table>\n"; |
||
| 242 | echo "\t<tr>\n\t\t<th class=\"data left required\" style=\"width: 130px\">{$this->lang['strname']}</th>\n"; |
||
| 243 | echo "\t\t<td class=\"data1\"><input size=\"15\" maxlength=\"{$data->_maxNameLen}\" name=\"formRolename\" value=\"", htmlspecialchars($_POST['formRolename']), "\" /></td>\n\t</tr>\n"; |
||
| 244 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strpassword']}</th>\n"; |
||
| 245 | echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n"; |
||
| 246 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconfirm']}</th>\n"; |
||
| 247 | echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formConfirm\" value=\"", htmlspecialchars($_POST['formConfirm']), "\" /></td>\n\t</tr>\n"; |
||
| 248 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$this->lang['strsuper']}</label></th>\n"; |
||
| 249 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"", |
||
| 250 | (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 251 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$this->lang['strcreatedb']}</label></th>\n"; |
||
| 252 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"", |
||
| 253 | (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 254 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateRole\">{$this->lang['strcancreaterole']}</label></th>\n"; |
||
| 255 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateRole\" name=\"formCreateRole\"", |
||
| 256 | (isset($_POST['formCreateRole'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 257 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formInherits\">{$this->lang['strinheritsprivs']}</label></th>\n"; |
||
| 258 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formInherits\" name=\"formInherits\"", |
||
| 259 | (isset($_POST['formInherits'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 260 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCanLogin\">{$this->lang['strcanlogin']}</label></th>\n"; |
||
| 261 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCanLogin\" name=\"formCanLogin\"", |
||
| 262 | (isset($_POST['formCanLogin'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 263 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconnlimit']}</th>\n"; |
||
| 264 | echo "\t\t<td class=\"data1\"><input size=\"4\" name=\"formConnLimit\" value=\"", htmlspecialchars($_POST['formConnLimit']), "\" /></td>\n\t</tr>\n"; |
||
| 265 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strexpires']}</th>\n"; |
||
| 266 | echo "\t\t<td class=\"data1\"><input size=\"23\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n"; |
||
| 267 | |||
| 268 | $roles = $data->getRoles(); |
||
| 269 | if ($roles->recordCount() > 0) { |
||
| 270 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmemberof']}</th>\n"; |
||
| 271 | echo "\t\t<td class=\"data\">\n"; |
||
| 272 | echo "\t\t\t<select name=\"memberof[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n"; |
||
| 273 | while (!$roles->EOF) { |
||
| 274 | $rolename = $roles->fields['rolname']; |
||
| 275 | echo "\t\t\t\t<option value=\"{$rolename}\"", |
||
| 276 | (in_array($rolename, $_POST['memberof'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n"; |
||
| 277 | $roles->moveNext(); |
||
| 278 | } |
||
| 279 | echo "\t\t\t</select>\n"; |
||
| 280 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 281 | |||
| 282 | $roles->moveFirst(); |
||
| 283 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmembers']}</th>\n"; |
||
| 284 | echo "\t\t<td class=\"data\">\n"; |
||
| 285 | echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n"; |
||
| 286 | while (!$roles->EOF) { |
||
| 287 | $rolename = $roles->fields['rolname']; |
||
| 288 | echo "\t\t\t\t<option value=\"{$rolename}\"", |
||
| 289 | (in_array($rolename, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n"; |
||
| 290 | $roles->moveNext(); |
||
| 291 | } |
||
| 292 | echo "\t\t\t</select>\n"; |
||
| 293 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 294 | |||
| 295 | $roles->moveFirst(); |
||
| 296 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stradminmembers']}</th>\n"; |
||
| 297 | echo "\t\t<td class=\"data\">\n"; |
||
| 298 | echo "\t\t\t<select name=\"adminmembers[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n"; |
||
| 299 | while (!$roles->EOF) { |
||
| 300 | $rolename = $roles->fields['rolname']; |
||
| 301 | echo "\t\t\t\t<option value=\"{$rolename}\"", |
||
| 302 | (in_array($rolename, $_POST['adminmembers'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n"; |
||
| 303 | $roles->moveNext(); |
||
| 304 | } |
||
| 305 | echo "\t\t\t</select>\n"; |
||
| 306 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 307 | } |
||
| 308 | |||
| 309 | echo "</table>\n"; |
||
| 310 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
| 311 | echo $this->misc->form; |
||
| 312 | echo "<input type=\"submit\" name=\"create\" value=\"{$this->lang['strcreate']}\" />\n"; |
||
| 313 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 314 | echo "</form>\n"; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Actually creates the new role in the database. |
||
| 319 | */ |
||
| 320 | public function doSaveCreate() |
||
| 321 | { |
||
| 322 | $data = $this->misc->getDatabaseAccessor(); |
||
| 323 | |||
| 324 | $this->coalesceArr($_POST, 'memberof', []); |
||
| 325 | |||
| 326 | $this->coalesceArr($_POST, 'members', []); |
||
| 327 | |||
| 328 | $this->coalesceArr($_POST, 'adminmembers', []); |
||
| 329 | |||
| 330 | // Check data |
||
| 331 | if ('' == $_POST['formRolename']) { |
||
| 332 | $this->doCreate($this->lang['strroleneedsname']); |
||
| 333 | } elseif ($_POST['formPassword'] != $_POST['formConfirm']) { |
||
| 334 | $this->doCreate($this->lang['strpasswordconfirm']); |
||
| 335 | } else { |
||
| 336 | $status = $data->createRole( |
||
| 337 | $_POST['formRolename'], |
||
| 338 | $_POST['formPassword'], |
||
| 339 | isset($_POST['formSuper']), |
||
| 340 | isset($_POST['formCreateDB']), |
||
| 341 | isset($_POST['formCreateRole']), |
||
| 342 | isset($_POST['formInherits']), |
||
| 343 | isset($_POST['formCanLogin']), |
||
| 344 | $_POST['formConnLimit'], |
||
| 345 | $_POST['formExpires'], |
||
| 346 | $_POST['memberof'], |
||
| 347 | $_POST['members'], |
||
| 348 | $_POST['adminmembers'] |
||
| 349 | ); |
||
| 350 | if (0 == $status) { |
||
| 351 | $this->doDefault($this->lang['strrolecreated']); |
||
| 352 | } else { |
||
| 353 | $this->doCreate($this->lang['strrolecreatedbad']); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Function to allow alter a role. |
||
| 360 | * |
||
| 361 | * @param mixed $msg |
||
| 362 | */ |
||
| 363 | public function doAlter($msg = '') |
||
| 364 | { |
||
| 365 | $data = $this->misc->getDatabaseAccessor(); |
||
| 366 | |||
| 367 | $this->printTrail('role'); |
||
| 368 | $this->printTitle($this->lang['stralter'], 'pg.role.alter'); |
||
| 369 | $this->printMsg($msg); |
||
| 370 | |||
| 371 | $roledata = $data->getRole($_REQUEST['rolename']); |
||
| 372 | |||
| 373 | if ($roledata->recordCount() > 0) { |
||
| 374 | $server_info = $this->misc->getServerInfo(); |
||
| 375 | $canRename = $data->hasUserRename() && ($_REQUEST['rolename'] != $server_info['username']); |
||
| 376 | $roledata->fields['rolsuper'] = $data->phpBool($roledata->fields['rolsuper']); |
||
| 377 | $roledata->fields['rolcreatedb'] = $data->phpBool($roledata->fields['rolcreatedb']); |
||
| 378 | $roledata->fields['rolcreaterole'] = $data->phpBool($roledata->fields['rolcreaterole']); |
||
| 379 | $roledata->fields['rolinherit'] = $data->phpBool($roledata->fields['rolinherit']); |
||
| 380 | $roledata->fields['rolcanlogin'] = $data->phpBool($roledata->fields['rolcanlogin']); |
||
| 381 | |||
| 382 | if (!isset($_POST['formExpires'])) { |
||
| 383 | if ($canRename) { |
||
| 384 | $_POST['formNewRoleName'] = $roledata->fields['rolname']; |
||
| 385 | } |
||
| 386 | |||
| 387 | if ($roledata->fields['rolsuper']) { |
||
| 388 | $_POST['formSuper'] = ''; |
||
| 389 | } |
||
| 390 | |||
| 391 | if ($roledata->fields['rolcreatedb']) { |
||
| 392 | $_POST['formCreateDB'] = ''; |
||
| 393 | } |
||
| 394 | |||
| 395 | if ($roledata->fields['rolcreaterole']) { |
||
| 396 | $_POST['formCreateRole'] = ''; |
||
| 397 | } |
||
| 398 | |||
| 399 | if ($roledata->fields['rolinherit']) { |
||
| 400 | $_POST['formInherits'] = ''; |
||
| 401 | } |
||
| 402 | |||
| 403 | if ($roledata->fields['rolcanlogin']) { |
||
| 404 | $_POST['formCanLogin'] = ''; |
||
| 405 | } |
||
| 406 | |||
| 407 | $_POST['formConnLimit'] = '-1' == $roledata->fields['rolconnlimit'] ? '' : $roledata->fields['rolconnlimit']; |
||
| 408 | $_POST['formExpires'] = 'infinity' == $roledata->fields['rolvaliduntil'] ? '' : $roledata->fields['rolvaliduntil']; |
||
| 409 | $_POST['formPassword'] = ''; |
||
| 410 | } |
||
| 411 | |||
| 412 | echo '<form action="' . \SUBFOLDER . "/src/views/roles\" method=\"post\">\n"; |
||
| 413 | echo "<table>\n"; |
||
| 414 | echo "\t<tr>\n\t\t<th class=\"data left\" style=\"width: 130px\">{$this->lang['strname']}</th>\n"; |
||
| 415 | echo "\t\t<td class=\"data1\">", ($canRename ? "<input name=\"formNewRoleName\" size=\"15\" maxlength=\"{$data->_maxNameLen}\" value=\"" . htmlspecialchars($_POST['formNewRoleName']) . '" />' : $this->misc->printVal($roledata->fields['rolname'])), "</td>\n\t</tr>\n"; |
||
| 416 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strpassword']}</th>\n"; |
||
| 417 | echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"15\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n"; |
||
| 418 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconfirm']}</th>\n"; |
||
| 419 | echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"15\" name=\"formConfirm\" value=\"\" /></td>\n\t</tr>\n"; |
||
| 420 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$this->lang['strsuper']}</label></th>\n"; |
||
| 421 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"", |
||
| 422 | (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 423 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$this->lang['strcreatedb']}</label></th>\n"; |
||
| 424 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"", |
||
| 425 | (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 426 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateRole\">{$this->lang['strcancreaterole']}</label></th>\n"; |
||
| 427 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateRole\" name=\"formCreateRole\"", |
||
| 428 | (isset($_POST['formCreateRole'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 429 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formInherits\">{$this->lang['strinheritsprivs']}</label></th>\n"; |
||
| 430 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formInherits\" name=\"formInherits\"", |
||
| 431 | (isset($_POST['formInherits'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 432 | echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCanLogin\">{$this->lang['strcanlogin']}</label></th>\n"; |
||
| 433 | echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCanLogin\" name=\"formCanLogin\"", |
||
| 434 | (isset($_POST['formCanLogin'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n"; |
||
| 435 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconnlimit']}</th>\n"; |
||
| 436 | echo "\t\t<td class=\"data1\"><input size=\"4\" name=\"formConnLimit\" value=\"", htmlspecialchars($_POST['formConnLimit']), "\" /></td>\n\t</tr>\n"; |
||
| 437 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strexpires']}</th>\n"; |
||
| 438 | echo "\t\t<td class=\"data1\"><input size=\"23\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n"; |
||
| 439 | |||
| 440 | if (!isset($_POST['memberof'])) { |
||
| 441 | $memberof = $data->getMemberOf($_REQUEST['rolename']); |
||
| 442 | if ($memberof->recordCount() > 0) { |
||
| 443 | $i = 0; |
||
| 444 | while (!$memberof->EOF) { |
||
| 445 | $_POST['memberof'][$i++] = $memberof->fields['rolname']; |
||
| 446 | $memberof->moveNext(); |
||
| 447 | } |
||
| 448 | } else { |
||
| 449 | $_POST['memberof'] = []; |
||
| 450 | } |
||
| 451 | |||
| 452 | $memberofold = implode(',', $_POST['memberof']); |
||
| 453 | } |
||
| 454 | if (!isset($_POST['members'])) { |
||
| 455 | $members = $data->getMembers($_REQUEST['rolename']); |
||
| 456 | if ($members->recordCount() > 0) { |
||
| 457 | $i = 0; |
||
| 458 | while (!$members->EOF) { |
||
| 459 | $_POST['members'][$i++] = $members->fields['rolname']; |
||
| 460 | $members->moveNext(); |
||
| 461 | } |
||
| 462 | } else { |
||
| 463 | $_POST['members'] = []; |
||
| 464 | } |
||
| 465 | |||
| 466 | $membersold = implode(',', $_POST['members']); |
||
| 467 | } |
||
| 468 | if (!isset($_POST['adminmembers'])) { |
||
| 469 | $adminmembers = $data->getMembers($_REQUEST['rolename'], 't'); |
||
| 470 | if ($adminmembers->recordCount() > 0) { |
||
| 471 | $i = 0; |
||
| 472 | while (!$adminmembers->EOF) { |
||
| 473 | $_POST['adminmembers'][$i++] = $adminmembers->fields['rolname']; |
||
| 474 | $adminmembers->moveNext(); |
||
| 475 | } |
||
| 476 | } else { |
||
| 477 | $_POST['adminmembers'] = []; |
||
| 478 | } |
||
| 479 | |||
| 480 | $adminmembersold = implode(',', $_POST['adminmembers']); |
||
| 481 | } |
||
| 482 | |||
| 483 | $roles = $data->getRoles($_REQUEST['rolename']); |
||
| 484 | if ($roles->recordCount() > 0) { |
||
| 485 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmemberof']}</th>\n"; |
||
| 486 | echo "\t\t<td class=\"data\">\n"; |
||
| 487 | echo "\t\t\t<select name=\"memberof[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n"; |
||
| 488 | while (!$roles->EOF) { |
||
| 489 | $rolename = $roles->fields['rolname']; |
||
| 490 | echo "\t\t\t\t<option value=\"{$rolename}\"", |
||
| 491 | (in_array($rolename, $_POST['memberof'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n"; |
||
| 492 | $roles->moveNext(); |
||
| 493 | } |
||
| 494 | echo "\t\t\t</select>\n"; |
||
| 495 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 496 | |||
| 497 | $roles->moveFirst(); |
||
| 498 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmembers']}</th>\n"; |
||
| 499 | echo "\t\t<td class=\"data\">\n"; |
||
| 500 | echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n"; |
||
| 501 | while (!$roles->EOF) { |
||
| 502 | $rolename = $roles->fields['rolname']; |
||
| 503 | echo "\t\t\t\t<option value=\"{$rolename}\"", |
||
| 504 | (in_array($rolename, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n"; |
||
| 505 | $roles->moveNext(); |
||
| 506 | } |
||
| 507 | echo "\t\t\t</select>\n"; |
||
| 508 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 509 | |||
| 510 | $roles->moveFirst(); |
||
| 511 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stradminmembers']}</th>\n"; |
||
| 512 | echo "\t\t<td class=\"data\">\n"; |
||
| 513 | echo "\t\t\t<select name=\"adminmembers[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n"; |
||
| 514 | while (!$roles->EOF) { |
||
| 515 | $rolename = $roles->fields['rolname']; |
||
| 516 | echo "\t\t\t\t<option value=\"{$rolename}\"", |
||
| 517 | (in_array($rolename, $_POST['adminmembers'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n"; |
||
| 518 | $roles->moveNext(); |
||
| 519 | } |
||
| 520 | echo "\t\t\t</select>\n"; |
||
| 521 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 522 | } |
||
| 523 | echo "</table>\n"; |
||
| 524 | |||
| 525 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_alter\" />\n"; |
||
| 526 | echo '<input type="hidden" name="rolename" value="', htmlspecialchars($_REQUEST['rolename']), "\" />\n"; |
||
| 527 | echo '<input type="hidden" name="memberofold" value="', isset($_POST['memberofold']) ? $_POST['memberofold'] : htmlspecialchars($memberofold), "\" />\n"; |
||
|
|
|||
| 528 | echo '<input type="hidden" name="membersold" value="', isset($_POST['membersold']) ? $_POST['membersold'] : htmlspecialchars($membersold), "\" />\n"; |
||
| 529 | echo '<input type="hidden" name="adminmembersold" value="', isset($_POST['adminmembersold']) ? $_POST['adminmembersold'] : htmlspecialchars($adminmembersold), "\" />\n"; |
||
| 530 | echo $this->misc->form; |
||
| 531 | echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n"; |
||
| 532 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 533 | echo "</form>\n"; |
||
| 534 | } else { |
||
| 535 | echo "<p>{$this->lang['strnodata']}</p>\n"; |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Function to save after editing a role. |
||
| 541 | */ |
||
| 542 | public function doSaveAlter() |
||
| 543 | { |
||
| 544 | $data = $this->misc->getDatabaseAccessor(); |
||
| 545 | |||
| 546 | $this->coalesceArr($_POST, 'memberof', []); |
||
| 547 | |||
| 548 | $this->coalesceArr($_POST, 'members', []); |
||
| 549 | |||
| 550 | $this->coalesceArr($_POST, 'adminmembers', []); |
||
| 551 | |||
| 552 | // Check name and password |
||
| 553 | if (isset($_POST['formNewRoleName']) && '' == $_POST['formNewRoleName']) { |
||
| 554 | $this->doAlter($this->lang['strroleneedsname']); |
||
| 555 | } elseif ($_POST['formPassword'] != $_POST['formConfirm']) { |
||
| 556 | $this->doAlter($this->lang['strpasswordconfirm']); |
||
| 557 | } else { |
||
| 558 | if (isset($_POST['formNewRoleName'])) { |
||
| 559 | $status = $data->setRenameRole($_POST['rolename'], $_POST['formPassword'], isset($_POST['formSuper']), isset($_POST['formCreateDB']), isset($_POST['formCreateRole']), isset($_POST['formInherits']), isset($_POST['formCanLogin']), $_POST['formConnLimit'], $_POST['formExpires'], $_POST['memberof'], $_POST['members'], $_POST['adminmembers'], $_POST['memberofold'], $_POST['membersold'], $_POST['adminmembersold'], $_POST['formNewRoleName']); |
||
| 560 | } else { |
||
| 561 | $status = $data->setRole($_POST['rolename'], $_POST['formPassword'], isset($_POST['formSuper']), isset($_POST['formCreateDB']), isset($_POST['formCreateRole']), isset($_POST['formInherits']), isset($_POST['formCanLogin']), $_POST['formConnLimit'], $_POST['formExpires'], $_POST['memberof'], $_POST['members'], $_POST['adminmembers'], $_POST['memberofold'], $_POST['membersold'], $_POST['adminmembersold']); |
||
| 562 | } |
||
| 563 | |||
| 564 | if (0 == $status) { |
||
| 565 | $this->doDefault($this->lang['strrolealtered']); |
||
| 566 | } else { |
||
| 567 | $this->doAlter($this->lang['strrolealteredbad']); |
||
| 568 | } |
||
| 569 | } |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Show confirmation of drop a role and perform actual drop. |
||
| 574 | * |
||
| 575 | * @param mixed $confirm |
||
| 576 | */ |
||
| 577 | public function doDrop($confirm) |
||
| 578 | { |
||
| 579 | $data = $this->misc->getDatabaseAccessor(); |
||
| 580 | |||
| 581 | if ($confirm) { |
||
| 582 | $this->printTrail('role'); |
||
| 583 | $this->printTitle($this->lang['strdroprole'], 'pg.role.drop'); |
||
| 584 | |||
| 585 | echo '<p>', sprintf($this->lang['strconfdroprole'], $this->misc->printVal($_REQUEST['rolename'])), "</p>\n"; |
||
| 586 | |||
| 587 | echo '<form action="' . \SUBFOLDER . "/src/views/roles\" method=\"post\">\n"; |
||
| 588 | echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
||
| 589 | echo '<input type="hidden" name="rolename" value="', htmlspecialchars($_REQUEST['rolename']), "\" />\n"; |
||
| 590 | echo $this->misc->form; |
||
| 591 | echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n"; |
||
| 592 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 593 | echo "</form>\n"; |
||
| 594 | } else { |
||
| 595 | $status = $data->dropRole($_REQUEST['rolename']); |
||
| 596 | if (0 == $status) { |
||
| 597 | $this->doDefault($this->lang['strroledropped']); |
||
| 598 | } else { |
||
| 599 | $this->doDefault($this->lang['strroledroppedbad']); |
||
| 600 | } |
||
| 601 | } |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Show the properties of a role. |
||
| 606 | * |
||
| 607 | * @param mixed $msg |
||
| 608 | */ |
||
| 609 | public function doProperties($msg = '') |
||
| 610 | { |
||
| 611 | $data = $this->misc->getDatabaseAccessor(); |
||
| 612 | |||
| 613 | $this->printTrail('role'); |
||
| 614 | $this->printTitle($this->lang['strproperties'], 'pg.role'); |
||
| 615 | $this->printMsg($msg); |
||
| 616 | |||
| 617 | $roledata = $data->getRole($_REQUEST['rolename']); |
||
| 618 | if ($roledata->recordCount() > 0) { |
||
| 619 | $roledata->fields['rolsuper'] = $data->phpBool($roledata->fields['rolsuper']); |
||
| 620 | $roledata->fields['rolcreatedb'] = $data->phpBool($roledata->fields['rolcreatedb']); |
||
| 621 | $roledata->fields['rolcreaterole'] = $data->phpBool($roledata->fields['rolcreaterole']); |
||
| 622 | $roledata->fields['rolinherit'] = $data->phpBool($roledata->fields['rolinherit']); |
||
| 623 | $roledata->fields['rolcanlogin'] = $data->phpBool($roledata->fields['rolcanlogin']); |
||
| 624 | |||
| 625 | echo "<table>\n"; |
||
| 626 | echo "\t<tr>\n\t\t<th class=\"data\" style=\"width: 130px\">Description</th>\n"; |
||
| 627 | echo "\t\t<th class=\"data\" style=\"width: 120\">Value</th>\n\t</tr>\n"; |
||
| 628 | echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strname']}</td>\n"; |
||
| 629 | echo "\t\t<td class=\"data1\">", htmlspecialchars($_REQUEST['rolename']), "</td>\n\t</tr>\n"; |
||
| 630 | echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strsuper']}</td>\n"; |
||
| 631 | echo "\t\t<td class=\"data2\">", (($roledata->fields['rolsuper']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n\t</tr>\n"; |
||
| 632 | echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strcreatedb']}</td>\n"; |
||
| 633 | echo "\t\t<td class=\"data1\">", (($roledata->fields['rolcreatedb']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n"; |
||
| 634 | echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strcancreaterole']}</td>\n"; |
||
| 635 | echo "\t\t<td class=\"data2\">", (($roledata->fields['rolcreaterole']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n"; |
||
| 636 | echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strinheritsprivs']}</td>\n"; |
||
| 637 | echo "\t\t<td class=\"data1\">", (($roledata->fields['rolinherit']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n"; |
||
| 638 | echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strcanlogin']}</td>\n"; |
||
| 639 | echo "\t\t<td class=\"data2\">", (($roledata->fields['rolcanlogin']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n"; |
||
| 640 | echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strconnlimit']}</td>\n"; |
||
| 641 | echo "\t\t<td class=\"data1\">", ('-1' == $roledata->fields['rolconnlimit'] ? $this->lang['strnolimit'] : $this->misc->printVal($roledata->fields['rolconnlimit'])), "</td>\n"; |
||
| 642 | echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strexpires']}</td>\n"; |
||
| 643 | echo "\t\t<td class=\"data2\">", ('infinity' == $roledata->fields['rolvaliduntil'] || is_null($roledata->fields['rolvaliduntil']) ? $this->lang['strnever'] : $this->misc->printVal($roledata->fields['rolvaliduntil'])), "</td>\n"; |
||
| 644 | echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strsessiondefaults']}</td>\n"; |
||
| 645 | echo "\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolconfig']), "</td>\n"; |
||
| 646 | echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strmemberof']}</td>\n"; |
||
| 647 | echo "\t\t<td class=\"data2\">"; |
||
| 648 | $memberof = $data->getMemberOf($_REQUEST['rolename']); |
||
| 649 | if ($memberof->recordCount() > 0) { |
||
| 650 | while (!$memberof->EOF) { |
||
| 651 | echo $this->misc->printVal($memberof->fields['rolname']), "<br />\n"; |
||
| 652 | $memberof->moveNext(); |
||
| 653 | } |
||
| 654 | } |
||
| 655 | echo "</td>\n\t</tr>\n"; |
||
| 656 | echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strmembers']}</td>\n"; |
||
| 657 | echo "\t\t<td class=\"data1\">"; |
||
| 658 | $members = $data->getMembers($_REQUEST['rolename']); |
||
| 659 | if ($members->recordCount() > 0) { |
||
| 660 | while (!$members->EOF) { |
||
| 661 | echo $this->misc->printVal($members->fields['rolname']), "<br />\n"; |
||
| 662 | $members->moveNext(); |
||
| 663 | } |
||
| 664 | } |
||
| 665 | echo "</td>\n\t</tr>\n"; |
||
| 666 | echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['stradminmembers']}</td>\n"; |
||
| 667 | echo "\t\t<td class=\"data2\">"; |
||
| 668 | $adminmembers = $data->getMembers($_REQUEST['rolename'], 't'); |
||
| 669 | if ($adminmembers->recordCount() > 0) { |
||
| 670 | while (!$adminmembers->EOF) { |
||
| 671 | echo $this->misc->printVal($adminmembers->fields['rolname']), "<br />\n"; |
||
| 672 | $adminmembers->moveNext(); |
||
| 673 | } |
||
| 674 | } |
||
| 675 | echo "</td>\n\t</tr>\n"; |
||
| 676 | echo "</table>\n"; |
||
| 677 | } else { |
||
| 678 | echo "<p>{$this->lang['strnodata']}</p>\n"; |
||
| 679 | } |
||
| 680 | |||
| 681 | $navlinks = [ |
||
| 682 | 'showall' => [ |
||
| 683 | 'attr' => [ |
||
| 684 | 'href' => [ |
||
| 685 | 'url' => 'roles', |
||
| 686 | 'urlvars' => [ |
||
| 687 | 'server' => $_REQUEST['server'], |
||
| 688 | ], |
||
| 689 | ], |
||
| 690 | ], |
||
| 691 | 'content' => $this->lang['strshowallroles'], |
||
| 692 | ], |
||
| 693 | 'alter' => [ |
||
| 694 | 'attr' => [ |
||
| 695 | 'href' => [ |
||
| 696 | 'url' => 'roles', |
||
| 697 | 'urlvars' => [ |
||
| 698 | 'action' => 'alter', |
||
| 699 | 'server' => $_REQUEST['server'], |
||
| 700 | 'rolename' => $_REQUEST['rolename'], |
||
| 701 | ], |
||
| 702 | ], |
||
| 703 | ], |
||
| 704 | 'content' => $this->lang['stralter'], |
||
| 705 | ], |
||
| 706 | 'drop' => [ |
||
| 707 | 'attr' => [ |
||
| 708 | 'href' => [ |
||
| 709 | 'url' => 'roles', |
||
| 710 | 'urlvars' => [ |
||
| 711 | 'action' => 'confirm_drop', |
||
| 712 | 'server' => $_REQUEST['server'], |
||
| 713 | 'rolename' => $_REQUEST['rolename'], |
||
| 714 | ], |
||
| 715 | ], |
||
| 716 | ], |
||
| 717 | 'content' => $this->lang['strdrop'], |
||
| 718 | ], |
||
| 719 | ]; |
||
| 720 | |||
| 721 | $this->printNavLinks($navlinks, 'roles-properties', get_defined_vars()); |
||
| 722 | } |
||
| 723 | |||
| 724 | /** |
||
| 725 | * If a role is not a superuser role, then we have an 'account management' |
||
| 726 | * page for change his password, etc. We don't prevent them from |
||
| 727 | * messing with the URL to gain access to other role admin stuff, because |
||
| 728 | * the PostgreSQL permissions will prevent them changing anything anyway. |
||
| 729 | * |
||
| 730 | * @param mixed $msg |
||
| 731 | */ |
||
| 732 | public function doAccount($msg = '') |
||
| 785 | } |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Show confirmation of change password and actually change password. |
||
| 789 | * |
||
| 790 | * @param mixed $confirm |
||
| 791 | * @param mixed $msg |
||
| 792 | */ |
||
| 793 | public function doChangePassword($confirm, $msg = '') |
||
| 837 | } |
||
| 838 | } |
||
| 842 |