| Total Complexity | 62 |
| Total Lines | 414 |
| 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 = '') |
||
| 160 | } |
||
| 161 | |||
| 162 | public function printGrantLinks() |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Prints the form to grants permision on an object to a user. |
||
| 281 | * |
||
| 282 | * @param string $mode either grant or revoke |
||
| 283 | * @param string $msg The message |
||
| 284 | */ |
||
| 285 | public function formAlter($mode, $msg = '') |
||
| 286 | { |
||
| 287 | $data = $this->misc->getDatabaseAccessor(); |
||
| 288 | |||
| 289 | $this->coalesceArr($_REQUEST, 'username', []); |
||
| 290 | |||
| 291 | $this->coalesceArr($_REQUEST, 'groupname', []); |
||
| 292 | |||
| 293 | $this->coalesceArr($_REQUEST, 'privilege', []); |
||
| 294 | |||
| 295 | // Get users from the database |
||
| 296 | $users = $data->getUsers(); |
||
| 297 | // Get groups from the database |
||
| 298 | $groups = $data->getGroups(); |
||
| 299 | |||
| 300 | $this->printTrail($_REQUEST['subject']); |
||
| 301 | |||
| 302 | $this->printTitle($this->lang['str'.$mode], 'pg.privilege.'.$mode); |
||
| 303 | |||
| 304 | $this->printMsg($msg); |
||
| 305 | |||
| 306 | echo '<form action="'.\SUBFOLDER.'/src/views/privileges" method="post">'.PHP_EOL; |
||
| 307 | echo '<table>'.PHP_EOL; |
||
| 308 | echo "<tr><th class=\"data left\">{$this->lang['strusers']}</th>".PHP_EOL; |
||
| 309 | echo '<td class="data1"><select name="username[]" multiple="multiple" size="', min(6, $users->recordCount()), '">'.PHP_EOL; |
||
| 310 | while (!$users->EOF) { |
||
| 311 | $uname = htmlspecialchars($users->fields['usename']); |
||
| 312 | echo "<option value=\"{$uname}\"", |
||
| 313 | in_array($users->fields['usename'], $_REQUEST['username'], true) ? ' selected="selected"' : '', ">{$uname}</option>".PHP_EOL; |
||
| 314 | $users->moveNext(); |
||
| 315 | } |
||
| 316 | echo '</select></td></tr>'.PHP_EOL; |
||
| 317 | echo "<tr><th class=\"data left\">{$this->lang['strgroups']}</th>".PHP_EOL; |
||
| 318 | echo '<td class="data1">'.PHP_EOL; |
||
| 319 | echo '<input type="checkbox" id="public" name="public"', (isset($_REQUEST['public']) ? ' checked="checked"' : ''), ' /><label for="public">PUBLIC</label>'.PHP_EOL; |
||
| 320 | // Only show groups if there are groups! |
||
| 321 | if ($groups->recordCount() > 0) { |
||
| 322 | echo '<br /><select name="groupname[]" multiple="multiple" size="', min(6, $groups->recordCount()), '">'.PHP_EOL; |
||
| 323 | while (!$groups->EOF) { |
||
| 324 | $gname = htmlspecialchars($groups->fields['groname']); |
||
| 325 | echo "<option value=\"{$gname}\"", |
||
| 326 | in_array($groups->fields['groname'], $_REQUEST['groupname'], true) ? ' selected="selected"' : '', ">{$gname}</option>".PHP_EOL; |
||
| 327 | $groups->moveNext(); |
||
| 328 | } |
||
| 329 | echo '</select>'.PHP_EOL; |
||
| 330 | } |
||
| 331 | echo '</td></tr>'.PHP_EOL; |
||
| 332 | echo "<tr><th class=\"data left required\">{$this->lang['strprivileges']}</th>".PHP_EOL; |
||
| 333 | echo '<td class="data1">'.PHP_EOL; |
||
| 334 | foreach ($data->privlist[$_REQUEST['subject']] as $v) { |
||
| 335 | $v = htmlspecialchars($v); |
||
| 336 | echo "<input type=\"checkbox\" id=\"privilege[${v}]\" name=\"privilege[${v}]\"", |
||
| 337 | isset($_REQUEST['privilege'][$v]) ? ' checked="checked"' : '', " /><label for=\"privilege[${v}]\">{$v}</label><br />".PHP_EOL; |
||
| 338 | } |
||
| 339 | echo '</td></tr>'.PHP_EOL; |
||
| 340 | // Grant option |
||
| 341 | if ($data->hasGrantOption()) { |
||
| 342 | echo "<tr><th class=\"data left\">{$this->lang['stroptions']}</th>".PHP_EOL; |
||
| 343 | echo '<td class="data1">'.PHP_EOL; |
||
| 344 | if ('grant' == $mode) { |
||
| 345 | echo '<input type="checkbox" id="grantoption" name="grantoption"', |
||
| 346 | isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', ' /><label for="grantoption">GRANT OPTION</label>'.PHP_EOL; |
||
| 347 | } elseif ('revoke' == $mode) { |
||
| 348 | echo '<input type="checkbox" id="grantoption" name="grantoption"', |
||
| 349 | isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', ' /><label for="grantoption">GRANT OPTION FOR</label><br />'.PHP_EOL; |
||
| 350 | echo '<input type="checkbox" id="cascade" name="cascade"', |
||
| 351 | isset($_REQUEST['cascade']) ? ' checked="checked"' : '', ' /><label for="cascade">CASCADE</label><br />'.PHP_EOL; |
||
| 352 | } |
||
| 353 | echo '</td></tr>'.PHP_EOL; |
||
| 354 | } |
||
| 355 | echo '</table>'.PHP_EOL; |
||
| 356 | |||
| 357 | echo '<p><input type="hidden" name="action" value="save" />'.PHP_EOL; |
||
| 358 | echo '<input type="hidden" name="mode" value="', htmlspecialchars($mode), '" />'.PHP_EOL; |
||
| 359 | echo '<input type="hidden" name="subject" value="', htmlspecialchars($_REQUEST['subject']), '" />'.PHP_EOL; |
||
| 360 | if (isset($_REQUEST[$_REQUEST['subject'].'_oid'])) { |
||
| 361 | echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject'].'_oid'), |
||
| 362 | '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject'].'_oid']), '" />'.PHP_EOL; |
||
| 363 | } |
||
| 364 | |||
| 365 | echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject']), |
||
| 366 | '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject']]), '" />'.PHP_EOL; |
||
| 367 | if ('column' == $_REQUEST['subject']) { |
||
| 368 | echo '<input type="hidden" name="table" value="', |
||
| 369 | htmlspecialchars($_REQUEST['table']), '" />'.PHP_EOL; |
||
| 370 | } |
||
| 371 | |||
| 372 | echo $this->misc->form; |
||
| 373 | echo sprintf('<input type="submit" name="%s" value="%s" />%s', $mode, $this->lang['str'.$mode], PHP_EOL); |
||
| 374 | |||
| 375 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>"; |
||
| 376 | echo '</form>'.PHP_EOL; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Grant permissions on an object to a user. |
||
| 381 | * |
||
| 382 | * @param string $mode 'grant' or 'revoke' |
||
| 383 | */ |
||
| 384 | public function doAlter($mode) |
||
| 426 | } |
||
| 427 | } |
||
| 429 |