Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RoleServiceFactory creates Role Provider from configuration options #46

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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