| Total Complexity | 66 |
| Total Lines | 410 |
| 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 | ])) { |
||
| 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 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | // Determine whether object should be ref'd by name or oid. |
||
| 83 | if (isset($_REQUEST[$subject . '_oid'])) { |
||
| 84 | $object = $_REQUEST[$subject . '_oid']; |
||
| 85 | } else { |
||
| 86 | $object = $_REQUEST[$subject]; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Get the privileges on the object, given its type |
||
| 90 | if ('column' == $subject) { |
||
| 91 | $privileges = $data->getPrivileges($object, 'column', $_REQUEST['table']); |
||
| 92 | } else { |
||
| 93 | $privileges = $data->getPrivileges($object, $subject); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (sizeof($privileges) > 0) { |
||
| 97 | echo "<table>\n"; |
||
| 98 | if ($data->hasRoles()) { |
||
| 99 | echo "<tr><th class=\"data\">{$this->lang['strrole']}</th>"; |
||
| 100 | } else { |
||
| 101 | echo "<tr><th class=\"data\">{$this->lang['strtype']}</th><th class=\"data\">{$this->lang['struser']}/{$this->lang['strgroup']}</th>"; |
||
| 102 | } |
||
| 103 | |||
| 104 | foreach ($data->privlist[$subject] as $v2) { |
||
| 105 | // Skip over ALL PRIVILEGES |
||
| 106 | if ('ALL PRIVILEGES' == $v2) { |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | |||
| 110 | echo "<th class=\"data\">{$v2}</th>\n"; |
||
| 111 | } |
||
| 112 | if ($data->hasGrantOption()) { |
||
| 113 | echo "<th class=\"data\">{$this->lang['strgrantor']}</th>"; |
||
| 114 | } |
||
| 115 | echo "</tr>\n"; |
||
| 116 | |||
| 117 | // Loop over privileges, outputting them |
||
| 118 | $i = 0; |
||
| 119 | foreach ($privileges as $v) { |
||
| 120 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
| 121 | echo "<tr class=\"data{$id}\">\n"; |
||
| 122 | if (!$data->hasRoles()) { |
||
| 123 | echo '<td>', $this->misc->printVal($v[0]), "</td>\n"; |
||
| 124 | } |
||
| 125 | |||
| 126 | echo '<td>', $this->misc->printVal($v[1]), "</td>\n"; |
||
| 127 | foreach ($data->privlist[$subject] as $v2) { |
||
| 128 | // Skip over ALL PRIVILEGES |
||
| 129 | if ('ALL PRIVILEGES' == $v2) { |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | |||
| 133 | echo '<td>'; |
||
| 134 | if (in_array($v2, $v[2], true)) { |
||
| 135 | echo $this->lang['stryes']; |
||
| 136 | } else { |
||
| 137 | echo $this->lang['strno']; |
||
| 138 | } |
||
| 139 | |||
| 140 | // If we have grant option for this, end mark |
||
| 141 | if ($data->hasGrantOption() && in_array($v2, $v[4], true)) { |
||
| 142 | echo $this->lang['strasterisk']; |
||
| 143 | } |
||
| 144 | |||
| 145 | echo "</td>\n"; |
||
| 146 | } |
||
| 147 | if ($data->hasGrantOption()) { |
||
| 148 | echo '<td>', $this->misc->printVal($v[3]), "</td>\n"; |
||
| 149 | } |
||
| 150 | echo "</tr>\n"; |
||
| 151 | ++$i; |
||
| 152 | } |
||
| 153 | |||
| 154 | echo '</table>'; |
||
| 155 | } else { |
||
| 156 | echo "<p>{$this->lang['strnoprivileges']}</p>\n"; |
||
| 157 | } |
||
| 158 | $this->printGrantLinks(); |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | public function printGrantLinks() |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Grant permissions on an object to a user. |
||
| 279 | * |
||
| 280 | * @param bool $confirm To show entry screen |
||
| 281 | * @param string $mode 'grant' or 'revoke' |
||
| 282 | * @param string $msg (optional) A message to show |
||
| 283 | */ |
||
| 284 | public function doAlter($confirm, $mode, $msg = '') |
||
| 426 |