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