Skip to content

Commit

Permalink
Merge pull request #18 from visto9259/master
Browse files Browse the repository at this point in the history
Update to require doctrine peristence v2
  • Loading branch information
visto9259 authored Feb 6, 2021
2 parents df61ac2 + 6411204 commit f06a7e2
Show file tree
Hide file tree
Showing 14 changed files with 2,538 additions and 1,663 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"laminas/laminas-eventmanager": "^3.0",
"laminas/laminas-mvc": "^3.0",
"laminas/laminas-servicemanager": "^3.0.3",
"zfr/rbac": "~1.2"
"zfr/rbac": "~1.2",
"doctrine/persistence": "^2.1"
},
"require-dev": {
"laminas/laminas-authentication": "~2.2",
Expand All @@ -41,11 +42,11 @@
"laminas/laminas-http": "~2.2",
"laminas/laminas-i18n": "~2.7.3",
"laminas/laminas-serializer": "~2.2",
"laminas/laminas-view": "~2.2",
"phpunit/phpunit": "8.5.8",
"laminas/laminas-view": "~2.11.3",
"phpunit/phpunit": "9.5.2",
"squizlabs/php_codesniffer": "3.5.5",
"php-coveralls/php-coveralls": "^2.2",
"doctrine/doctrine-orm-module": "^3.0"
"doctrine/doctrine-orm-module": "^3.1"
},
"suggest": {
"laminas/laminas-developer-tools": "if you want to show information about the roles",
Expand Down
3,955 changes: 2,311 additions & 1,644 deletions composer.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/07. Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class Module
```

Now that we have this in place let us quickly define our `PostService`. We will be using a Service that makes use
of Doctrine, so we require a `Doctrine\Common\Persistence\ObjectManager` as dependency.
of Doctrine, so we require a `Doctrine\Persistence\ObjectManager` as dependency.

```php
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

class PostService
{
Expand Down Expand Up @@ -159,7 +159,7 @@ The solution is to inject the `AuthorizationService` into your services and chec
permissions before doing anything wrong. So let's modify our previously created `PostService` class

```php
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;
use LmcRbacMvc\Service\AuthorizationService;

class PostService
Expand Down Expand Up @@ -292,7 +292,7 @@ previous example. In here we will cover a very common use case. Users of our App
permissions to their own content. So let's quickly refresh our `PostService` class:

```php
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

class PostService
{
Expand Down Expand Up @@ -401,7 +401,7 @@ context. This is done by simply passing it to the `isGranted()` method. For this
one last time.

```php
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

class PostService
{
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/ObjectRepositoryRoleProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

namespace LmcRbacMvc\Factory;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Role/ObjectRepositoryRoleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace LmcRbacMvc\Role;

use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\Persistence\ObjectRepository;
use LmcRbacMvc\Exception\RoleNotFoundException;

/**
Expand Down
1 change: 1 addition & 0 deletions src/View/Strategy/UnauthorizedStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function onError(MvcEvent $event)
break;

default:
break;
}

$response = $event->getResponse() ?: new HttpResponse();
Expand Down
25 changes: 25 additions & 0 deletions tests/Asset/MockRoleWithPermissionTraversable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace LmcRbacMvcTest\Asset;

use Rbac\Role\RoleInterface;


class MockRoleWithPermissionTraversable implements RoleInterface
{
public function getPermissions()
{
return new \ArrayObject(['permission-method-a', 'permission-method-b']);
}

public function getName()
{
return 'role-with-permission-traversable';
}
public function hasPermission($permission)
{
return false;
}

}
23 changes: 23 additions & 0 deletions tests/Collector/RbacCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace LmcRbacMvcTest\Collector;

use LmcRbacMvc\Identity\IdentityInterface;
use LmcRbacMvcTest\Asset\MockRoleWithPermissionTraversable;
use Rbac\Role\RoleInterface;
use Laminas\Mvc\MvcEvent;
use Laminas\Permissions\Rbac\Role;
Expand Down Expand Up @@ -241,6 +242,28 @@ public function testCollectPermissionsMethod()
$this->assertEquals($expectedCollection, $collection);
}

/**
* Tests the collectPermissions method when the role implements Traversable
*/
public function testCollectPermissionsTraversable()
{
$expectedCollection = [
'guards' => [],
'roles' => ['role-with-permission-traversable'],
'permissions' => [
'role-with-permission-traversable' => ['permission-method-a', 'permission-method-b'],
],
'options' => [
'guest_role' => 'guest',
'protection_policy' => GuardInterface::POLICY_ALLOW,
],
];

$collection = $this->collectPermissionsPropertyTestBase(new MockRoleWithPermissionTraversable());
$this->assertEquals($expectedCollection, $collection);
}


/**
* Base method for the *collectPermissionProperty tests
* @param RoleInterface $role
Expand Down
15 changes: 15 additions & 0 deletions tests/Factory/AssertionPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ public function testFactory()

$this->assertInstanceOf('LmcRbacMvc\Assertion\AssertionPluginManager', $pluginManager);
}

public function testFactoryInvokable()
{
$serviceManager = new ServiceManager();
$serviceManager->setService('Config', [
'lmc_rbac' => [
'assertion_manager' => []
]
]);

$factory = new AssertionPluginManagerFactory();
$pluginManager = $factory($serviceManager, 'notused');

$this->assertInstanceOf('LmcRbacMvc\Assertion\AssertionPluginManager', $pluginManager);
}
}
6 changes: 3 additions & 3 deletions tests/Factory/ObjectRepositoryRoleProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testFactoryUsingObjectRepository()
'object_repository' => 'RoleObjectRepository'
];

$serviceManager->setService('RoleObjectRepository', $this->createMock('Doctrine\Common\Persistence\ObjectRepository'));
$serviceManager->setService('RoleObjectRepository', $this->createMock('Doctrine\Persistence\ObjectRepository'));

$roleProvider = $pluginManager->get('LmcRbacMvc\Role\ObjectRepositoryRoleProvider', $options);
$this->assertInstanceOf('LmcRbacMvc\Role\ObjectRepositoryRoleProvider', $roleProvider);
Expand All @@ -55,11 +55,11 @@ public function testFactoryUsingObjectManager()
'class_name' => 'Role'
];

$objectManager = $this->createMock('Doctrine\Common\Persistence\ObjectManager');
$objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
$objectManager->expects($this->once())
->method('getRepository')
->with($options['class_name'])
->will($this->returnValue($this->createMock('Doctrine\Common\Persistence\ObjectRepository')));
->will($this->returnValue($this->createMock('Doctrine\Persistence\ObjectRepository')));

$serviceManager->setService('ObjectManager', $objectManager);

Expand Down
16 changes: 14 additions & 2 deletions tests/Role/InMemoryRoleProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class InMemoryRoleProviderTest extends \PHPUnit\Framework\TestCase
public function testInMemoryProvider()
{
$inMemoryProvider = new InMemoryRoleProvider([
'system' => [
'permissions' => ['all'],
],
'admin' => [
'children' => ['member'],
'permissions' => ['delete']
Expand All @@ -39,9 +42,9 @@ public function testInMemoryProvider()
'guest'
]);

$roles = $inMemoryProvider->getRoles(['admin', 'member', 'guest']);
$roles = $inMemoryProvider->getRoles(['admin', 'member', 'guest', 'system']);

$this->assertCount(3, $roles);
$this->assertCount(4, $roles);

// Test admin role
$adminRole = $roles[0];
Expand All @@ -64,6 +67,15 @@ public function testInMemoryProvider()
$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->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);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Role/ObjectRepositoryRoleProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
namespace LmcRbacMvcTest\Role;

use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\ToolsException;
use Doctrine\Persistence\ObjectManager;
use Rbac\Traversal\RecursiveRoleIterator;
use Laminas\ServiceManager\ServiceManager;
use LmcRbacMvc\Role\ObjectRepositoryRoleProvider;
Expand Down Expand Up @@ -155,7 +157,8 @@ public function testThrowExceptionIfAskedRoleIsNotFound()
}

/**
* @return \Doctrine\Common\Persistence\ObjectManager
* @return ObjectManager
* @throws ToolsException
*/
private function getObjectManager()
{
Expand Down
74 changes: 73 additions & 1 deletion tests/Service/RoleServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@

namespace LmcRbacMvcTest\Service;

use LmcRbacMvc\Identity\IdentityInterface;
use LmcRbacMvc\Identity\IdentityProviderInterface;
use LmcRbacMvc\Role\InMemoryRoleProvider;
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;

/**
* @covers \LmcRbacMvc\Service\RoleService
*/
class RoleServiceTest extends \PHPUnit\Framework\TestCase
class RoleServiceTest extends TestCase
{
public function roleProvider()
{
Expand Down Expand Up @@ -218,6 +224,72 @@ public function testReturnGuestRoleIfNoIdentityIsFound()
$this->assertEquals('guest', $result[0]->getName());
}

public function testSetIdentityProvider()
{
$identityProvider = $this->createMock(IdentityProviderInterface::class);
$identityProvider->expects($this->any())
->method('getIdentity')
->will($this->returnValue('test'));
$roleService = new RoleService(
$this->createMock(IdentityProviderInterface::class),
new InMemoryRoleProvider([]),
$this->createMock(TraversalStrategyInterface::class)
);
$roleService->setIdentityProvider($identityProvider);
$this->assertEquals('test', $roleService->getIdentity());
}

public function testSetRoleProvider()
{
$role = $this->createMock(RoleInterface::class);
$identity = $this->createMock(IdentityInterface::class);
$identity->expects($this->once())->method('getRoles')->will($this->returnValue(new \ArrayObject([$role])));

$identityProvider = $this->createMock(IdentityProviderInterface::class);
$identityProvider->expects($this->any())
->method('getIdentity')
->will($this->returnValue($identity));
$roleProvider = new InMemoryRoleProvider([
'member' => [
'children' => ['guest'],
],
'guest'
]);
$roleService = new RoleService(
$identityProvider,
$roleProvider,
new RecursiveRoleIteratorStrategy()
);
$roleService->setRoleProvider($roleProvider);
$roles = $roleService->getIdentityRoles();
$this->assertEquals($role, $roles[0]);
}

public function testConvertRolesTraversable()
{
$identity = $this->createMock(IdentityInterface::class);
$identity->expects($this->once())->method('getRoles')->will($this->returnValue(['guest']));

$identityProvider = $this->createMock(IdentityProviderInterface::class);
$identityProvider->expects($this->any())
->method('getIdentity')
->will($this->returnValue($identity));
$roleService = new RoleService(
$identityProvider,
$this->createMock(RoleProviderInterface::class),
new RecursiveRoleIteratorStrategy()
);
$roleProvider = new InMemoryRoleProvider([
'member' => [
'children' => ['guest']
],
'guest'
]);
$roleService->setRoleProvider($roleProvider);
$this->assertEquals(false, $roleService->matchIdentityRoles(['member']));
}


public function testThrowExceptionIfIdentityIsWrongType()
{
$this->expectException('LmcRbacMvc\Exception\RuntimeException');
Expand Down
Loading

0 comments on commit f06a7e2

Please sign in to comment.