| Total Complexity | 41 |
| Total Lines | 269 |
| Duplicated Lines | 0 % |
| Coverage | 97.25% |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like AclManager 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 AclManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class AclManager { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * |
||
| 32 | * @var AclList |
||
| 33 | */ |
||
| 34 | protected static $aclList; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * |
||
| 38 | * @var PermissionsMap |
||
| 39 | */ |
||
| 40 | protected static $permissionMap; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Create AclList with default roles and resources. |
||
| 44 | */ |
||
| 45 | 22 | public static function start(): void { |
|
| 46 | 22 | self::$aclList = new AclList(); |
|
| 47 | 22 | self::$aclList->init(); |
|
| 48 | 22 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Load acls, roles, resources and permissions from providers. |
||
| 52 | * |
||
| 53 | * @param AclProviderInterface[] $providers |
||
| 54 | */ |
||
| 55 | 11 | public static function initFromProviders(?array $providers = []): void { |
|
| 62 | } |
||
| 63 | 11 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * |
||
| 67 | * @param array|string $selectedProviders |
||
| 68 | */ |
||
| 69 | 1 | public static function reloadFromSelectedProviders($selectedProviders = '*') { |
|
| 70 | 1 | self::$aclList->clear(); |
|
| 71 | 1 | $providers = []; |
|
| 72 | 1 | foreach (self::$aclList->getProviders() as $prov) { |
|
| 73 | 1 | if ($selectedProviders === '*' || \array_search(\get_class($prov), $selectedProviders) !== false) { |
|
|
1 ignored issue
–
show
|
|||
| 74 | 1 | $providers[] = $prov; |
|
| 75 | } |
||
| 76 | } |
||
| 77 | 1 | self::initFromProviders($providers); |
|
| 78 | 1 | } |
|
| 79 | |||
| 80 | 6 | public static function addRole(string $name, ?array $parents = []) { |
|
| 82 | 6 | } |
|
| 83 | |||
| 84 | 1 | public static function addRoles(array $nameParents) { |
|
| 85 | 1 | foreach ($nameParents as $name => $parents) { |
|
| 86 | 1 | self::$aclList->addRole(new Role($name, $parents)); |
|
| 87 | } |
||
| 88 | 1 | } |
|
| 89 | |||
| 90 | 8 | public static function addResource(string $name, ?string $value = null) { |
|
| 91 | 8 | self::$aclList->addResource(new Resource($name, $value)); |
|
| 92 | 8 | } |
|
| 93 | |||
| 94 | 1 | public static function addResources(array $nameValue) { |
|
| 95 | 1 | foreach ($nameValue as $name => $value) { |
|
| 96 | 1 | self::$aclList->addResource(new Resource($name, $value)); |
|
| 97 | } |
||
| 98 | 1 | } |
|
| 99 | |||
| 100 | 10 | public static function addPermission(string $name, int $level = 0) { |
|
| 101 | 10 | self::$aclList->addPermission(new Permission($name, $level)); |
|
| 102 | 10 | } |
|
| 103 | |||
| 104 | 1 | public static function addPermissions(array $nameLevel) { |
|
| 105 | 1 | foreach ($nameLevel as $name => $level) { |
|
| 106 | 1 | self::$aclList->addPermission(new Permission($name, $level)); |
|
| 107 | } |
||
| 108 | 1 | } |
|
| 109 | |||
| 110 | 3 | public static function setPermissionLevel(string $name, int $level) { |
|
| 111 | 3 | self::$aclList->setPermissionLevel($name, $level); |
|
| 112 | 2 | } |
|
| 113 | |||
| 114 | 10 | public static function getRoles() { |
|
| 115 | 10 | return self::$aclList->getRoles(); |
|
| 116 | } |
||
| 117 | |||
| 118 | 9 | public static function getResources() { |
|
| 119 | 9 | return self::$aclList->getResources(); |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * |
||
| 124 | * @return \Ubiquity\security\acl\models\AclList |
||
| 125 | */ |
||
| 126 | 8 | public static function getAclList() { |
|
| 127 | 8 | return AclManager::$aclList; |
|
| 128 | } |
||
| 129 | |||
| 130 | 13 | public static function getPermissions() { |
|
| 132 | } |
||
| 133 | |||
| 134 | 7 | public static function getAcls() { |
|
| 135 | 7 | return self::$aclList->getAcls(); |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Allow role to access to resource with the permission. |
||
| 140 | * |
||
| 141 | * @param string $role |
||
| 142 | * @param string $resource |
||
| 143 | * @param string $permission |
||
| 144 | */ |
||
| 145 | 9 | public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
|
| 146 | 9 | self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
|
| 147 | 9 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Add role, resource and permission and allow this role to access to resource with the permission. |
||
| 151 | * |
||
| 152 | * @param string $role |
||
| 153 | * @param string $resource |
||
| 154 | * @param string $permission |
||
| 155 | */ |
||
| 156 | 4 | public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
|
| 157 | 4 | self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL'); |
|
| 158 | 4 | } |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Check if access to resource is allowed for role with the permission. |
||
| 162 | * |
||
| 163 | * @param string $role |
||
| 164 | * @param string $resource |
||
| 165 | * @param string $permission |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | 20 | public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool { |
|
| 169 | 20 | return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL'); |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Save all acls,roles, resources and permissions for AclProviders with no autoSave. |
||
| 174 | */ |
||
| 175 | 5 | public static function saveAll() { |
|
| 176 | 5 | self::$aclList->saveAll(); |
|
| 177 | 5 | } |
|
| 178 | |||
| 179 | /** |
||
| 180 | * |
||
| 181 | * @param string $role |
||
| 182 | */ |
||
| 183 | 4 | public static function removeRole(string $role) { |
|
| 184 | 4 | self::$aclList->removeRole($role); |
|
| 185 | 4 | } |
|
| 186 | |||
| 187 | /** |
||
| 188 | * |
||
| 189 | * @param string $permission |
||
| 190 | */ |
||
| 191 | 4 | public static function removePermission(string $permission) { |
|
| 192 | 4 | self::$aclList->removePermission($permission); |
|
| 193 | 4 | } |
|
| 194 | |||
| 195 | /** |
||
| 196 | * |
||
| 197 | * @param string $resource |
||
| 198 | */ |
||
| 199 | 2 | public static function removeResource(string $resource) { |
|
| 200 | 2 | self::$aclList->removeResource($resource); |
|
| 201 | 2 | } |
|
| 202 | |||
| 203 | /** |
||
| 204 | * |
||
| 205 | * @param string $role |
||
| 206 | * @param string $resource |
||
| 207 | * @param string $permission |
||
| 208 | */ |
||
| 209 | 4 | public static function removeAcl(string $role, string $resource, string $permission = null) { |
|
| 210 | 4 | self::$aclList->removeAcl($role, $resource, $permission); |
|
| 211 | 4 | } |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Initialize acls cache with controllers annotations. |
||
| 215 | * Do not execute at runtime |
||
| 216 | * |
||
| 217 | * @param array $config |
||
| 218 | * @throws \Ubiquity\exceptions\AclException |
||
| 219 | */ |
||
| 220 | 3 | public static function initCache(&$config) { |
|
| 221 | 3 | CacheManager::start($config); |
|
| 222 | 3 | CacheManager::registerAnnotations([ |
|
| 223 | 3 | 'allow' => AllowAnnotation::class, |
|
| 224 | 'resource' => ResourceAnnotation::class, |
||
| 225 | 'permission' => PermissionAnnotation::class |
||
| 226 | ]); |
||
| 227 | 3 | $files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true); |
|
| 228 | 3 | $parser = new AclControllerParser(); |
|
| 229 | 3 | $parser->init(); |
|
| 230 | 3 | foreach ($files as $file) { |
|
| 231 | 3 | if (\is_file($file)) { |
|
| 232 | 3 | $controller = ClassUtils::getClassFullNameFromFile($file); |
|
| 233 | try { |
||
| 234 | 3 | $parser->parse($controller); |
|
| 235 | } catch (\Exception $e) { |
||
| 236 | if ($e instanceof AclException) { |
||
| 237 | throw $e; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | 3 | $parser->save(); |
|
| 243 | 3 | } |
|
| 244 | |||
| 245 | /** |
||
| 246 | * |
||
| 247 | * @return \Ubiquity\security\acl\cache\PermissionsMap |
||
| 248 | */ |
||
| 249 | 1 | public static function getPermissionMap() { |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * |
||
| 259 | * @param string $controller |
||
| 260 | * @param string $action |
||
| 261 | * @param string $resource |
||
| 262 | * @param string $permission |
||
| 263 | */ |
||
| 264 | 1 | public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL') { |
|
| 265 | 1 | self::$aclList->getResourceByName($resource); |
|
| 266 | 1 | self::$aclList->getPermissionByName($permission); |
|
| 267 | 1 | self::$permissionMap->addAction($controller, $action, $resource, $permission); |
|
| 268 | 1 | } |
|
| 269 | |||
| 270 | /** |
||
| 271 | * |
||
| 272 | * @param AbstractAclPart $part |
||
| 273 | * @param string $providerClass |
||
| 274 | * @return boolean |
||
| 275 | */ |
||
| 276 | 2 | public static function existPartIn(AbstractAclPart $part, string $providerClass) { |
|
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * |
||
| 282 | * @param AclElement $elm |
||
| 283 | * @param string $providerClass |
||
| 284 | * @return boolean |
||
| 285 | */ |
||
| 286 | 2 | public static function existAclIn(AclElement $elm, string $providerClass) { |
|
| 287 | 2 | return self::$aclList->existAclIn($elm, $providerClass); |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * |
||
| 292 | * @param string $providerClass |
||
| 293 | * @return \Ubiquity\security\acl\persistence\AclProviderInterface|NULL |
||
| 294 | */ |
||
| 295 | 2 | public static function getProvider(string $providerClass) { |
|
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 |