| Total Complexity | 40 |
| Total Lines | 263 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PermsHelper 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 PermsHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class PermsHelper extends Helper |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * API methods allowed in current module |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $current = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * API methods allowed in all modules |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $allowed = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Permissions on folders enabled flag |
||
| 40 | * |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $permissionsOnFolders = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritDoc} |
||
| 47 | * |
||
| 48 | * Init API and WebAPP base URL |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public function initialize(array $config): void |
||
| 53 | { |
||
| 54 | $modules = (array)$this->_View->get('modules'); |
||
| 55 | // using foreach instead of Hash::combine |
||
| 56 | // to avoid RuntimeError "Hash::combine() needs an equal number of keys + values" |
||
| 57 | foreach ($modules as $name => $module) { |
||
| 58 | if (Hash::check($module, 'hints.allow')) { |
||
| 59 | $this->allowed[$name] = Hash::get($module, 'hints.allow'); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | $currentModule = (array)$this->_View->get('currentModule'); |
||
| 63 | $this->current = (array)Hash::get($currentModule, 'hints.allow'); |
||
| 64 | $schema = (array)$this->_View->get('foldersSchema'); |
||
| 65 | $this->permissionsOnFolders = in_array('Permissions', (array)Hash::get($schema, 'associations')); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Check lock/unlock permission. |
||
| 70 | * |
||
| 71 | * @return bool |
||
| 72 | */ |
||
| 73 | public function canLock(): bool |
||
| 74 | { |
||
| 75 | return $this->userIsAdmin(); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Check create permission. |
||
| 80 | * |
||
| 81 | * @param string|null $module Module name |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public function canCreate(?string $module = null): bool |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Return modules that can be created by the authenticated user. |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function canCreateModules(): array |
||
| 95 | { |
||
| 96 | $modules = array_keys((array)$this->_View->get('modules')); |
||
| 97 | |||
| 98 | return array_values( |
||
| 99 | array_filter( |
||
| 100 | $modules, |
||
| 101 | function ($module) { |
||
| 102 | return $this->canCreate($module); |
||
| 103 | } |
||
| 104 | ) |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Check delete permission. |
||
| 110 | * |
||
| 111 | * @param array $object The object |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function canDelete(array $object): bool |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Check save permission. |
||
| 127 | * |
||
| 128 | * @param string|null $module Module name |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function canSave(?string $module = null): bool |
||
| 132 | { |
||
| 133 | return $this->userIsAdmin() || ($this->isAllowed('PATCH', $module) && $this->userIsAllowed($module)); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Map of modules and their save permissions for the authenticated user. |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function canSaveMap(): array |
||
| 142 | { |
||
| 143 | $modules = array_keys((array)$this->_View->get('modules')); |
||
| 144 | $map = []; |
||
| 145 | foreach ($modules as $module) { |
||
| 146 | $map[$module] = $this->canSave($module); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $map; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check read permission. |
||
| 154 | * |
||
| 155 | * @param string|null $module Module name |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function canRead(?string $module = null): bool |
||
| 159 | { |
||
| 160 | return $this->isAllowed('GET', $module); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Check if a method is allowed on a module. |
||
| 165 | * |
||
| 166 | * @param string $method Method to check |
||
| 167 | * @param string|null $module Module name, if missing or null current module is used. |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | protected function isAllowed(string $method, ?string $module = null): bool |
||
| 171 | { |
||
| 172 | if (empty($module)) { |
||
| 173 | if (empty($this->current)) { |
||
| 174 | return true; |
||
| 175 | } |
||
| 176 | |||
| 177 | return in_array($method, $this->current); |
||
| 178 | } |
||
| 179 | |||
| 180 | $allowed = (array)Hash::get($this->allowed, $module); |
||
| 181 | |||
| 182 | return in_array($method, $allowed); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Access string (can be 'read', 'write', 'hidden') per role and module. |
||
| 187 | * |
||
| 188 | * @param array $accessControl The access control array |
||
| 189 | * @param string $roleName The role name |
||
| 190 | * @param string $moduleName The module name |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function access(array $accessControl, string $roleName, string $moduleName): string |
||
| 194 | { |
||
| 195 | $roleAccesses = Hash::get($accessControl, $roleName, []); |
||
| 196 | if (empty($roleAccesses)) { |
||
| 197 | return 'write'; |
||
| 198 | } |
||
| 199 | $hiddenModules = Hash::get($roleAccesses, 'hidden', []); |
||
| 200 | if (in_array($moduleName, $hiddenModules)) { |
||
| 201 | return 'hidden'; |
||
| 202 | } |
||
| 203 | $readonlyModules = Hash::get($roleAccesses, 'readonly', []); |
||
| 204 | |||
| 205 | return in_array($moduleName, $readonlyModules) ? 'read' : 'write'; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Return true if authenticated user has role admin |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function userIsAdmin(): bool |
||
| 214 | { |
||
| 215 | return in_array('admin', $this->userRoles()); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Check permissions for user if object is a folder. |
||
| 220 | * |
||
| 221 | * @param string|null $module The module, if passed. |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function userIsAllowed(?string $module): bool |
||
| 225 | { |
||
| 226 | $objectType = !empty($module) ? $module : $this->_View->get('objectType'); |
||
| 227 | if ($this->permissionsOnFolders === false || $objectType !== 'folders' || $this->userIsAdmin()) { |
||
| 228 | return true; |
||
| 229 | } |
||
| 230 | |||
| 231 | $object = $this->_View->get('object'); |
||
| 232 | $permsRoles = (array)Hash::get((array)$object, 'meta.perms.roles'); |
||
| 233 | if (empty($permsRoles)) { |
||
| 234 | return true; |
||
| 235 | } |
||
| 236 | |||
| 237 | return !empty(array_intersect($permsRoles, $this->userRoles())); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return authenticated user roles |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function userRoles(): array |
||
| 246 | { |
||
| 247 | /** @var \Authentication\Identity|null $identity */ |
||
| 248 | $identity = $this->_View->get('user'); |
||
| 249 | |||
| 250 | return empty($identity) ? [] : (array)$identity->get('roles'); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Return true if object is locked by parents. |
||
| 255 | * When user is admin, return false. |
||
| 256 | * When user is not admin, return true if at least one parent is locked for user. |
||
| 257 | * Return false otherwise |
||
| 258 | * |
||
| 259 | * @param string $id The object id |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function isLockedByParents(string $id): bool |
||
| 285 | } |
||
| 286 | } |
||
| 287 |