| Total Complexity | 62 |
| Total Lines | 357 |
| Duplicated Lines | 0 % |
| Coverage | 81.13% |
| Changes | 7 | ||
| Bugs | 1 | Features | 2 |
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 | protected static ?AclList $aclList=null; |
||
| 31 | |||
| 32 | protected static PermissionsMap $permissionMap; |
||
| 33 | |||
| 34 | protected static array $providersPersistence; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Create AclList with default roles and resources. |
||
| 38 | 22 | */ |
|
| 39 | 22 | public static function start(): void { |
|
| 40 | 22 | self::$aclList = new AclList(); |
|
| 41 | 22 | self::$aclList->init(); |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Start the Acls with AclCacheProvider (for attributes or annotations). |
||
| 46 | 6 | */ |
|
| 47 | 6 | public static function startWithCacheProvider(): void { |
|
| 48 | 6 | self::start(); |
|
| 49 | 6 | self::initFromProviders([new AclCacheProvider()]); |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Check whether the Acl service is started. |
||
| 54 | * |
||
| 55 | * @return bool |
||
| 56 | 2 | */ |
|
| 57 | 2 | public static function isStarted(): bool { |
|
| 58 | return isset(self::$aclList) && (self::$aclList instanceof AclList); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Load acls, roles, resources and permissions from providers. |
||
| 63 | * |
||
| 64 | * @param AclProviderInterface[] $providers |
||
| 65 | 11 | */ |
|
| 66 | 11 | public static function initFromProviders(?array $providers = []): void { |
|
| 67 | 11 | self::$aclList->setProviders($providers); |
|
|
1 ignored issue
–
show
|
|||
| 68 | 10 | if (\count($providers) > 0) { |
|
| 69 | 10 | self::$aclList->loadAcls(); |
|
| 70 | 10 | self::$aclList->loadRoles(); |
|
| 71 | 10 | self::$aclList->loadResources(); |
|
| 72 | self::$aclList->loadPermissions(); |
||
| 73 | 11 | } |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @param array|string $selectedProviders |
||
| 79 | 3 | */ |
|
| 80 | 3 | public static function reloadFromSelectedProviders($selectedProviders = '*') { |
|
| 81 | 3 | $sProviders = self::$aclList->getProviders(); |
|
| 82 | 3 | self::$aclList->clear(); |
|
| 83 | 3 | $providers = []; |
|
| 84 | 3 | foreach ($sProviders as $prov) { |
|
| 85 | 3 | if ($selectedProviders === '*' || (\is_array($selectedProviders) && \array_search(\get_class($prov), $selectedProviders) !== false)) { |
|
| 86 | $providers[] = $prov; |
||
| 87 | } |
||
| 88 | 3 | } |
|
| 89 | 3 | self::initFromProviders($providers); |
|
| 90 | 3 | self::$aclList->setProviders($sProviders); |
|
| 91 | } |
||
| 92 | 8 | ||
| 93 | 8 | public static function addRole(string $name, ?array $parents = []) { |
|
| 94 | 8 | self::$aclList->addRole(new Role($name, $parents)); |
|
| 95 | } |
||
| 96 | 1 | ||
| 97 | 1 | public static function addRoles(array $nameParents) { |
|
| 98 | 1 | foreach ($nameParents as $name => $parents) { |
|
| 99 | self::$aclList->addRole(new Role($name, $parents)); |
||
| 100 | 1 | } |
|
| 101 | } |
||
| 102 | 7 | ||
| 103 | 7 | public static function addResource(string $name, ?string $value = null) { |
|
| 104 | 7 | self::$aclList->addResource(new Resource($name, $value)); |
|
| 105 | } |
||
| 106 | 1 | ||
| 107 | 1 | public static function addResources(array $nameValue) { |
|
| 108 | 1 | foreach ($nameValue as $name => $value) { |
|
| 109 | self::$aclList->addResource(new Resource($name, $value)); |
||
| 110 | 1 | } |
|
| 111 | } |
||
| 112 | 9 | ||
| 113 | 9 | public static function addPermission(string $name, int $level = 0) { |
|
| 114 | 9 | self::$aclList->addPermission(new Permission($name, $level)); |
|
| 115 | } |
||
| 116 | 1 | ||
| 117 | 1 | public static function addPermissions(array $nameLevel) { |
|
| 118 | 1 | foreach ($nameLevel as $name => $level) { |
|
| 119 | self::$aclList->addPermission(new Permission($name, $level)); |
||
| 120 | 1 | } |
|
| 121 | } |
||
| 122 | 3 | ||
| 123 | 3 | public static function setPermissionLevel(string $name, int $level) { |
|
| 124 | 2 | self::$aclList->setPermissionLevel($name, $level); |
|
| 125 | } |
||
| 126 | 8 | ||
| 127 | 8 | public static function getRoles() { |
|
| 128 | return self::$aclList->getRoles(); |
||
| 129 | } |
||
| 130 | 7 | ||
| 131 | 7 | public static function getResources() { |
|
| 132 | return self::$aclList->getResources(); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * |
||
| 137 | * @return \Ubiquity\security\acl\models\AclList |
||
| 138 | 3 | */ |
|
| 139 | 3 | public static function getAclList() { |
|
| 140 | return AclManager::$aclList; |
||
| 141 | } |
||
| 142 | 11 | ||
| 143 | 11 | public static function getPermissions():array { |
|
| 144 | return self::$aclList->getPermissions(); |
||
| 145 | } |
||
| 146 | 6 | ||
| 147 | 6 | public static function getAcls() { |
|
| 148 | return self::$aclList->getAcls(); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Allow role to access to resource with the permission. |
||
| 153 | * |
||
| 154 | * @param string $role |
||
| 155 | * @param ?string $resource |
||
| 156 | * @param ?string $permission |
||
| 157 | 10 | */ |
|
| 158 | 10 | public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
|
| 159 | 10 | self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Add role, resource and permission and allow this role to access to resource with the permission. |
||
| 164 | * |
||
| 165 | * @param string $role |
||
| 166 | * @param ?string $resource |
||
| 167 | * @param ?string $permission |
||
| 168 | 3 | */ |
|
| 169 | 3 | public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
|
| 170 | 3 | self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL'); |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Check if access to resource is allowed for role with the permission. |
||
| 175 | * |
||
| 176 | * @param string $role |
||
| 177 | * @param ?string $resource |
||
| 178 | * @param ?string $permission |
||
| 179 | * @return bool |
||
| 180 | 20 | */ |
|
| 181 | 20 | public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool { |
|
| 182 | return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL'); |
||
| 183 | } |
||
| 184 | |||
| 185 | public static function isAllowedRoute(string $role,string $routeName): bool { |
||
| 186 | $routeInfo=Router::getRouteInfoByName($routeName); |
||
| 187 | if (!isset ( $routeInfo ['controller'] )) { |
||
| 188 | $routeInfo=\current($routeInfo); |
||
|
1 ignored issue
–
show
|
|||
| 189 | } |
||
| 190 | $controller=$routeInfo['controller']??null; |
||
| 191 | $action=$routeInfo['action']??null; |
||
| 192 | if(isset($controller) && isset($action)){ |
||
| 193 | $resourceController = self::getPermissionMap ()->getRessourcePermission ( $controller, $action ); |
||
| 194 | if (isset ( $resourceController )) { |
||
| 195 | try{ |
||
| 196 | if (self::isAllowed ( $role, $resourceController ['resource'], $resourceController ['permission'] )) { |
||
| 197 | return true; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | catch(AclException $e){ |
||
| 201 | //Nothing to do |
||
| 202 | } |
||
| 203 | } |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Save all acls,roles, resources and permissions for AclProviders with no autoSave. |
||
| 211 | 4 | */ |
|
| 212 | 4 | public static function saveAll(): void { |
|
| 213 | 4 | self::$aclList->saveAll(); |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * |
||
| 218 | * @param string $role |
||
| 219 | 2 | */ |
|
| 220 | 2 | public static function removeRole(string $role) { |
|
| 221 | 2 | self::$aclList->removeRole($role); |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * |
||
| 226 | * @param string $permission |
||
| 227 | 2 | */ |
|
| 228 | 2 | public static function removePermission(string $permission) { |
|
| 229 | 2 | self::$aclList->removePermission($permission); |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * |
||
| 234 | * @param string $resource |
||
| 235 | */ |
||
| 236 | public static function removeResource(string $resource) { |
||
| 237 | self::$aclList->removeResource($resource); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * |
||
| 242 | * @param string $role |
||
| 243 | * @param string $resource |
||
| 244 | * @param ?string $permission |
||
| 245 | 2 | */ |
|
| 246 | 2 | public static function removeAcl(string $role, string $resource, ?string $permission = null) { |
|
| 247 | 2 | self::$aclList->removeAcl($role, $resource, $permission); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Initialize acls cache with controllers annotations. |
||
| 252 | * Do not execute at runtime |
||
| 253 | * |
||
| 254 | * @param array $config |
||
| 255 | * @throws \Ubiquity\exceptions\AclException |
||
| 256 | 2 | */ |
|
| 257 | 2 | public static function initCache(&$config) { |
|
| 258 | if(!self::isStarted()){ |
||
| 259 | self::start(); |
||
| 260 | self::initFromProviders([ |
||
| 261 | new AclCacheProvider() |
||
| 262 | ]); |
||
| 263 | 2 | } |
|
| 264 | 2 | self::filterProviders(AclCacheProvider::class); |
|
| 265 | 2 | self::reloadFromSelectedProviders([]); |
|
| 266 | 2 | self::registerAnnotations(); |
|
| 267 | 2 | $files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true); |
|
| 268 | 2 | $parser = new AclControllerParser(); |
|
| 269 | 2 | $parser->init(); |
|
| 270 | 2 | foreach ($files as $file) { |
|
| 271 | 2 | if (\is_file($file)) { |
|
| 272 | $controller = ClassUtils::getClassFullNameFromFile($file); |
||
| 273 | 2 | try { |
|
| 274 | $parser->parse($controller); |
||
| 275 | } catch (\Exception $e) { |
||
| 276 | if ($e instanceof AclException) { |
||
| 277 | throw $e; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | 2 | } |
|
| 282 | 2 | $parser->save(); |
|
| 283 | 2 | self::removefilterProviders(); |
|
| 284 | 2 | self::reloadFromSelectedProviders(); |
|
| 285 | } |
||
| 286 | 2 | ||
| 287 | 2 | protected static function registerAnnotations() { |
|
| 288 | 2 | CacheManager::getAnnotationsEngineInstance()->registerAcls(); |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * |
||
| 293 | * @return \Ubiquity\security\acl\cache\PermissionsMap |
||
| 294 | 1 | */ |
|
| 295 | 1 | public static function getPermissionMap():PermissionsMap { |
|
| 296 | 1 | if (! isset(self::$permissionMap)) { |
|
| 297 | 1 | self::$permissionMap = new PermissionsMap(); |
|
| 298 | self::$permissionMap->load(); |
||
| 299 | 1 | } |
|
| 300 | return self::$permissionMap; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * |
||
| 305 | * @param string $controller |
||
| 306 | * @param string $action |
||
| 307 | * @param string $resource |
||
| 308 | * @param string $permission |
||
| 309 | 1 | */ |
|
| 310 | 1 | public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL'):void { |
|
| 311 | 1 | self::$aclList->getResourceByName($resource); |
|
| 312 | 1 | self::$aclList->getPermissionByName($permission); |
|
| 313 | 1 | self::$permissionMap->addAction($controller, $action, $resource, $permission); |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * |
||
| 318 | * @param AbstractAclPart $part |
||
| 319 | * @param string $providerClass |
||
| 320 | * @return boolean |
||
| 321 | 2 | */ |
|
| 322 | 2 | public static function existPartIn(AbstractAclPart $part, string $providerClass):bool { |
|
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * |
||
| 328 | * @param AclElement $elm |
||
| 329 | * @param string $providerClass |
||
| 330 | * @return boolean |
||
| 331 | 2 | */ |
|
| 332 | 2 | public static function existAclIn(AclElement $elm, string $providerClass):bool { |
|
| 333 | return self::$aclList->existAclIn($elm, $providerClass); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * |
||
| 338 | * @param string $providerClass |
||
| 339 | * @return AclProviderInterface|NULL |
||
| 340 | 2 | */ |
|
| 341 | 2 | public static function getProvider(string $providerClass):?AclProviderInterface { |
|
| 343 | } |
||
| 344 | |||
| 345 | public static function getModelClassesSwap(): array { |
||
| 346 | $result = []; |
||
| 347 | $aclList = self::getAclList(); |
||
| 348 | if (isset($aclList)) { |
||
| 349 | foreach ($aclList->getProviders() as $prov) { |
||
| 350 | $result += $prov->getModelClassesSwap(); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | return $result; |
||
| 354 | } |
||
| 355 | 2 | ||
| 356 | 2 | public static function filterProviders(string $providerClass):void { |
|
| 366 | } |
||
| 367 | 2 | ||
| 368 | 2 | public static function removefilterProviders():void { |
|
| 369 | 2 | self::$aclList->setProviders(self::$providersPersistence); |
|
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Initializes AclDAOProvider and creates ACL tables in the specified dbOffset. |
||
| 374 | * Do not use in production |
||
| 375 | * @param array $config |
||
| 376 | * @param string $dbOffset |
||
| 377 | * @param array $classes |
||
| 378 | * associative array['acl'=>'','role'=>'','resource'=>'','permission'=>''] |
||
| 379 | * @return AclDAOProvider |
||
| 380 | * @throws AclException |
||
| 381 | */ |
||
| 382 | public static function initializeDAOProvider(array &$config, string $dbOffset='default', array $classes=[]): AclDAOProvider { |
||
| 385 | } |
||
| 386 | } |
||
| 387 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.