Skip to content

Commit

Permalink
Merge pull request LM-Commons#4 from visto9259/1.0.x
Browse files Browse the repository at this point in the history
updated for namespace change to Lmc\Rbac\Mvc
  • Loading branch information
visto9259 authored Aug 16, 2024
2 parents 9389d10 + 4004167 commit 4fa73a4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 90 deletions.
13 changes: 7 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 46 additions & 70 deletions src/Collector/RbacCollector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -18,54 +21,50 @@

namespace LmcRbac\Mvc\DevTools\Collector;

use InvalidArgumentException;
use Laminas\DeveloperTools\Collector\CollectorInterface;
use Laminas\Mvc\MvcEvent;
use Lmc\Rbac\Mvc\Options\ModuleOptions;
use Lmc\Rbac\Mvc\Role\RecursiveRoleIterator;
use Lmc\Rbac\Mvc\Service\RoleService;
use Lmc\Rbac\Role\RoleInterface;
use LmcRbacMvc\Role\RecursiveRoleIterator;
use RecursiveIteratorIterator;
use ReflectionProperty;
use ReflectionException;
use ReflectionProperty;
use Serializable;
use Traversable;
use Laminas\Mvc\MvcEvent;
use Laminas\DeveloperTools\Collector\CollectorInterface;
use LmcRbacMvc\Options\ModuleOptions;
use LmcRbacMvc\Service\RoleService;
use InvalidArgumentException;

use function array_values;
use function array_walk;
use function is_array;
use function iterator_to_array;
use function method_exists;
use function serialize;
use function unserialize;

/**
* RbacCollector
*
* @author Michaël Gallego <[email protected]>
* @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';
}

/**
* Collector Priority.
*
* @return integer
*/
public function getPriority(): int
{
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -162,20 +140,15 @@ 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
{
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) {
Expand All @@ -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);
}

Expand All @@ -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}
*/
Expand All @@ -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.");
}

Expand All @@ -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'];
}
}
28 changes: 14 additions & 14 deletions test/Collector/RbacCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' => ['*']
Expand Down Expand Up @@ -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);

Expand All @@ -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' => ['*']
Expand Down Expand Up @@ -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));
Expand All @@ -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);

Expand Down

0 comments on commit 4fa73a4

Please sign in to comment.