From da6e600f741b76141717ce01f2bbebfe598c5205 Mon Sep 17 00:00:00 2001 From: "Eric Richer eric.richer@vistoconsulting.com" Date: Tue, 20 Aug 2024 15:06:58 -0400 Subject: [PATCH] Added assertion_manager config to Module options Signed-off-by: Eric Richer eric.richer@vistoconsulting.com --- config/lmcrbac.global.php.dist | 4 +-- .../AssertionPluginManagerFactory.php | 6 +++-- src/Options/ModuleOptions.php | 25 +++++++++++++++++++ src/Options/ModuleOptionsFactory.php | 9 ++++++- .../AssertionPluginManagerFactoryTest.php | 14 ++++++----- test/Options/ModuleOptionsFactoryTest.php | 18 +++++++++++++ test/Options/ModuleOptionsTest.php | 5 ++++ 7 files changed, 70 insertions(+), 11 deletions(-) diff --git a/config/lmcrbac.global.php.dist b/config/lmcrbac.global.php.dist index c9fd207..e873f22 100644 --- a/config/lmcrbac.global.php.dist +++ b/config/lmcrbac.global.php.dist @@ -77,7 +77,7 @@ return [ * 'AssertOwnerCanEditArticle' => \My\Namespace\AssertOwnerCanEditArticleFactory::class * ], */ - 'assertion_map' => [ - ], + // 'assertion_map' => [], + // 'assertion_manager' => [], ], ]; diff --git a/src/Assertion/AssertionPluginManagerFactory.php b/src/Assertion/AssertionPluginManagerFactory.php index 7c275a0..40cd52b 100644 --- a/src/Assertion/AssertionPluginManagerFactory.php +++ b/src/Assertion/AssertionPluginManagerFactory.php @@ -3,6 +3,7 @@ namespace Lmc\Rbac\Assertion; use Laminas\ServiceManager\Factory\FactoryInterface; +use Lmc\Rbac\Options\ModuleOptions; use Psr\Container\ContainerInterface; class AssertionPluginManagerFactory implements FactoryInterface @@ -13,8 +14,9 @@ class AssertionPluginManagerFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): AssertionPluginManager { - $config = $container->get('config')['lmc_rbac']['assertion_manager']; + /** @var ModuleOptions $config */ + $config = $container->get(ModuleOptions::class); - return new AssertionPluginManager($container, $config); + return new AssertionPluginManager($container, $config->getAssertionManager()); } } diff --git a/src/Options/ModuleOptions.php b/src/Options/ModuleOptions.php index 18a18ba..b494620 100644 --- a/src/Options/ModuleOptions.php +++ b/src/Options/ModuleOptions.php @@ -52,6 +52,13 @@ class ModuleOptions extends AbstractOptions InMemoryRoleProvider::class => [], ]; + /** + * Assertion plugin manager configuration + * + * @var array + */ + protected array $assertionManager = []; + /** * Constructor * @@ -114,4 +121,22 @@ public function getRoleProvider(): array { return $this->roleProvider; } + + /** + * Set the configuration for the assertion plugin manager + * + * @param array $assertionManager + */ + public function setAssertionManager(array $assertionManager): void + { + $this->assertionManager = $assertionManager; + } + + /** + * @return array + */ + public function getAssertionManager(): array + { + return $this->assertionManager; + } } diff --git a/src/Options/ModuleOptionsFactory.php b/src/Options/ModuleOptionsFactory.php index e44d456..86bdf3c 100644 --- a/src/Options/ModuleOptionsFactory.php +++ b/src/Options/ModuleOptionsFactory.php @@ -21,6 +21,7 @@ namespace Lmc\Rbac\Options; +use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Psr\Container\ContainerInterface; /** @@ -30,6 +31,12 @@ class ModuleOptionsFactory { public function __invoke(ContainerInterface $container): ModuleOptions { - return new ModuleOptions($container->get('config')['lmc_rbac']); + $config = $container->get('config'); + + if (!isset($config['lmc_rbac']) || !is_array($config['lmc_rbac'])) { + throw new ServiceNotCreatedException('No lmc_rbac config found.'); + } + + return new ModuleOptions($config['lmc_rbac']); } } diff --git a/test/Assertion/AssertionPluginManagerFactoryTest.php b/test/Assertion/AssertionPluginManagerFactoryTest.php index 9e48b8f..565af7b 100644 --- a/test/Assertion/AssertionPluginManagerFactoryTest.php +++ b/test/Assertion/AssertionPluginManagerFactoryTest.php @@ -24,21 +24,23 @@ use Laminas\ServiceManager\ServiceManager; use Lmc\Rbac\Assertion\AssertionPluginManager; use Lmc\Rbac\Assertion\AssertionPluginManagerFactory; +use Lmc\Rbac\Options\ModuleOptions; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerInterface; #[CoversClass(AssertionPluginManagerFactory::class)] class AssertionPluginManagerFactoryTest extends TestCase { public function testFactory(): void { - $serviceManager = new ServiceManager(); - $serviceManager->setService('config', [ - 'lmc_rbac' => [ + $moduleOptions = new ModuleOptions( + [ 'assertion_manager' => [], - ], - ]); - + ] + ); + $serviceManager = new ServiceManager(); + $serviceManager->setService(ModuleOptions::class, $moduleOptions); $factory = new AssertionPluginManagerFactory(); $pluginManager = $factory($serviceManager, AssertionPluginManager::class); diff --git a/test/Options/ModuleOptionsFactoryTest.php b/test/Options/ModuleOptionsFactoryTest.php index 6da1f25..6b5fb51 100644 --- a/test/Options/ModuleOptionsFactoryTest.php +++ b/test/Options/ModuleOptionsFactoryTest.php @@ -21,6 +21,7 @@ namespace LmcTest\Rbac\Options; +use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Laminas\ServiceManager\ServiceManager; use Lmc\Rbac\Options\ModuleOptions; use Lmc\Rbac\Options\ModuleOptionsFactory; @@ -42,4 +43,21 @@ public function testFactory(): void $this->assertInstanceOf(ModuleOptions::class, $options); } + public function testNoConfig(): void + { + $factory = new ModuleOptionsFactory(); + $serviceManager = new ServiceManager(); + + $config = []; + $serviceManager->setService('config', $config); + + $this->expectException(ServiceNotCreatedException::class); + $factory($serviceManager); + + $config['lmc_rbac'] = 'foo'; + $serviceManager->setService('config', $config); + + $this->expectException(ServiceNotCreatedException::class); + $factory($serviceManager); + } } diff --git a/test/Options/ModuleOptionsTest.php b/test/Options/ModuleOptionsTest.php index 4f5fb3d..1fff349 100644 --- a/test/Options/ModuleOptionsTest.php +++ b/test/Options/ModuleOptionsTest.php @@ -39,6 +39,7 @@ public function testAssertModuleDefaultOptions(): void $this->assertIsArray($moduleOptions->getRoleProvider()); $this->assertIsArray($moduleOptions->getAssertionMap()); $this->assertEquals(InMemoryRoleProvider::class, key($moduleOptions->getRoleProvider())); + $this->assertIsArray($moduleOptions->getAssertionManager()); } public function testSettersAndGetters(): void @@ -50,10 +51,14 @@ public function testSettersAndGetters(): void 'foo' => 'bar', ], 'identity_provider' => 'foo', + 'assertion_manager' => [ + 'factories' => [], + ], ]); $this->assertEquals('unknown', $moduleOptions->getGuestRole()); $this->assertEquals([], $moduleOptions->getRoleProvider()); $this->assertEquals(['foo' => 'bar'], $moduleOptions->getAssertionMap()); + $this->assertEquals(['factories' => []], $moduleOptions->getAssertionManager()); } }