diff --git a/composer.lock b/composer.lock index 5478223..9934674 100644 --- a/composer.lock +++ b/composer.lock @@ -1416,12 +1416,12 @@ "source": { "type": "git", "url": "https://github.com/LM-Commons/LmcRbacMvc.git", - "reference": "eb68b0860aa889070c4397b55620d8a43ef743a7" + "reference": "5e550b55f6ccb9fd980aa0c8ca6351d4c837d066" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/LM-Commons/LmcRbacMvc/zipball/eb68b0860aa889070c4397b55620d8a43ef743a7", - "reference": "eb68b0860aa889070c4397b55620d8a43ef743a7", + "url": "https://api.github.com/repos/LM-Commons/LmcRbacMvc/zipball/5e550b55f6ccb9fd980aa0c8ca6351d4c837d066", + "reference": "5e550b55f6ccb9fd980aa0c8ca6351d4c837d066", "shasum": "" }, "require": { @@ -1446,12 +1446,13 @@ "type": "library", "extra": { "laminas": { - "module": "LmcRbacMvc" + "module": "Lmc\\Rbac\\Mvc", + "config-provider": "Lmc\\Rbac\\Mvc\\ConfigProvider" } }, "autoload": { "psr-4": { - "LmcRbacMvc\\": "src" + "Lmc\\Rbac\\Mvc\\": "src" } }, "notification-url": "https://packagist.org/downloads/", @@ -1490,7 +1491,7 @@ "issues": "https://github.com/LM-Commons/LmcRbacMvc/issues", "source": "https://github.com/LM-Commons/LmcRbacMvc/tree/4.0.x" }, - "time": "2024-08-16T17:34:09+00:00" + "time": "2024-08-16T20:37:07+00:00" }, { "name": "nikic/php-parser", diff --git a/src/Collector/RbacCollector.php b/src/Collector/RbacCollector.php index eaf99ea..bdee314 100644 --- a/src/Collector/RbacCollector.php +++ b/src/Collector/RbacCollector.php @@ -1,4 +1,7 @@ - * @license MIT */ class RbacCollector implements CollectorInterface, Serializable { /** * Collector priority */ - const PRIORITY = -100; - - protected array $collectedGuards = []; - - protected array $collectedRoles = []; - + const PRIORITY = -100; + protected array $collectedGuards = []; + protected array $collectedRoles = []; protected array $collectedPermissions = []; - - protected array $collectedOptions = []; - - /** - * Collector Name. - * - * @return string - */ + protected array $collectedOptions = []; +/** + * Collector Name. + */ public function getName(): string { return 'lmc_rbac'; @@ -64,8 +65,6 @@ public function getName(): string /** * Collector Priority. - * - * @return integer */ public function getPriority(): int { @@ -75,53 +74,40 @@ public function getPriority(): int /** * Collects data. * - * @param MvcEvent $mvcEvent * @throws ReflectionException */ public function collect(MvcEvent $mvcEvent): void { - if (!$application = $mvcEvent->getApplication()) { + if (! $application = $mvcEvent->getApplication()) { return; } $serviceManager = $application->getServiceManager(); - - /* @var RoleService $roleService */ - $roleService = $serviceManager->get('LmcRbacMvc\Service\RoleService'); - - /* @var ModuleOptions $options */ - $options = $serviceManager->get('LmcRbacMvc\Options\ModuleOptions'); - - // Start collect all the data we need! - $this->collectOptions($options); +/** @var RoleService $roleService */ + $roleService = $serviceManager->get(RoleService::class); +/** @var ModuleOptions $options */ + $options = $serviceManager->get(ModuleOptions::class); + $this->collectOptions($options); $this->collectGuards($options->getGuards()); $this->collectIdentityRolesAndPermissions($roleService); } /** * Collect options - * - * @param ModuleOptions $moduleOptions - * @return void */ private function collectOptions(ModuleOptions $moduleOptions): void { - $this->collectedOptions = [ - 'guest_role' => $moduleOptions->getGuestRole(), - 'protection_policy' => $moduleOptions->getProtectionPolicy() - ]; + $this->collectedOptions = ['guest_role' => $moduleOptions->getGuestRole(), 'protection_policy' => $moduleOptions->getProtectionPolicy()]; } /** * Collect guards * * @param array $guards - * @return void */ private function collectGuards(array $guards): void { $this->collectedGuards = []; - foreach ($guards as $type => $rules) { $this->collectedGuards[$type] = $rules; } @@ -130,25 +116,17 @@ private function collectGuards(array $guards): void /** * Collect roles and permissions * - * @param RoleService $roleService - * @return void * @throws ReflectionException */ private function collectIdentityRolesAndPermissions(RoleService $roleService): void { $identityRoles = $roleService->getIdentityRoles(); - foreach ($identityRoles as $role) { $roleName = $role->getName(); - if (empty($role->getChildren())) { $this->collectedRoles[] = $roleName; } else { - $iteratorIterator = new RecursiveIteratorIterator( - new RecursiveRoleIterator($role->getChildren()), - RecursiveIteratorIterator::SELF_FIRST - ); - + $iteratorIterator = new RecursiveIteratorIterator(new RecursiveRoleIterator($role->getChildren()), RecursiveIteratorIterator::SELF_FIRST); foreach ($iteratorIterator as $childRole) { $this->collectedRoles[$roleName][] = $childRole->getName(); $this->collectPermissions($childRole); @@ -162,8 +140,6 @@ private function collectIdentityRolesAndPermissions(RoleService $roleService): v /** * Collect permissions for the given role * - * @param RoleInterface $role - * @return void * @throws ReflectionException */ private function collectPermissions(RoleInterface $role): void @@ -171,11 +147,8 @@ private function collectPermissions(RoleInterface $role): void if (method_exists($role, 'getPermissions')) { $permissions = $role->getPermissions(); } else { - // Gather the permissions for the given role. We have to use reflection as - // the RoleInterface does not have "getPermissions" method - $reflectionProperty = new ReflectionProperty($role, 'permissions'); - - $permissions = $reflectionProperty->getValue($role); + $reflectionProperty = new ReflectionProperty($role, 'permissions'); + $permissions = $reflectionProperty->getValue($role); } if ($permissions instanceof Traversable) { @@ -185,7 +158,6 @@ private function collectPermissions(RoleInterface $role): void array_walk($permissions, function (&$permission) { $permission = (string) $permission; }); - $this->collectedPermissions[$role->getName()] = array_values($permissions); } @@ -198,9 +170,13 @@ public function getCollection(): array 'guards' => $this->collectedGuards, 'roles' => $this->collectedRoles, 'permissions' => $this->collectedPermissions, - 'options' => $this->collectedOptions + 'options' => $this->collectedOptions, ]; - } + } // Start collect all the data we need! + + // Gather the permissions for the given role. We have to use reflection as + // the RoleInterface does not have "getPermissions" method + /** * {@inheritDoc} */ @@ -215,7 +191,7 @@ public function serialize(): ?string public function unserialize($data): void { $collection = unserialize($data); - if (!is_array($collection)) { + if (! is_array($collection)) { throw new InvalidArgumentException(__METHOD__ . ": Unserialized data should be an array."); } @@ -229,9 +205,9 @@ public function __serialize(): array public function __unserialize(array $data): void { - $this->collectedGuards = $data['guards']; - $this->collectedRoles = $data['roles']; - $this->collectedPermissions = $data['permissions']; - $this->collectedOptions = $data['options']; + $this->collectedGuards = $data['guards']; + $this->collectedRoles = $data['roles']; + $this->collectedPermissions = $data['permissions']; + $this->collectedOptions = $data['options']; } } diff --git a/test/Collector/RbacCollectorTest.php b/test/Collector/RbacCollectorTest.php index 159d401..d751a8f 100644 --- a/test/Collector/RbacCollectorTest.php +++ b/test/Collector/RbacCollectorTest.php @@ -24,11 +24,11 @@ use Lmc\Rbac\Role\RoleInterface; use Laminas\Mvc\MvcEvent; use LmcRbac\Mvc\DevTools\Collector\RbacCollector; -use LmcRbacMvc\Guard\GuardInterface; -use LmcRbacMvc\Options\ModuleOptions; +use Lmc\Rbac\Mvc\Guard\GuardInterface; +use Lmc\Rbac\Mvc\Options\ModuleOptions; use Lmc\Rbac\Role\InMemoryRoleProvider; -use LmcRbacMvc\Service\RoleService; -use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; +use Lmc\Rbac\Mvc\Service\RoleService; +use Lmc\Rbac\Mvc\Role\RecursiveRoleIteratorStrategy; use LmcRbac\Mvc\DevToolsTest\Asset\MockRoleWithPermissionMethod; use LmcRbac\Mvc\DevToolsTest\Asset\MockRoleWithPermissionProperty; use Lmc\Rbac\Service\RoleService as BaseRoleService; @@ -108,10 +108,10 @@ public function testCanCollect() 'guest_role' => 'guest', 'protection_policy' => GuardInterface::POLICY_ALLOW, 'guards' => [ - 'LmcRbacMvc\Guard\RouteGuard' => [ + 'Lmc\Rbac\Mvc\Guard\RouteGuard' => [ 'admin*' => ['*'] ], - 'LmcRbacMvc\Guard\ControllerGuard' => [ + 'Lmc\Rbac\Mvc\Guard\ControllerGuard' => [ [ 'controller' => 'Foo', 'roles' => ['*'] @@ -145,14 +145,14 @@ public function testCanCollect() $identity = $this->createMock(IdentityInterface::class); $identity->expects($this->once())->method('getRoles')->willReturn($dataToCollect['identity_role']); - $identityProvider = $this->createMock(\LmcRbacMvc\Identity\IdentityProviderInterface::class); + $identityProvider = $this->createMock(\Lmc\Rbac\Mvc\Identity\IdentityProviderInterface::class); $identityProvider->expects($this->once())->method('getIdentity')->willReturn($identity); $baseRoleService = new BaseRoleService(new InMemoryRoleProvider($dataToCollect['role_config']), 'guest'); $roleService = new RoleService($identityProvider, $baseRoleService, new RecursiveRoleIteratorStrategy()); - $serviceManager->setService('LmcRbacMvc\Service\RoleService', $roleService); - $serviceManager->setService('LmcRbacMvc\Options\ModuleOptions', new ModuleOptions($dataToCollect['module_options'])); + $serviceManager->setService('Lmc\Rbac\Mvc\Service\RoleService', $roleService); + $serviceManager->setService('Lmc\Rbac\Mvc\Options\ModuleOptions', new ModuleOptions($dataToCollect['module_options'])); $collector = new RbacCollector(); $collector->collect($mvcEvent); @@ -161,10 +161,10 @@ public function testCanCollect() $expectedCollection = [ 'guards' => [ - 'LmcRbacMvc\Guard\RouteGuard' => [ + 'Lmc\Rbac\Mvc\Guard\RouteGuard' => [ 'admin*' => ['*'] ], - 'LmcRbacMvc\Guard\ControllerGuard' => [ + 'Lmc\Rbac\Mvc\Guard\ControllerGuard' => [ [ 'controller' => 'Foo', 'roles' => ['*'] @@ -273,7 +273,7 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role): array ->method('getRoles') ->willReturn([$role]); - $identityProvider = $this->createMock(\LmcRbacMvc\Identity\IdentityProviderInterface::class); + $identityProvider = $this->createMock(\Lmc\Rbac\Mvc\Identity\IdentityProviderInterface::class); $identityProvider->expects($this->once()) ->method('getIdentity') ->will($this->returnValue($identity)); @@ -287,8 +287,8 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role): array $baseRoleService, new RecursiveRoleIteratorStrategy() ); - $serviceManager->setService('LmcRbacMvc\Service\RoleService', $roleService); - $serviceManager->setService('LmcRbacMvc\Options\ModuleOptions', new ModuleOptions()); + $serviceManager->setService('Lmc\Rbac\Mvc\Service\RoleService', $roleService); + $serviceManager->setService('Lmc\Rbac\Mvc\Options\ModuleOptions', new ModuleOptions()); $collector = new RbacCollector(); $collector->collect($mvcEvent);