diff --git a/composer.json b/composer.json index a762d152..18fdc5d9 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "laminas/laminas-eventmanager": "^3.0", "laminas/laminas-mvc": "^3.0", "laminas/laminas-servicemanager": "^3.0", - "zfr/rbac": "~1.2", + "laminas/laminas-permissions-rbac": "^3.0", "doctrine/persistence": "^2.1" }, "require-dev": { diff --git a/src/Collector/RbacCollector.php b/src/Collector/RbacCollector.php index 3a0c645a..08600630 100644 --- a/src/Collector/RbacCollector.php +++ b/src/Collector/RbacCollector.php @@ -18,9 +18,8 @@ namespace LmcRbacMvc\Collector; -use Rbac\Role\HierarchicalRoleInterface; -use Rbac\Role\RoleInterface; -use Rbac\Traversal\RecursiveRoleIterator; +use Laminas\Permissions\Rbac\RoleInterface; +use LmcRbacMvc\Role\RecursiveRoleIterator; use RecursiveIteratorIterator; use ReflectionProperty; use ReflectionException; @@ -154,7 +153,7 @@ private function collectIdentityRolesAndPermissions(RoleService $roleService) foreach ($identityRoles as $role) { $roleName = $role->getName(); - if (!$role instanceof HierarchicalRoleInterface) { + if (empty($role->hasChildren())) { $this->collectedRoles[] = $roleName; } else { $iteratorIterator = new RecursiveIteratorIterator( diff --git a/src/Factory/AuthorizationServiceFactory.php b/src/Factory/AuthorizationServiceFactory.php index a8332e00..5cee912b 100644 --- a/src/Factory/AuthorizationServiceFactory.php +++ b/src/Factory/AuthorizationServiceFactory.php @@ -25,7 +25,6 @@ use LmcRbacMvc\Options\ModuleOptions; use LmcRbacMvc\Service\AuthorizationService; use LmcRbacMvc\Service\RoleService; -use Rbac\Rbac; /** * Factory to create the authorization service @@ -43,8 +42,8 @@ class AuthorizationServiceFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - /* @var Rbac $rbac */ - $rbac = $container->get(Rbac::class); + /* @var \Laminas\Permissions\Rbac\Rbac $rbac */ + $rbac = $container->get(\Rbac\Rbac::class); /* @var RoleService $roleService */ $roleService = $container->get(RoleService::class); diff --git a/src/Factory/RbacFactory.php b/src/Factory/RbacFactory.php index 2582e5ca..dd48a05d 100644 --- a/src/Factory/RbacFactory.php +++ b/src/Factory/RbacFactory.php @@ -19,7 +19,7 @@ namespace LmcRbacMvc\Factory; use Psr\Container\ContainerInterface; -use Rbac\Rbac; +use LmcRbacMvc\Rbac\Rbac; use Rbac\Traversal\Strategy\GeneratorStrategy; use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\ServiceLocatorInterface; @@ -38,7 +38,7 @@ class RbacFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - return new Rbac(new GeneratorStrategy()); + return new Rbac(); } /** diff --git a/src/Factory/RoleServiceFactory.php b/src/Factory/RoleServiceFactory.php index ff3f034e..af71965a 100644 --- a/src/Factory/RoleServiceFactory.php +++ b/src/Factory/RoleServiceFactory.php @@ -27,8 +27,8 @@ use LmcRbacMvc\Role\RoleProviderInterface; use LmcRbacMvc\Role\RoleProviderPluginManager; use LmcRbacMvc\Service\RoleService; -use Rbac\Rbac; -use Rbac\Traversal\Strategy\TraversalStrategyInterface; +use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; +use LmcRbacMvc\Role\TraversalStrategyInterface; /** * Factory to create the role service @@ -66,7 +66,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o $roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig)); /* @var TraversalStrategyInterface $traversalStrategy */ - $traversalStrategy = $container->get(Rbac::class)->getTraversalStrategy(); + $traversalStrategy = new RecursiveRoleIteratorStrategy();//$container->get(Rbac::class)->getTraversalStrategy(); $roleService = new RoleService($identityProvider, $roleProvider, $traversalStrategy); $roleService->setGuestRole($moduleOptions->getGuestRole()); diff --git a/src/Permission/PermissionInterface.php b/src/Permission/PermissionInterface.php index b141c408..056d2eed 100644 --- a/src/Permission/PermissionInterface.php +++ b/src/Permission/PermissionInterface.php @@ -18,8 +18,6 @@ namespace LmcRbacMvc\Permission; -use Rbac\Permission\PermissionInterface as BasePermissionInterface; - /** * Interface that permissions must implement to be used with the AuthorizationService * @@ -30,6 +28,6 @@ * @license MIT * TODO Remove deprecated interface */ -interface PermissionInterface extends BasePermissionInterface +interface PermissionInterface { } diff --git a/src/Rbac/Rbac.php b/src/Rbac/Rbac.php new file mode 100644 index 00000000..bd9a50d6 --- /dev/null +++ b/src/Rbac/Rbac.php @@ -0,0 +1,83 @@ +roles; + } + + /** + * Determines if access is granted by checking the role and child roles for permission. + * + * @param RoleInterface|string $role + * @param null|AssertionInterface|Callable $assertion + * @throws Exception\InvalidArgumentException If the role is not found. + * @throws Exception\InvalidArgumentException If the assertion is an invalid type. + */ + public function isGranted($role, string $permission, $assertion = null): bool + { + if ( is_array($role) && $role[0] instanceof RoleInterface) { + $role = $role[0]; + } + if (! $this->hasRole($role)) { + throw new Exception\InvalidArgumentException(sprintf( + 'No role with name "%s" could be found', + is_object($role) ? $role->getName() : $role + )); + } + + if (is_string($role)) { + $role = $this->getRole($role); + } + + $result = $role->hasPermission($permission); + if (false === $result || null === $assertion) { + return $result; + } + + if ( + ! $assertion instanceof AssertionInterface + && ! is_callable($assertion) + ) { + throw new Exception\InvalidArgumentException( + 'Assertions must be a Callable or an instance of Laminas\Permissions\Rbac\AssertionInterface' + ); + } + + if ($assertion instanceof AssertionInterface) { + return $result && $assertion->assert($this, $role, $permission); + } + + // Callable assertion provided. + return $result && $assertion($this, $role, $permission); + } + + /** + * Is a role registered? + * + * @param RoleInterface|string $role + */ + public function hasRole($role): bool + { + if(empty($this->roles)){ + $this->addRole($role); + } + + return parent::hasRole($role); + } + +} \ No newline at end of file diff --git a/src/Role/InMemoryRoleProvider.php b/src/Role/InMemoryRoleProvider.php index 6ccbd7af..47eee89e 100644 --- a/src/Role/InMemoryRoleProvider.php +++ b/src/Role/InMemoryRoleProvider.php @@ -18,9 +18,7 @@ namespace LmcRbacMvc\Role; -use Rbac\Role\RoleInterface; -use Rbac\Role\HierarchicalRole; -use Rbac\Role\Role; +use Laminas\Permissions\Rbac\RoleInterface; /** * Simple role providers that store them in memory (ideal for small websites) @@ -99,7 +97,7 @@ protected function getRole($roleName) $roleConfig = $this->rolesConfig[$roleName]; if (isset($roleConfig['children'])) { - $role = new HierarchicalRole($roleName); + $role = new Role($roleName); $childRoles = (array)$roleConfig['children']; foreach ($childRoles as $childRole) { $childRole = $this->getRole($childRole); diff --git a/src/Role/RecursiveRoleIterator.php b/src/Role/RecursiveRoleIterator.php new file mode 100644 index 00000000..469da604 --- /dev/null +++ b/src/Role/RecursiveRoleIterator.php @@ -0,0 +1,71 @@ +current() instanceof RoleInterface); + } + + /** + * @return bool + */ + public function hasChildren() : bool + { + $current = $this->current(); + + if (!$current instanceof RoleInterface) { + return false; + } + + return $current->hasChildren(); + } + + /** + * @return RecursiveRoleIterator + */ + public function getChildren() :? RecursiveRoleIterator + { + return new RecursiveRoleIterator($this->current()->getChildren()); + } +} diff --git a/src/Role/RecursiveRoleIteratorStrategy.php b/src/Role/RecursiveRoleIteratorStrategy.php new file mode 100644 index 00000000..86632e34 --- /dev/null +++ b/src/Role/RecursiveRoleIteratorStrategy.php @@ -0,0 +1,40 @@ +children instanceof \Doctrine\ORM\PersistentCollection ) + return $this->children->toArray(); + else + return array_values($this->children); + } + + /** + * Get the parent roles. + * + * @return RoleInterface[] + */ + public function getParents(): array + { + return $this->parents; + } + + /** + * {@inheritDoc} + */ + public function hasChildren() + { + return !empty($this->children); + } + + /** + * Check if a role is a descendant. + */ + protected function hasDescendant(RoleInterface $role): bool + { + if(empty($this->children)) + $this->children = []; + + return parent::hasDescendant($role); + } +} \ No newline at end of file diff --git a/src/Role/RoleProviderInterface.php b/src/Role/RoleProviderInterface.php index 57e74437..f0272d46 100644 --- a/src/Role/RoleProviderInterface.php +++ b/src/Role/RoleProviderInterface.php @@ -18,7 +18,7 @@ namespace LmcRbacMvc\Role; -use Rbac\Role\RoleInterface; +use Laminas\Permissions\Rbac\RoleInterface; /** * A role provider is an object that collect roles from string and convert them to RoleInterface instances diff --git a/src/Role/TraversalStrategyInterface.php b/src/Role/TraversalStrategyInterface.php new file mode 100644 index 00000000..8ea0b0da --- /dev/null +++ b/src/Role/TraversalStrategyInterface.php @@ -0,0 +1,34 @@ +roleService->getIdentityRoles(); - +// var_dump(__METHOD__,$roles); if (empty($roles)) { return false; } - +// var_dump('Checking IsGranted by Rbac class'); if (!$this->rbac->isGranted($roles, $permission)) { return false; } diff --git a/src/Service/RoleService.php b/src/Service/RoleService.php index 5e7e2396..211eeeb3 100644 --- a/src/Service/RoleService.php +++ b/src/Service/RoleService.php @@ -18,13 +18,13 @@ namespace LmcRbacMvc\Service; -use Rbac\Role\RoleInterface; +use Laminas\Permissions\Rbac\RoleInterface; use Traversable; use LmcRbacMvc\Exception; use LmcRbacMvc\Identity\IdentityInterface; use LmcRbacMvc\Identity\IdentityProviderInterface; use LmcRbacMvc\Role\RoleProviderInterface; -use Rbac\Traversal\Strategy\TraversalStrategyInterface; +use LmcRbacMvc\Role\TraversalStrategyInterface; /** * Role service diff --git a/tests/Asset/FlatRole.php b/tests/Asset/FlatRole.php index 85991060..5c8edccc 100644 --- a/tests/Asset/FlatRole.php +++ b/tests/Asset/FlatRole.php @@ -20,8 +20,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -use Rbac\Permission\PermissionInterface; -use Rbac\Role\Role; +use LmcRbacMvc\Role\Role; +use LmcRbacMvc\Permission\PermissionInterface; /** * @ORM\Entity @@ -77,7 +77,7 @@ public function getId() * @param PermissionInterface|string $permission * @return void */ - public function addPermission($permission) + public function addPermission($permission):void { if (is_string($permission)) { $name = $permission; diff --git a/tests/Asset/HierarchicalRole.php b/tests/Asset/HierarchicalRole.php index 3cd9f471..2b91de45 100644 --- a/tests/Asset/HierarchicalRole.php +++ b/tests/Asset/HierarchicalRole.php @@ -20,8 +20,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -use Rbac\Permission\PermissionInterface; -use Rbac\Role\HierarchicalRole as BaseHierarchicalRole; +use LmcRbacMvc\Permission\PermissionInterface; +use LmcRbacMvc\Role\Role as BaseHierarchicalRole; /** * @ORM\Entity @@ -46,7 +46,7 @@ class HierarchicalRole extends BaseHierarchicalRole protected $name; /** - * @var \Rbac\Role\RoleInterface[]|\Doctrine\Common\Collections\Collection + * @var \Laminas\Permissions\Rbac\RoleInterface[]|\Doctrine\Common\Collections\Collection * * @ORM\ManyToMany(targetEntity="HierarchicalRole") */ @@ -84,7 +84,7 @@ public function getId() * @param PermissionInterface|string $permission * @return void */ - public function addPermission($permission) + public function addPermission($permission):void { if (is_string($permission)) { $name = $permission; diff --git a/tests/Asset/MockRoleWithPermissionMethod.php b/tests/Asset/MockRoleWithPermissionMethod.php index 8f4013c0..e6db7920 100644 --- a/tests/Asset/MockRoleWithPermissionMethod.php +++ b/tests/Asset/MockRoleWithPermissionMethod.php @@ -2,7 +2,8 @@ namespace LmcRbacMvcTest\Asset; -use Rbac\Role\RoleInterface; + +use Laminas\Permissions\Rbac\RoleInterface; class MockRoleWithPermissionMethod implements RoleInterface { @@ -11,11 +12,45 @@ public function getPermissions() return ['permission-method-a', 'permission-method-b']; } - public function getName() + public function getName() : string { return 'role-with-permission-method'; } - public function hasPermission($permission) + public function hasPermission($permission) : bool + { + return false; + } + + /** + * Add permission to the role. + */ + public function addPermission(string $name): void{ return;} + + /** + * Add a child. + */ + public function addChild(RoleInterface $child): void{return;} + + /** + * Get the children roles. + * + * @return RoleInterface[] + */ + public function getChildren(): iterable{return [];} + + /** + * Add a parent. + */ + public function addParent(RoleInterface $parent): void{return;} + + /** + * Get the parent roles. + * + * @return RoleInterface[] + */ + public function getParents(): iterable{return [];} + + public function hasChildren() : bool { return false; } diff --git a/tests/Asset/MockRoleWithPermissionProperty.php b/tests/Asset/MockRoleWithPermissionProperty.php index ad757725..c4a125a1 100644 --- a/tests/Asset/MockRoleWithPermissionProperty.php +++ b/tests/Asset/MockRoleWithPermissionProperty.php @@ -2,18 +2,63 @@ namespace LmcRbacMvcTest\Asset; -use Rbac\Role\RoleInterface; +use Laminas\Permissions\Rbac\RoleInterface; class MockRoleWithPermissionProperty implements RoleInterface { private $permissions = ['permission-property-a', 'permission-property-b']; - public function getName() + public function getName():string { return 'role-with-permission-property'; } - public function hasPermission($permission) + public function hasPermission($permission): bool { return false; } + + public function addPermission(string $name): void{ + return; + } + + /** + * Add a child. + */ + public function addChild(RoleInterface $child): void{ + return; + } + + /** + * Get the children roles. + * + * @return RoleInterface[] + */ + public function getChildren(): iterable{ + return []; + } + + /** + * Add a parent. + */ + public function addParent(RoleInterface $parent): void{ + return; + } + + /** + * Get the parent roles. + * + * @return RoleInterface[] + */ + public function getParents(): iterable{ + return []; + } + + /** + * Get the children roles. + * + * @return RoleInterface[] + */ + public function hasChildren(): iterable{ + return []; + } } diff --git a/tests/Asset/MockRoleWithPermissionTraversable.php b/tests/Asset/MockRoleWithPermissionTraversable.php index 07ac90e4..f1935139 100644 --- a/tests/Asset/MockRoleWithPermissionTraversable.php +++ b/tests/Asset/MockRoleWithPermissionTraversable.php @@ -3,7 +3,7 @@ namespace LmcRbacMvcTest\Asset; -use Rbac\Role\RoleInterface; +use Laminas\Permissions\Rbac\RoleInterface; class MockRoleWithPermissionTraversable implements RoleInterface @@ -13,13 +13,57 @@ public function getPermissions() return new \ArrayObject(['permission-method-a', 'permission-method-b']); } - public function getName() + public function getName():string { return 'role-with-permission-traversable'; } - public function hasPermission($permission) + public function hasPermission($permission):bool { return false; } + public function addPermission(string $name): void{ + return; + } + + /** + * Add a child. + */ + public function addChild(RoleInterface $child): void{ + return; + } + + /** + * Get the children roles. + * + * @return RoleInterface[] + */ + public function getChildren(): iterable{ + return []; + } + + /** + * Add a parent. + */ + public function addParent(RoleInterface $parent): void{ + return; + } + + /** + * Get the parent roles. + * + * @return RoleInterface[] + */ + public function getParents(): iterable{ + return []; + } + + /** + * Get the children roles. + * + * @return RoleInterface[] + */ + public function hasChildren(): iterable{ + return []; + } } diff --git a/tests/Asset/Permission.php b/tests/Asset/Permission.php index 6fdfae65..706c7ec4 100644 --- a/tests/Asset/Permission.php +++ b/tests/Asset/Permission.php @@ -20,13 +20,12 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; -use Rbac\Permission\PermissionInterface; /** * @ORM\Entity * @ORM\Table(name="permissions") */ -class Permission implements PermissionInterface +class Permission { /** * @var int|null diff --git a/tests/Collector/RbacCollectorTest.php b/tests/Collector/RbacCollectorTest.php index f50354f1..3efa2dae 100644 --- a/tests/Collector/RbacCollectorTest.php +++ b/tests/Collector/RbacCollectorTest.php @@ -21,15 +21,14 @@ use Laminas\ServiceManager\ServiceManager; use LmcRbacMvc\Identity\IdentityInterface; use LmcRbacMvcTest\Asset\MockRoleWithPermissionTraversable; -use Rbac\Role\RoleInterface; +use Laminas\Permissions\Rbac\RoleInterface; use Laminas\Mvc\MvcEvent; -use Laminas\Permissions\Rbac\Role; use LmcRbacMvc\Collector\RbacCollector; use LmcRbacMvc\Guard\GuardInterface; use LmcRbacMvc\Options\ModuleOptions; use LmcRbacMvc\Role\InMemoryRoleProvider; use LmcRbacMvc\Service\RoleService; -use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; +use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; use LmcRbacMvcTest\Asset\MockRoleWithPermissionMethod; use LmcRbacMvcTest\Asset\MockRoleWithPermissionProperty; @@ -178,10 +177,7 @@ public function testCanCollect() $collection = $collector->getCollection(); $expectedCollection = [ - 'options' => [ - 'guest_role' => 'guest', - 'protection_policy' => 'allow' - ], + 'guards' => [ 'LmcRbacMvc\Guard\RouteGuard' => [ 'admin*' => ['*'] @@ -197,9 +193,13 @@ public function testCanCollect() 'member' => ['guest'] ], 'permissions' => [ - 'member' => ['write', 'delete'], + 'member' => ['write', 'delete', 'read'], 'guest' => ['read'] - ] + ], + 'options' => [ + 'guest_role' => 'guest', + 'protection_policy' => 'allow' + ], ]; $this->assertEquals($expectedCollection, $collection); @@ -272,7 +272,7 @@ public function testCollectPermissionsTraversable() /** * Base method for the *collectPermissionProperty tests * @param RoleInterface $role - * @return array|\string[] + * @return array|string[] */ private function collectPermissionsPropertyTestBase(RoleInterface $role) { diff --git a/tests/Factory/AuthorizationServiceFactoryTest.php b/tests/Factory/AuthorizationServiceFactoryTest.php index a7427e96..67c7a7ab 100644 --- a/tests/Factory/AuthorizationServiceFactoryTest.php +++ b/tests/Factory/AuthorizationServiceFactoryTest.php @@ -31,7 +31,7 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $serviceManager->setService('Rbac\Rbac', $this->getMockBuilder('Rbac\Rbac')->disableOriginalConstructor()->getMock()); + $serviceManager->setService('Rbac\Rbac', $this->getMockBuilder(\LmcRbacMvc\Rbac\Rbac::class)->disableOriginalConstructor()->getMock()); $serviceManager->setService( 'LmcRbacMvc\Service\RoleService', diff --git a/tests/Factory/RbacFactoryTest.php b/tests/Factory/RbacFactoryTest.php index 46843a12..b3abe179 100644 --- a/tests/Factory/RbacFactoryTest.php +++ b/tests/Factory/RbacFactoryTest.php @@ -33,7 +33,7 @@ public function testCanCreateFromFactory() $rbac = $factory->createService($serviceManager); - $this->assertInstanceOf('Rbac\Rbac', $rbac); - $this->assertInstanceOf('Rbac\Traversal\Strategy\TraversalStrategyInterface', $rbac->getTraversalStrategy()); + $this->assertInstanceOf('LmcRbacMvc\Rbac\Rbac', $rbac); +// $this->assertInstanceOf('LmcRbacMvc\Role\TraversalStrategyInterface', $rbac->getTraversalStrategy()); } } diff --git a/tests/Factory/RoleServiceFactoryTest.php b/tests/Factory/RoleServiceFactoryTest.php index 808e2b5a..e2bb1dfd 100644 --- a/tests/Factory/RoleServiceFactoryTest.php +++ b/tests/Factory/RoleServiceFactoryTest.php @@ -40,15 +40,15 @@ public function testFactory() ] ]); - $traversalStrategy = $this->createMock('Rbac\Traversal\Strategy\TraversalStrategyInterface'); + $traversalStrategy = $this->createMock('LmcRbacMvc\Role\RecursiveRoleIteratorStrategy'); $roleProvider = $this->createMock('\LmcRbacMvc\Role\RoleProviderInterface'); - $rbac = $this->createMock('Rbac\Rbac'); - $rbac->expects($this->once()) - ->method('getTraversalStrategy') - ->will($this->returnValue( - $traversalStrategy - )); + $rbac = $this->createMock('LmcRbacMvc\Rbac\Rbac'); +// $rbac->expects($this->once()) +// ->method('getTraversalStrategy') +// ->will($this->returnValue( +// $traversalStrategy +// )); $pluginManager = $this->createMock('\LmcRbacMvc\Role\RoleProviderPluginManager'); $pluginManager->expects($this->once()) @@ -90,15 +90,15 @@ public function testIfRoleArrayPointerBeyondArrayEnd() next($roleProvider); $options->setRoleProvider($roleProvider); - $traversalStrategy = $this->createMock('Rbac\Traversal\Strategy\TraversalStrategyInterface'); + $traversalStrategy = $this->createMock('LmcRbacMvc\Role\RecursiveRoleIteratorStrategy'); $roleProvider = $this->createMock('\LmcRbacMvc\Role\RoleProviderInterface'); - $rbac = $this->createMock('Rbac\Rbac'); - $rbac->expects($this->once()) - ->method('getTraversalStrategy') - ->will($this->returnValue( - $traversalStrategy - )); + $rbac = $this->createMock('Laminas\Permissions\Rbac\Rbac'); +// $rbac->expects($this->once()) +// ->method('getTraversalStrategy') +// ->will($this->returnValue( +// $traversalStrategy +// )); $pluginManager = $this->createMock('\LmcRbacMvc\Role\RoleProviderPluginManager'); $pluginManager->expects($this->once()) diff --git a/tests/Guard/ControllerGuardTest.php b/tests/Guard/ControllerGuardTest.php index fd10ea4b..4f51881b 100644 --- a/tests/Guard/ControllerGuardTest.php +++ b/tests/Guard/ControllerGuardTest.php @@ -25,7 +25,7 @@ use LmcRbacMvc\Guard\GuardInterface; use LmcRbacMvc\Role\InMemoryRoleProvider; use LmcRbacMvc\Service\RoleService; -use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; +use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; /** * @covers \LmcRbacMvc\Guard\AbstractGuard diff --git a/tests/Guard/ControllerPermissionsGuardTest.php b/tests/Guard/ControllerPermissionsGuardTest.php index 07a5c6e4..57550a9c 100644 --- a/tests/Guard/ControllerPermissionsGuardTest.php +++ b/tests/Guard/ControllerPermissionsGuardTest.php @@ -26,7 +26,7 @@ use LmcRbacMvc\Guard\GuardInterface; use LmcRbacMvc\Role\InMemoryRoleProvider; use LmcRbacMvc\Service\RoleService; -use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; +use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; /** * @covers \LmcRbacMvc\Guard\AbstractGuard diff --git a/tests/Guard/RouteGuardTest.php b/tests/Guard/RouteGuardTest.php index 9e4cc30a..56af7b08 100644 --- a/tests/Guard/RouteGuardTest.php +++ b/tests/Guard/RouteGuardTest.php @@ -27,7 +27,7 @@ use LmcRbacMvc\Guard\RoutePermissionsGuard; use LmcRbacMvc\Role\InMemoryRoleProvider; use LmcRbacMvc\Service\RoleService; -use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; +use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; /** * @covers \LmcRbacMvc\Guard\AbstractGuard diff --git a/tests/Identity/AuthenticationIdentityProviderTest.php b/tests/Identity/AuthenticationIdentityProviderTest.php index 214b16bb..5963a967 100644 --- a/tests/Identity/AuthenticationIdentityProviderTest.php +++ b/tests/Identity/AuthenticationIdentityProviderTest.php @@ -31,7 +31,7 @@ class AuthenticationIdentityProviderTest extends \PHPUnit\Framework\TestCase protected $identityProvider; /** - * @var \Laminas\Authentication\AuthenticationService|\PHPUnit_Framework_MockObject_MockObject + * @var \Laminas\Authentication\AuthenticationService|\PHPUnit\Framework\MockObject\MockObject */ protected $authenticationService; diff --git a/tests/Role/InMemoryRoleProviderTest.php b/tests/Role/InMemoryRoleProviderTest.php index e5d7d35c..71cc56c3 100644 --- a/tests/Role/InMemoryRoleProviderTest.php +++ b/tests/Role/InMemoryRoleProviderTest.php @@ -19,7 +19,6 @@ namespace LmcRbacMvcTest\Role; use LmcRbacMvc\Role\InMemoryRoleProvider; - /** * @covers \LmcRbacMvc\Role\InMemoryRoleProvider */ @@ -48,35 +47,33 @@ public function testInMemoryProvider() // Test admin role $adminRole = $roles[0]; - $this->assertInstanceOf('Rbac\Role\HierarchicalRoleInterface', $adminRole); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $adminRole); $this->assertEquals('admin', $adminRole->getName()); $this->assertTrue($adminRole->hasPermission('delete')); // Test member role $memberRole = $roles[1]; - $this->assertInstanceOf('Rbac\Role\HierarchicalRoleInterface', $memberRole); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $memberRole); $this->assertEquals('member', $memberRole->getName()); $this->assertTrue($memberRole->hasPermission('write')); $this->assertFalse($memberRole->hasPermission('delete')); // Test guest role $guestRole = $roles[2]; - $this->assertInstanceOf('Rbac\Role\RoleInterface', $guestRole); - $this->assertNotInstanceOf('Rbac\Role\HierarchicalRoleInterface', $guestRole); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $guestRole); $this->assertEquals('guest', $guestRole->getName()); $this->assertFalse($guestRole->hasPermission('write')); $this->assertFalse($guestRole->hasPermission('delete')); // Test system role $systemRole = $roles[3]; - $this->assertInstanceOf('Rbac\Role\RoleInterface', $systemRole); - $this->assertNotInstanceOf('Rbac\Role\HierarchicalRoleInterface', $systemRole); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $systemRole); $this->assertEquals('system', $systemRole->getName()); $this->assertTrue($systemRole->hasPermission('all')); $this->assertFalse($systemRole->hasPermission('write')); $this->assertFalse($systemRole->hasPermission('delete')); - $this->assertSame($adminRole->getChildren()['member'], $memberRole); - $this->assertSame($memberRole->getChildren()['guest'], $guestRole); + $this->assertSame($adminRole->getChildren()[0], $memberRole); + $this->assertSame($memberRole->getChildren()[0], $guestRole); } } diff --git a/tests/Role/ObjectRepositoryRoleProviderTest.php b/tests/Role/ObjectRepositoryRoleProviderTest.php index cc1bdc3c..77e8b25a 100644 --- a/tests/Role/ObjectRepositoryRoleProviderTest.php +++ b/tests/Role/ObjectRepositoryRoleProviderTest.php @@ -21,7 +21,7 @@ use Doctrine\ORM\Tools\SchemaTool; use Doctrine\ORM\Tools\ToolsException; use Doctrine\Persistence\ObjectManager; -use Rbac\Traversal\RecursiveRoleIterator; +use LmcRbacMvc\Role\RecursiveRoleIterator; use Laminas\ServiceManager\ServiceManager; use LmcRbacMvc\Role\ObjectRepositoryRoleProvider; use LmcRbacMvcTest\Asset\FlatRole; @@ -62,7 +62,7 @@ public function testObjectRepositoryProviderForFlatRole() $this->assertCount(1, $roles); $this->assertIsArray($roles); - $this->assertInstanceOf('Rbac\Role\RoleInterface', $roles[0]); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $roles[0]); $this->assertEquals('admin', $roles[0]->getName()); } @@ -95,7 +95,7 @@ public function testObjectRepositoryProviderForHierarchicalRole() $this->assertCount(1, $roles); $this->assertIsArray($roles); - $this->assertInstanceOf('Rbac\Role\HierarchicalRoleInterface', $roles[0]); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $roles[0]); $this->assertEquals('admin', $roles[0]->getName()); $iteratorIterator = new \RecursiveIteratorIterator( @@ -106,7 +106,7 @@ public function testObjectRepositoryProviderForHierarchicalRole() $childRolesString = ''; foreach ($iteratorIterator as $childRole) { - $this->assertInstanceOf('Rbac\Role\HierarchicalRoleInterface', $childRole); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $childRole); $childRolesString .= $childRole->getName(); } diff --git a/tests/Service/AuthorizationServiceTest.php b/tests/Service/AuthorizationServiceTest.php index efdbf41b..a7871ae0 100644 --- a/tests/Service/AuthorizationServiceTest.php +++ b/tests/Service/AuthorizationServiceTest.php @@ -18,8 +18,8 @@ namespace LmcRbacMvcTest\Service; -use Rbac\Rbac; -use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; +use LmcRbacMvc\Rbac\Rbac; +use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; use Laminas\ServiceManager\ServiceManager; use LmcRbacMvc\Role\InMemoryRoleProvider; use LmcRbacMvc\Service\AuthorizationService; @@ -124,7 +124,7 @@ public function testGranted($role, $permission, $context, $isGranted, $assertion ] ]; - $identity = $this->createMock('LmcRbacMvc\Identity\IdentityInterface'); + $identity = $this->createMock(\LmcRbacMvc\Identity\IdentityInterface::class); $identity->expects($this->once())->method('getRoles')->will($this->returnValue((array) $role)); $identityProvider = $this->createMock('LmcRbacMvc\Identity\IdentityProviderInterface'); @@ -136,8 +136,9 @@ public function testGranted($role, $permission, $context, $isGranted, $assertion $roleService = new RoleService( $identityProvider, new InMemoryRoleProvider($roleConfig), - $rbac->getTraversalStrategy() + new RecursiveRoleIteratorStrategy() ); + $assertionPluginManager = new AssertionPluginManager(new ServiceManager(), $assertionPluginConfig); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); @@ -148,8 +149,8 @@ public function testGranted($role, $permission, $context, $isGranted, $assertion public function testDoNotCallAssertionIfThePermissionIsNotGranted() { - $role = $this->createMock('Rbac\Role\RoleInterface'); - $rbac = $this->createMock('Rbac\Rbac'); + $role = $this->createMock('Laminas\Permissions\Rbac\RoleInterface'); + $rbac = $this->createMock('LmcRbacMvc\Rbac\Rbac'); $roleService = $this->createMock('LmcRbacMvc\Service\RoleService'); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue([$role])); @@ -164,8 +165,8 @@ public function testDoNotCallAssertionIfThePermissionIsNotGranted() public function testThrowExceptionForInvalidAssertion() { - $role = $this->createMock('Rbac\Role\RoleInterface'); - $rbac = $this->createMock('Rbac\Rbac'); + $role = $this->createMock('Laminas\Permissions\Rbac\RoleInterface'); + $rbac = $this->createMock('LmcRbacMvc\Rbac\Rbac'); $rbac->expects($this->once())->method('isGranted')->will($this->returnValue(true)); @@ -183,8 +184,8 @@ public function testThrowExceptionForInvalidAssertion() public function testDynamicAssertions() { - $role = $this->createMock('Rbac\Role\RoleInterface'); - $rbac = $this->createMock('Rbac\Rbac'); + $role = $this->createMock('Laminas\Permissions\Rbac\RoleInterface'); + $rbac = $this->createMock('LmcRbacMvc\Rbac\Rbac'); $rbac->expects($this->exactly(2))->method('isGranted')->will($this->returnValue(true)); @@ -221,7 +222,7 @@ function (AuthorizationService $injectedService) use ($authorizationService, &$c public function testAssertionMap() { - $rbac = $this->createMock('Rbac\Rbac'); + $rbac = $this->createMock('LmcRbacMvc\Rbac\Rbac'); $roleService = $this->createMock('LmcRbacMvc\Service\RoleService'); $assertionPluginManager = $this->createMock('LmcRbacMvc\Assertion\AssertionPluginManager'); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); @@ -241,7 +242,7 @@ public function testAssertionMap() */ public function testGetIdentity() { - $rbac = $this->createMock('Rbac\Rbac'); + $rbac = $this->createMock('LmcRbacMvc\Rbac\Rbac'); $identity = $this->createMock('LmcRbacMvc\Identity\IdentityInterface'); $roleService = $this->createMock('LmcRbacMvc\Service\RoleService'); $assertionManager = $this->createMock('LmcRbacMvc\Assertion\AssertionPluginManager'); diff --git a/tests/Service/RoleServiceTest.php b/tests/Service/RoleServiceTest.php index cf46f89f..a99b31ce 100644 --- a/tests/Service/RoleServiceTest.php +++ b/tests/Service/RoleServiceTest.php @@ -24,9 +24,9 @@ use LmcRbacMvc\Role\RoleProviderInterface; use LmcRbacMvc\Service\RoleService; use PHPUnit\Framework\TestCase; -use Rbac\Role\RoleInterface; -use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; -use Rbac\Traversal\Strategy\TraversalStrategyInterface; +use Laminas\Permissions\Rbac\RoleInterface; +use LmcRbacMvc\Role\RecursiveRoleIteratorStrategy; +use LmcRbacMvc\Role\TraversalStrategyInterface; /** * @covers \LmcRbacMvc\Service\RoleService @@ -211,7 +211,7 @@ public function testReturnGuestRoleIfNoIdentityIsFound() $roleService = new RoleService( $identityProvider, new InMemoryRoleProvider([]), - $this->createMock('Rbac\Traversal\Strategy\TraversalStrategyInterface') + $this->createMock('LmcRbacMvc\Role\TraversalStrategyInterface') ); $roleService->setGuestRole('guest'); @@ -220,7 +220,7 @@ public function testReturnGuestRoleIfNoIdentityIsFound() $this->assertEquals('guest', $roleService->getGuestRole()); $this->assertCount(1, $result); - $this->assertInstanceOf('Rbac\Role\RoleInterface', $result[0]); + $this->assertInstanceOf('Laminas\Permissions\Rbac\RoleInterface', $result[0]); $this->assertEquals('guest', $result[0]->getName()); } @@ -303,7 +303,7 @@ public function testThrowExceptionIfIdentityIsWrongType() $roleService = new RoleService( $identityProvider, $this->createMock('LmcRbacMvc\Role\RoleProviderInterface'), - $this->createMock('Rbac\Traversal\Strategy\TraversalStrategyInterface') + $this->createMock('LmcRbacMvc\Role\TraversalStrategyInterface') ); $roleService->getIdentityRoles();