| Total Complexity | 77 |
| Total Lines | 387 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TPermissionsAction 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 TPermissionsAction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class TPermissionsAction extends TShellAction |
||
| 30 | { |
||
| 31 | protected $action = 'perm'; |
||
| 32 | protected $methods = ['index', 'role', 'add-rule', 'remove-rule']; |
||
| 33 | protected $parameters = [null, 'role-name', ['permission-name', 'action'], ['permission-name', 'db-rule-index']]; |
||
| 34 | protected $optional = [null, ['[+-]child', '...'], ['users', 'roles', 'verb', 'ips', 'priority', 'class'], null]; |
||
| 35 | protected $description = [ |
||
| 36 | 'Provides information about Permissions.', |
||
| 37 | 'Displays DB permission information. \'-a\' for all.', |
||
| 38 | 'Add and remove children from roles in the DB.', |
||
| 39 | 'Add a rule to the DB for a specific permission.', |
||
| 40 | 'Remove a rule from the DB for a specific permission.']; |
||
| 41 | |||
| 42 | private $_allPerms = false; |
||
| 43 | |||
| 44 | private $_manager = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * |
||
| 48 | */ |
||
| 49 | public function getAll() |
||
| 50 | { |
||
| 51 | return $this->_allPerms; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param bool $value If this is called, set the property to true |
||
| 56 | */ |
||
| 57 | public function setAll($value) |
||
| 58 | { |
||
| 59 | $this->_allPerms = TPropertyValue::ensureBoolean($value === '' ? true : $value); |
||
|
|
|||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Properties for the action set by parameter |
||
| 64 | * @param string $actionID the action being executed |
||
| 65 | * @return array properties for the $actionID |
||
| 66 | */ |
||
| 67 | public function options($actionID): array |
||
| 68 | { |
||
| 69 | if ($actionID === 'index') { |
||
| 70 | return ['all']; |
||
| 71 | } |
||
| 72 | return []; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Aliases for the properties to be set by parameter |
||
| 77 | * @param string $actionID the action being executed |
||
| 78 | * @return array<alias, property> properties for the $actionID |
||
| 79 | */ |
||
| 80 | public function optionAliases(): array |
||
| 81 | { |
||
| 82 | return ['a' => 'all']; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * |
||
| 87 | * @param mixed $rule |
||
| 88 | * @param null|mixed $writer |
||
| 89 | */ |
||
| 90 | protected function ruleToString($rule, $writer = null) |
||
| 91 | { |
||
| 92 | if (!$writer) { |
||
| 93 | $writer = $this->getWriter(); |
||
| 94 | } |
||
| 95 | $users = $rule->getUsers(); |
||
| 96 | if ($rule->getEveryoneApplied()) { |
||
| 97 | $users[] = '*'; |
||
| 98 | } else { |
||
| 99 | if ($rule->getAuthenticatedApplied()) { |
||
| 100 | $users[] = '@'; |
||
| 101 | } |
||
| 102 | if ($rule->getGuestApplied()) { |
||
| 103 | $users[] = '?'; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return ($writer->format($a = $rule->getAction(), [TShellWriter::BOLD, $a === 'allow' ? TShellWriter::GREEN : TShellWriter::RED]) . ': ') . |
||
| 108 | (get_class($rule) === 'Prado\\Security\\Permissions\\TUserOwnerRule' ? 'User Owner- ' : '') . |
||
| 109 | (($p = $rule->getPriority()) ? '∆' . $p . ' ' : '') . |
||
| 110 | (($users[0] !== '*') ? 'users="' . implode(', ', $users) . '" ' : '') . |
||
| 111 | ((($r = $rule->getRoles()) && (count($r) !== 1 || $r[0] !== '*')) ? 'roles="' . implode(', ', $r) . '" ' : '') . |
||
| 112 | ((($v = $rule->getVerb()) && $v !== '*') ? 'verb="' . $v . '" ' : '') . |
||
| 113 | ((($ip = $rule->getIPRules()) && (count($ip) !== 1 || $ip[0] !== '*')) ? 'ip="' . implode(', ', $ip) . '"' : ''); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * display the database parameter key values. |
||
| 118 | * @param array $args parameters |
||
| 119 | * @return bool is the action handled |
||
| 120 | */ |
||
| 121 | public function actionIndex($args) |
||
| 122 | { |
||
| 123 | $writer = $this->getWriter(); |
||
| 124 | if (!($manager = $this->getPermissionsManager())) { |
||
| 125 | $writer->writeError('No TPermissionsManager found to report'); |
||
| 126 | return true; |
||
| 127 | } |
||
| 128 | |||
| 129 | $writer->writeLine(); |
||
| 130 | $writer->writeLine('Permissions Manager Information', TShellWriter::UNDERLINE); |
||
| 131 | $writer->writeLine(); |
||
| 132 | $writer->writeLine('Super Roles: ' . implode(', ', $manager->getSuperRoles() ?? ['(none)'])); |
||
| 133 | $writer->writeLine('Default Roles: ' . implode(', ', $manager->getDefaultRoles() ?? ['(none)'])); |
||
| 134 | |||
| 135 | $dbConfigRoles = $manager->getDbConfigRoles(); |
||
| 136 | $dbConfigRules = $manager->getDbConfigPermissionRules(); |
||
| 137 | $roles = $this->getAll() ? $manager->getHierarchyRoleChildren(null) : $dbConfigRoles; |
||
| 138 | $rules = $this->getAll() ? $manager->getPermissionRules(null) : $dbConfigRules; |
||
| 139 | $len = 0; |
||
| 140 | foreach ($roles as $role => $children) { |
||
| 141 | if (($l = strlen($role)) > $len) { |
||
| 142 | $len = $l; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $writer->writeLine(); |
||
| 146 | $writer->write(" "); |
||
| 147 | $writer->writeLine('Roles:', TShellWriter::UNDERLINE); |
||
| 148 | foreach ($roles as $role => $children) { |
||
| 149 | $writer->write($writer->pad($role, $len + 1)); |
||
| 150 | $writer->writeLine($writer->wrapText(implode(', ', $children), $len + 1)); |
||
| 151 | } |
||
| 152 | |||
| 153 | $len = 0; |
||
| 154 | foreach ($rules as $permName => $permRules) { |
||
| 155 | if (($l = strlen($permName)) > $len) { |
||
| 156 | $len = $l; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | $writer->writeLine(); |
||
| 160 | $writer->write(" "); |
||
| 161 | $writer->writeLine('Permission Rules:', TShellWriter::UNDERLINE); |
||
| 162 | foreach ($rules as $name => $collection) { |
||
| 163 | $writer->write($writer->pad($name, $len + 1)); |
||
| 164 | $rules[$name] = []; |
||
| 165 | $i = 0; |
||
| 166 | foreach ($collection as $key => $rule) { |
||
| 167 | $rules[$name][] = '#' . ($i++) . ' ' . $this->ruleToString($rule, $writer); |
||
| 168 | } |
||
| 169 | $writer->writeLine($writer->wrapText(implode("\n", $rules[$name]), $len + 1)); |
||
| 170 | $writer->writeLine(); |
||
| 171 | } |
||
| 172 | $writer->writeLine(); |
||
| 173 | return true; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * get children of a role, and adds to and removes children from a db configuration. |
||
| 178 | * @param array $args parameters |
||
| 179 | * @return bool is the action handled |
||
| 180 | */ |
||
| 181 | public function actionRole($args) |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * adds a DB Configuration Permission Rule. Here is the format of the function |
||
| 245 | * arguments. |
||
| 246 | * 'perm/add-rule' permission_name action users roles verb ips priority |
||
| 247 | * and can be use like this: |
||
| 248 | * <code> |
||
| 249 | * prado-cli perm/add-rule '*' deny '*' 'Default' '*' '192.168.*' 1000 |
||
| 250 | * </code> |
||
| 251 | * @param array $args parameters |
||
| 252 | * @return bool is the action handled |
||
| 253 | */ |
||
| 254 | public function actionAddRule($args) |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * removes a DB Configuration Permission Rule |
||
| 328 | * @param array $args parameters |
||
| 329 | * @return bool is the action handled |
||
| 330 | */ |
||
| 331 | public function actionRemoveRule($args) |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * get the TPermissionsManager |
||
| 393 | * @return Prado\Security\Permissions\TPermissionsManager |
||
| 394 | */ |
||
| 395 | public function getPermissionsManager() |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * get the TPermissionsManager from the Application |
||
| 411 | * @param Prado\Security\Permissions\TPermissionsManager $manager |
||
| 412 | */ |
||
| 413 | public function setPermissionsManager($manager) |
||
| 418 |