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