| Total Complexity | 62 |
| Total Lines | 433 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PrivilegesController 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 PrivilegesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class PrivilegesController extends BaseController |
||
| 13 | { |
||
| 14 | public $table_place = 'privileges-privileges'; |
||
| 15 | |||
| 16 | public $controller_title = 'strprivileges'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Default method to render the controller according to the action parameter. |
||
| 20 | */ |
||
| 21 | public function render(): void |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Show permissions on a database, namespace, relation, language or function. |
||
| 51 | * |
||
| 52 | * @param mixed $msg |
||
| 53 | */ |
||
| 54 | public function doDefault($msg = ''): void |
||
| 55 | { |
||
| 56 | $data = $this->misc->getDatabaseAccessor(); |
||
| 57 | $subject = $_REQUEST['subject']; |
||
| 58 | |||
| 59 | $this->printTrail($subject); |
||
| 60 | |||
| 61 | // @@@FIXME: This switch is just a temporary solution, |
||
| 62 | // need a better way, maybe every type of object should |
||
| 63 | // have a tab bar??? |
||
| 64 | |||
| 65 | if (\in_array($subject, [ |
||
| 66 | 'server', |
||
| 67 | 'database', |
||
| 68 | 'schema', |
||
| 69 | 'table', |
||
| 70 | 'column', |
||
| 71 | 'view', |
||
| 72 | 'function', |
||
| 73 | ], true)) { |
||
| 74 | $this->printTabs($subject, 'privileges'); |
||
| 75 | } else { |
||
| 76 | $this->printTitle($this->lang['strprivileges'], 'pg.privilege'); |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->printMsg($msg); |
||
| 80 | |||
| 81 | if (!isset($data->privlist[$subject])) { |
||
| 82 | $this->container->halt('No privileges defined for subject ' . $subject); |
||
| 83 | |||
| 84 | return; |
||
| 85 | } |
||
| 86 | |||
| 87 | // Determine whether object should be ref'd by name or oid. |
||
| 88 | if (isset($_REQUEST[$subject . '_oid'])) { |
||
| 89 | $object = $_REQUEST[$subject . '_oid']; |
||
| 90 | } else { |
||
| 91 | $object = $_REQUEST[$subject]; |
||
| 92 | } |
||
| 93 | |||
| 94 | // Get the privileges on the object, given its type |
||
| 95 | if ('column' === $subject) { |
||
| 96 | $privileges = $data->getPrivileges($object, 'column', $_REQUEST['table']); |
||
| 97 | } else { |
||
| 98 | $privileges = $data->getPrivileges($object, $subject); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (0 < \count($privileges)) { |
||
|
|
|||
| 102 | echo '<table>' . \PHP_EOL; |
||
| 103 | |||
| 104 | if ($data->hasRoles()) { |
||
| 105 | echo "<tr><th class=\"data\">{$this->lang['strrole']}</th>"; |
||
| 106 | } else { |
||
| 107 | echo "<tr><th class=\"data\">{$this->lang['strtype']}</th><th class=\"data\">{$this->lang['struser']}/{$this->lang['strgroup']}</th>"; |
||
| 108 | } |
||
| 109 | |||
| 110 | foreach ($data->privlist[$subject] as $v2) { |
||
| 111 | // Skip over ALL PRIVILEGES |
||
| 112 | if ('ALL PRIVILEGES' === $v2) { |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | echo "<th class=\"data\">{$v2}</th>" . \PHP_EOL; |
||
| 117 | } |
||
| 118 | |||
| 119 | if ($data->hasGrantOption()) { |
||
| 120 | echo "<th class=\"data\">{$this->lang['strgrantor']}</th>"; |
||
| 121 | } |
||
| 122 | echo '</tr>' . \PHP_EOL; |
||
| 123 | |||
| 124 | // Loop over privileges, outputting them |
||
| 125 | $i = 0; |
||
| 126 | |||
| 127 | foreach ($privileges as $v) { |
||
| 128 | $id = (0 === ($i % 2) ? '1' : '2'); |
||
| 129 | echo "<tr class=\"data{$id}\">" . \PHP_EOL; |
||
| 130 | |||
| 131 | if (!$data->hasRoles()) { |
||
| 132 | echo '<td>', $this->misc->printVal($v[0]), '</td>' . \PHP_EOL; |
||
| 133 | } |
||
| 134 | |||
| 135 | echo '<td>', $this->misc->printVal($v[1]), '</td>' . \PHP_EOL; |
||
| 136 | |||
| 137 | foreach ($data->privlist[$subject] as $v2) { |
||
| 138 | // Skip over ALL PRIVILEGES |
||
| 139 | if ('ALL PRIVILEGES' === $v2) { |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | |||
| 143 | echo '<td>'; |
||
| 144 | |||
| 145 | if (\in_array($v2, $v[2], true)) { |
||
| 146 | echo $this->lang['stryes']; |
||
| 147 | } else { |
||
| 148 | echo $this->lang['strno']; |
||
| 149 | } |
||
| 150 | |||
| 151 | // If we have grant option for this, end mark |
||
| 152 | if ($data->hasGrantOption() && \in_array($v2, $v[4], true)) { |
||
| 153 | echo $this->lang['strasterisk']; |
||
| 154 | } |
||
| 155 | |||
| 156 | echo '</td>' . \PHP_EOL; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($data->hasGrantOption()) { |
||
| 160 | echo '<td>', $this->misc->printVal($v[3]), '</td>' . \PHP_EOL; |
||
| 161 | } |
||
| 162 | echo '</tr>' . \PHP_EOL; |
||
| 163 | ++$i; |
||
| 164 | } |
||
| 165 | |||
| 166 | echo '</table>'; |
||
| 167 | } else { |
||
| 168 | echo "<p>{$this->lang['strnoprivileges']}</p>" . \PHP_EOL; |
||
| 169 | } |
||
| 170 | $this->printGrantLinks(); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function printGrantLinks(): void |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Prints the form to grants permision on an object to a user. |
||
| 294 | * |
||
| 295 | * @param string $mode either grant or revoke |
||
| 296 | * @param string $msg The message |
||
| 297 | */ |
||
| 298 | public function formAlter($mode, $msg = ''): void |
||
| 299 | { |
||
| 300 | $data = $this->misc->getDatabaseAccessor(); |
||
| 301 | |||
| 302 | $this->coalesceArr($_REQUEST, 'username', []); |
||
| 303 | |||
| 304 | $this->coalesceArr($_REQUEST, 'groupname', []); |
||
| 305 | |||
| 306 | $this->coalesceArr($_REQUEST, 'privilege', []); |
||
| 307 | |||
| 308 | // Get users from the database |
||
| 309 | $users = $data->getUsers(); |
||
| 310 | // Get groups from the database |
||
| 311 | $groups = $data->getGroups(); |
||
| 312 | |||
| 313 | $this->printTrail($_REQUEST['subject']); |
||
| 314 | |||
| 315 | $this->printTitle($this->lang['str' . $mode], 'pg.privilege.' . $mode); |
||
| 316 | |||
| 317 | $this->printMsg($msg); |
||
| 318 | |||
| 319 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/privileges" method="post">' . \PHP_EOL; |
||
| 320 | echo '<table>' . \PHP_EOL; |
||
| 321 | echo "<tr><th class=\"data left\">{$this->lang['strusers']}</th>" . \PHP_EOL; |
||
| 322 | echo '<td class="data1"><select name="username[]" multiple="multiple" size="', \min(6, $users->recordCount()), '">' . \PHP_EOL; |
||
| 323 | |||
| 324 | while (!$users->EOF) { |
||
| 325 | $uname = \htmlspecialchars($users->fields['usename']); |
||
| 326 | echo "<option value=\"{$uname}\"", |
||
| 327 | \in_array($users->fields['usename'], $_REQUEST['username'], true) ? ' selected="selected"' : '', ">{$uname}</option>" . \PHP_EOL; |
||
| 328 | $users->moveNext(); |
||
| 329 | } |
||
| 330 | echo '</select></td></tr>' . \PHP_EOL; |
||
| 331 | echo "<tr><th class=\"data left\">{$this->lang['strgroups']}</th>" . \PHP_EOL; |
||
| 332 | echo '<td class="data1">' . \PHP_EOL; |
||
| 333 | echo '<input type="checkbox" id="public" name="public"', (isset($_REQUEST['public']) ? ' checked="checked"' : ''), ' /><label for="public">PUBLIC</label>' . \PHP_EOL; |
||
| 334 | // Only show groups if there are groups! |
||
| 335 | if (0 < $groups->recordCount()) { |
||
| 336 | echo '<br /><select name="groupname[]" multiple="multiple" size="', \min(6, $groups->recordCount()), '">' . \PHP_EOL; |
||
| 337 | |||
| 338 | while (!$groups->EOF) { |
||
| 339 | $gname = \htmlspecialchars($groups->fields['groname']); |
||
| 340 | echo "<option value=\"{$gname}\"", |
||
| 341 | \in_array($groups->fields['groname'], $_REQUEST['groupname'], true) ? ' selected="selected"' : '', ">{$gname}</option>" . \PHP_EOL; |
||
| 342 | $groups->moveNext(); |
||
| 343 | } |
||
| 344 | echo '</select>' . \PHP_EOL; |
||
| 345 | } |
||
| 346 | echo '</td></tr>' . \PHP_EOL; |
||
| 347 | echo "<tr><th class=\"data left required\">{$this->lang['strprivileges']}</th>" . \PHP_EOL; |
||
| 348 | echo '<td class="data1">' . \PHP_EOL; |
||
| 349 | |||
| 350 | foreach ($data->privlist[$_REQUEST['subject']] as $v) { |
||
| 351 | $v = \htmlspecialchars($v); |
||
| 352 | echo "<input type=\"checkbox\" id=\"privilege[{$v}]\" name=\"privilege[{$v}]\"", |
||
| 353 | isset($_REQUEST['privilege'][$v]) ? ' checked="checked"' : '', " /><label for=\"privilege[{$v}]\">{$v}</label><br />" . \PHP_EOL; |
||
| 354 | } |
||
| 355 | echo '</td></tr>' . \PHP_EOL; |
||
| 356 | // Grant option |
||
| 357 | if ($data->hasGrantOption()) { |
||
| 358 | echo "<tr><th class=\"data left\">{$this->lang['stroptions']}</th>" . \PHP_EOL; |
||
| 359 | echo '<td class="data1">' . \PHP_EOL; |
||
| 360 | |||
| 361 | if ('grant' === $mode) { |
||
| 362 | echo '<input type="checkbox" id="grantoption" name="grantoption"', |
||
| 363 | isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', ' /><label for="grantoption">GRANT OPTION</label>' . \PHP_EOL; |
||
| 364 | } elseif ('revoke' === $mode) { |
||
| 365 | echo '<input type="checkbox" id="grantoption" name="grantoption"', |
||
| 366 | isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', ' /><label for="grantoption">GRANT OPTION FOR</label><br />' . \PHP_EOL; |
||
| 367 | echo '<input type="checkbox" id="cascade" name="cascade"', |
||
| 368 | isset($_REQUEST['cascade']) ? ' checked="checked"' : '', ' /><label for="cascade">CASCADE</label><br />' . \PHP_EOL; |
||
| 369 | } |
||
| 370 | echo '</td></tr>' . \PHP_EOL; |
||
| 371 | } |
||
| 372 | echo '</table>' . \PHP_EOL; |
||
| 373 | |||
| 374 | echo '<p><input type="hidden" name="action" value="save" />' . \PHP_EOL; |
||
| 375 | echo '<input type="hidden" name="mode" value="', \htmlspecialchars($mode), '" />' . \PHP_EOL; |
||
| 376 | echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['subject']), '" />' . \PHP_EOL; |
||
| 377 | |||
| 378 | if (isset($_REQUEST[$_REQUEST['subject'] . '_oid'])) { |
||
| 379 | echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['subject'] . '_oid'), |
||
| 380 | '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['subject'] . '_oid']), '" />' . \PHP_EOL; |
||
| 381 | } |
||
| 382 | |||
| 383 | echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['subject']), |
||
| 384 | '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['subject']]), '" />' . \PHP_EOL; |
||
| 385 | |||
| 386 | if ('column' === $_REQUEST['subject']) { |
||
| 387 | echo '<input type="hidden" name="table" value="', |
||
| 388 | \htmlspecialchars($_REQUEST['table']), '" />' . \PHP_EOL; |
||
| 389 | } |
||
| 390 | |||
| 391 | echo $this->view->form; |
||
| 392 | echo \sprintf('<input type="submit" name="%s" value="%s" />%s', $mode, $this->lang['str' . $mode], \PHP_EOL); |
||
| 393 | |||
| 394 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>"; |
||
| 395 | echo '</form>' . \PHP_EOL; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Grant permissions on an object to a user. |
||
| 400 | * |
||
| 401 | * @param string $mode 'grant' or 'revoke' |
||
| 402 | */ |
||
| 403 | public function doAlter($mode): void |
||
| 445 | } |
||
| 446 | } |
||
| 448 |