Skip to content

Commit

Permalink
Merge pull request #46 from visto9259/fix-issue-45
Browse files Browse the repository at this point in the history
RoleServiceFactory creates Role Provider from configuration options
  • Loading branch information
visto9259 authored May 23, 2024
2 parents f2e392b + f7157fe commit cff8aa6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
22 changes: 12 additions & 10 deletions config/config.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@
/**
* Configuration for role provider
*
* It must be an array that contains configuration for the role provider. The provider config
* must follow the following format:
*
* 'LmcRbac\Role\InMemoryRoleProvider' => [
* 'role1' => [
* 'children' => ['children1', 'children2'], // OPTIONAL
* 'permissions' => ['edit', 'read'] // OPTIONAL
* ]
* ]
* It must be an array that contains configuration for the role provider.
* The default Role Provider is 'LmcRbac\Role\InMemoryRoleProvider'
*
* Supported options depend of the role provider, so please refer to the official documentation
*
* The provider config for InMemoryRoleProvider must follow the following format:
*
* 'LmcRbac\Role\InMemoryRoleProvider' => [
* 'role1' => [
* 'children' => ['children1', 'children2'], // OPTIONAL
* 'permissions' => ['edit', 'read'] // OPTIONAL
* ]
* ]
*/
'role_provider' => [],
// 'role_provider' => [],

/**
* Defining the assertion map
Expand Down
11 changes: 10 additions & 1 deletion src/Container/RoleServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace LmcRbac\Container;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use LmcRbac\Options\ModuleOptions;
use LmcRbac\Role\RoleProviderInterface;
use LmcRbac\Service\RoleService;
Expand All @@ -38,6 +39,14 @@ public function __invoke(ContainerInterface $container): RoleService
{
$moduleOptions = $container->get(ModuleOptions::class);

return new RoleService($container->get(RoleProviderInterface::class), $moduleOptions->getGuestRole());
// Get the role provider from the options
$roleProvider = $moduleOptions->getRoleProvider();
if (empty($roleProvider)) {
throw new ServiceNotCreatedException('No role provider defined in LmcRbac configuration.');
}

$roleProviderName = key($roleProvider);

return new RoleService($container->get($roleProviderName), $moduleOptions->getGuestRole());
}
}
5 changes: 4 additions & 1 deletion src/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ final class ModuleOptions extends AbstractOptions

/**
* A configuration for role provider
* Defaults to InMemoryRoleProvider
*
* @var array
*/
protected $roleProvider = [];
protected $roleProvider = [
'LmcRbac\Role\InMemoryRoleProvider' => [],
];

/**
* Constructor
Expand Down
7 changes: 5 additions & 2 deletions test/Container/RoleServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace LmcRbacTest\Container;

use Laminas\ServiceManager\ServiceManager;
use LmcRbac\Container\InMemoryRoleProviderFactory;
use LmcRbac\Container\RoleServiceFactory;
use LmcRbac\Options\ModuleOptions;
use LmcRbac\Role\InMemoryRoleProvider;
Expand All @@ -44,9 +45,11 @@ public function testCanCreateRoleService(): void
],
]);



$container = new ServiceManager(['services' => [
ModuleOptions::class => $options,
RoleProviderInterface::class => new InMemoryRoleProvider([]),
InMemoryRoleProvider::class => new InMemoryRoleProvider([]),
]]);

$factory = new RoleServiceFactory();
Expand All @@ -57,7 +60,7 @@ public function testCanCreateRoleService(): void

public function testThrowExceptionIfNoRoleProvider(): void
{
$this->expectException(\Psr\Container\NotFoundExceptionInterface::class);
$this->expectException(\Laminas\ServiceManager\Exception\ServiceNotCreatedException::class);

$options = new ModuleOptions([
'guest_role' => 'guest',
Expand Down
2 changes: 1 addition & 1 deletion test/Options/ModuleOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class ModuleOptionsTest extends TestCase
{
public function testAssertModuleDefaultOptions(): void
{
/** @var \LmcRbac\Options\ModuleOptions $moduleOptions */
$moduleOptions = new \LmcRbac\Options\ModuleOptions();

$this->assertEquals('guest', $moduleOptions->getGuestRole());
$this->assertIsArray($moduleOptions->getRoleProvider());
$this->assertIsArray($moduleOptions->getAssertionMap());
$this->assertEquals('LmcRbac\Role\InMemoryRoleProvider', key($moduleOptions->getRoleProvider()));
}

public function testSettersAndGetters(): void
Expand Down

0 comments on commit cff8aa6

Please sign in to comment.