diff --git a/composer.json b/composer.json index ec1c03b5..31f28bca 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ } ], "require": { - "php": "^7.3 || ^8.0 || ^8.1 || ^8.2 || ^8.3", + "php": "^8.1 || ^8.2 || ^8.3", "laminas/laminas-servicemanager": "^3.3", "laminas/laminas-stdlib": "^3.1", "doctrine/persistence": "^2.0 || ^3.0" diff --git a/src/Assertion/AssertionContainerFactory.php b/src/Assertion/AssertionContainerFactory.php new file mode 100644 index 00000000..a4e7cd68 --- /dev/null +++ b/src/Assertion/AssertionContainerFactory.php @@ -0,0 +1,41 @@ +get('config')['lmc_rbac']['assertion_manager']; + + return new AssertionContainer($container, $config); + } +} diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index a8d2a61c..5ac52289 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -40,12 +40,12 @@ public function getDependencyConfig(): array { return [ 'factories' => [ - \LmcRbac\Assertion\AssertionContainerInterface::class => \LmcRbac\Container\AssertionContainerFactory::class, - \LmcRbac\Options\ModuleOptions::class => \LmcRbac\Container\ModuleOptionsFactory::class, - \LmcRbac\Role\InMemoryRoleProvider::class => \LmcRbac\Container\InMemoryRoleProviderFactory::class, - \LmcRbac\Role\ObjectRepositoryRoleProvider::class => \LmcRbac\Container\ObjectRepositoryRoleProviderFactory::class, - \LmcRbac\Service\AuthorizationServiceInterface::class => \LmcRbac\Container\AuthorizationServiceFactory::class, - \LmcRbac\Service\RoleServiceInterface::class => \LmcRbac\Container\RoleServiceFactory::class, + \LmcRbac\Assertion\AssertionContainerInterface::class => \LmcRbac\Assertion\AssertionContainerFactory::class, + \LmcRbac\Options\ModuleOptions::class => \LmcRbac\Options\ModuleOptionsFactory::class, + \LmcRbac\Role\InMemoryRoleProvider::class => \LmcRbac\Role\InMemoryRoleProviderFactory::class, + \LmcRbac\Role\ObjectRepositoryRoleProvider::class => \LmcRbac\Role\ObjectRepositoryRoleProviderFactory::class, + \LmcRbac\Service\AuthorizationServiceInterface::class => \LmcRbac\Service\AuthorizationServiceFactory::class, + \LmcRbac\Service\RoleServiceInterface::class => \LmcRbac\Service\RoleServiceFactory::class, \LmcRbac\Rbac::class => \Laminas\ServiceManager\Factory\InvokableFactory::class, ], ]; diff --git a/src/Container/AssertionContainerFactory.php b/src/Container/AssertionContainerFactory.php index 54e41612..aff0db2f 100644 --- a/src/Container/AssertionContainerFactory.php +++ b/src/Container/AssertionContainerFactory.php @@ -29,6 +29,7 @@ * * @author Aeneas Rekkas * @licence MIT + * @deprecated Replaced by LmcRbac\Assertion\AssertionContainerFactory */ final class AssertionContainerFactory { diff --git a/src/Container/AuthorizationServiceFactory.php b/src/Container/AuthorizationServiceFactory.php index 741c3455..9428ee74 100644 --- a/src/Container/AuthorizationServiceFactory.php +++ b/src/Container/AuthorizationServiceFactory.php @@ -33,6 +33,7 @@ * * @author Michaël Gallego * @licence MIT + * @deprecated Replaced by LmcRbac\Service\AuthorizationServiceFactory */ final class AuthorizationServiceFactory { diff --git a/src/Container/InMemoryRoleProviderFactory.php b/src/Container/InMemoryRoleProviderFactory.php index 7fd7180b..420c8fb3 100644 --- a/src/Container/InMemoryRoleProviderFactory.php +++ b/src/Container/InMemoryRoleProviderFactory.php @@ -30,6 +30,7 @@ * * @author Vytautas Stankus * @licence MIT + * @deprecated Replaced by LmcRbac\Role\InMemoryRoleProviderFactory */ final class InMemoryRoleProviderFactory { diff --git a/src/Container/ModuleOptionsFactory.php b/src/Container/ModuleOptionsFactory.php index 2d566c3a..c2fbff48 100644 --- a/src/Container/ModuleOptionsFactory.php +++ b/src/Container/ModuleOptionsFactory.php @@ -29,6 +29,7 @@ * * @author Michaël Gallego * @licence MIT + * @deprecated Replaced by LmcRbac\Options\ModuleOptionsFactory */ final class ModuleOptionsFactory { diff --git a/src/Container/ObjectRepositoryRoleProviderFactory.php b/src/Container/ObjectRepositoryRoleProviderFactory.php index 45ad66b3..65072cb1 100644 --- a/src/Container/ObjectRepositoryRoleProviderFactory.php +++ b/src/Container/ObjectRepositoryRoleProviderFactory.php @@ -31,6 +31,7 @@ * * @author Michaël Gallego * @licence MIT + * @deprecated Replaced by LmcRbac\Role\ObjectRepositoryRoleProviderFactory */ final class ObjectRepositoryRoleProviderFactory { diff --git a/src/Container/RoleServiceFactory.php b/src/Container/RoleServiceFactory.php index 20277ead..8add13e3 100644 --- a/src/Container/RoleServiceFactory.php +++ b/src/Container/RoleServiceFactory.php @@ -31,6 +31,7 @@ * * @author Michaël Gallego * @licence MIT + * @deprecated Replaced by LmcRbac\Service\RoleServiceFactory */ final class RoleServiceFactory { diff --git a/src/Options/ModuleOptions.php b/src/Options/ModuleOptions.php index e53ef0f0..82c88491 100644 --- a/src/Options/ModuleOptions.php +++ b/src/Options/ModuleOptions.php @@ -29,21 +29,21 @@ * @author Michaël Gallego * @licence MIT */ -final class ModuleOptions extends AbstractOptions +class ModuleOptions extends AbstractOptions { /** * Guest role (used when no identity is found) * * @var string */ - protected $guestRole = 'guest'; + protected string $guestRole = 'guest'; /** * Assertion map * * @var array */ - protected $assertionMap = []; + protected array $assertionMap = []; /** * A configuration for role provider @@ -51,7 +51,7 @@ final class ModuleOptions extends AbstractOptions * * @var array */ - protected $roleProvider = [ + protected array $roleProvider = [ 'LmcRbac\Role\InMemoryRoleProvider' => [], ]; diff --git a/src/Options/ModuleOptionsFactory.php b/src/Options/ModuleOptionsFactory.php new file mode 100644 index 00000000..dd0f56f5 --- /dev/null +++ b/src/Options/ModuleOptionsFactory.php @@ -0,0 +1,39 @@ + + * @licence MIT + */ +final class ModuleOptionsFactory +{ + public function __invoke(ContainerInterface $container): ModuleOptions + { + return new ModuleOptions($container->get('config')['lmc_rbac']); + } +} diff --git a/src/Role/InMemoryRoleProviderFactory.php b/src/Role/InMemoryRoleProviderFactory.php new file mode 100644 index 00000000..89c8c502 --- /dev/null +++ b/src/Role/InMemoryRoleProviderFactory.php @@ -0,0 +1,44 @@ +get(ModuleOptions::class); + + return new InMemoryRoleProvider( + $moduleOptions->getRoleProvider()[InMemoryRoleProvider::class] ?? [] + ); + } +} diff --git a/src/Role/ObjectRepositoryRoleProviderFactory.php b/src/Role/ObjectRepositoryRoleProviderFactory.php new file mode 100644 index 00000000..94e37e7c --- /dev/null +++ b/src/Role/ObjectRepositoryRoleProviderFactory.php @@ -0,0 +1,64 @@ + + * @licence MIT + */ +final class ObjectRepositoryRoleProviderFactory +{ + public function __invoke(ContainerInterface $container): ObjectRepositoryRoleProvider + { + $moduleOptions = $container->get(ModuleOptions::class); + $options = $moduleOptions->getRoleProvider()[ObjectRepositoryRoleProvider::class] ?? []; + + if (! isset($options['role_name_property'])) { + throw new Exception\RuntimeException('The "role_name_property" option is missing'); + } + + if (isset($options['object_repository'])) { + $objectRepository = $container->get($options['object_repository']); + + return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); + } + + if (isset($options['object_manager'], $options['class_name'])) { + $objectManager = $container->get($options['object_manager']); + $objectRepository = $objectManager->getRepository($options['class_name']); + + return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); + } + + throw new Exception\RuntimeException( + 'No object repository was found while creating the LmcRbac object repository role provider. Are + you sure you specified either the "object_repository" option or "object_manager"/"class_name" options?' + ); + } +} diff --git a/src/Service/AuthorizationServiceFactory.php b/src/Service/AuthorizationServiceFactory.php new file mode 100644 index 00000000..e500b008 --- /dev/null +++ b/src/Service/AuthorizationServiceFactory.php @@ -0,0 +1,50 @@ + + * @licence MIT + */ +final class AuthorizationServiceFactory +{ + public function __invoke(ContainerInterface $container): AuthorizationService + { + $moduleOptions = $container->get(ModuleOptions::class); + + return new AuthorizationService( + $container->get(Rbac::class), + $container->get(RoleServiceInterface::class), + $container->get(AssertionContainerInterface::class), + $moduleOptions->getAssertionMap() + ); + } +} diff --git a/src/Service/RoleServiceFactory.php b/src/Service/RoleServiceFactory.php new file mode 100644 index 00000000..2b2e93af --- /dev/null +++ b/src/Service/RoleServiceFactory.php @@ -0,0 +1,51 @@ + + * @licence MIT + */ +final class RoleServiceFactory +{ + public function __invoke(ContainerInterface $container): RoleService + { + $moduleOptions = $container->get(ModuleOptions::class); + + // 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()); + } +} diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index 627770c0..68029849 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -34,12 +34,12 @@ public function testProvidesExpectedConfiguration() $provider = new ConfigProvider(); $expected = [ 'factories' => [ - \LmcRbac\Assertion\AssertionContainerInterface::class => \LmcRbac\Container\AssertionContainerFactory::class, - \LmcRbac\Options\ModuleOptions::class => \LmcRbac\Container\ModuleOptionsFactory::class, - \LmcRbac\Role\InMemoryRoleProvider::class => \LmcRbac\Container\InMemoryRoleProviderFactory::class, - \LmcRbac\Role\ObjectRepositoryRoleProvider::class => \LmcRbac\Container\ObjectRepositoryRoleProviderFactory::class, - \LmcRbac\Service\AuthorizationServiceInterface::class => \LmcRbac\Container\AuthorizationServiceFactory::class, - \LmcRbac\Service\RoleServiceInterface::class => \LmcRbac\Container\RoleServiceFactory::class, + \LmcRbac\Assertion\AssertionContainerInterface::class => \LmcRbac\Assertion\AssertionContainerFactory::class, + \LmcRbac\Options\ModuleOptions::class => \LmcRbac\Options\ModuleOptionsFactory::class, + \LmcRbac\Role\InMemoryRoleProvider::class => \LmcRbac\Role\InMemoryRoleProviderFactory::class, + \LmcRbac\Role\ObjectRepositoryRoleProvider::class => \LmcRbac\Role\ObjectRepositoryRoleProviderFactory::class, + \LmcRbac\Service\AuthorizationServiceInterface::class => \LmcRbac\Service\AuthorizationServiceFactory::class, + \LmcRbac\Service\RoleServiceInterface::class => \LmcRbac\Service\RoleServiceFactory::class, \LmcRbac\Rbac::class => \Laminas\ServiceManager\Factory\InvokableFactory::class, ], ]; diff --git a/test/Container/AssertionContainerFactoryTest.php b/test/Container/AssertionContainerFactoryTest.php index 0b0c032c..1418aadc 100644 --- a/test/Container/AssertionContainerFactoryTest.php +++ b/test/Container/AssertionContainerFactoryTest.php @@ -23,11 +23,11 @@ use Laminas\ServiceManager\ServiceManager; use LmcRbac\Assertion\AssertionContainer; -use LmcRbac\Container\AssertionContainerFactory; +use LmcRbac\Assertion\AssertionContainerFactory; use PHPUnit\Framework\TestCase; /** - * @covers \LmcRbac\Container\AssertionContainerFactory + * @covers \LmcRbac\Assertion\AssertionContainerFactory */ class AssertionContainerFactoryTest extends TestCase { diff --git a/test/Container/AuthorizationServiceFactoryTest.php b/test/Container/AuthorizationServiceFactoryTest.php index 7af870e1..e8f3eeea 100644 --- a/test/Container/AuthorizationServiceFactoryTest.php +++ b/test/Container/AuthorizationServiceFactoryTest.php @@ -22,7 +22,7 @@ namespace LmcRbacTest\Container; use LmcRbac\Assertion\AssertionContainerInterface; -use LmcRbac\Container\AuthorizationServiceFactory; +use LmcRbac\Service\AuthorizationServiceFactory; use LmcRbac\Options\ModuleOptions; use LmcRbac\Rbac; use LmcRbac\Service\AuthorizationService; @@ -32,7 +32,7 @@ use Psr\Container\ContainerInterface; /** - * @covers \LmcRbac\Container\AuthorizationServiceFactory + * @covers \LmcRbac\Service\AuthorizationServiceFactory */ class AuthorizationServiceFactoryTest extends TestCase { diff --git a/test/Container/InMemoryRoleProviderFactoryTest.php b/test/Container/InMemoryRoleProviderFactoryTest.php index a5915142..8f6c5baf 100644 --- a/test/Container/InMemoryRoleProviderFactoryTest.php +++ b/test/Container/InMemoryRoleProviderFactoryTest.php @@ -22,13 +22,13 @@ namespace LmcRbacTest\Container; use Laminas\ServiceManager\ServiceManager; -use LmcRbac\Container\InMemoryRoleProviderFactory; +use LmcRbac\Role\InMemoryRoleProviderFactory; use LmcRbac\Options\ModuleOptions; use LmcRbac\Role\InMemoryRoleProvider; use PHPUnit\Framework\TestCase; /** - * @covers \LmcRbac\Container\InMemoryRoleProviderFactory + * @covers \LmcRbac\Role\InMemoryRoleProviderFactory */ class InMemoryRoleProviderFactoryTest extends TestCase { diff --git a/test/Container/ModuleOptionsFactoryTest.php b/test/Container/ModuleOptionsFactoryTest.php index a127a910..dd4a7bb1 100644 --- a/test/Container/ModuleOptionsFactoryTest.php +++ b/test/Container/ModuleOptionsFactoryTest.php @@ -22,12 +22,12 @@ namespace LmcRbacTest\Container; use Laminas\ServiceManager\ServiceManager; -use LmcRbac\Container\ModuleOptionsFactory; +use LmcRbac\Options\ModuleOptionsFactory; use LmcRbac\Options\ModuleOptions; use PHPUnit\Framework\TestCase; /** - * @covers \LmcRbac\Container\ModuleOptionsFactory + * @covers \LmcRbac\Options\ModuleOptionsFactory */ class ModuleOptionsFactoryTest extends TestCase { diff --git a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php index 7fbff9f6..b6adf988 100644 --- a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php +++ b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php @@ -24,14 +24,14 @@ use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectRepository; use Laminas\ServiceManager\ServiceManager; -use LmcRbac\Container\ObjectRepositoryRoleProviderFactory; +use LmcRbac\Role\ObjectRepositoryRoleProviderFactory; use LmcRbac\Exception\RuntimeException; use LmcRbac\Options\ModuleOptions; use LmcRbac\Role\ObjectRepositoryRoleProvider; use PHPUnit\Framework\TestCase; /** - * @covers \LmcRbac\Container\ObjectRepositoryRoleProviderFactory + * @covers \LmcRbac\Role\ObjectRepositoryRoleProviderFactory */ class ObjectRepositoryRoleProviderFactoryTest extends TestCase { diff --git a/test/Container/RoleServiceFactoryTest.php b/test/Container/RoleServiceFactoryTest.php index 5f0163a9..12947f9f 100644 --- a/test/Container/RoleServiceFactoryTest.php +++ b/test/Container/RoleServiceFactoryTest.php @@ -22,13 +22,13 @@ namespace LmcRbacTest\Container; use Laminas\ServiceManager\ServiceManager; -use LmcRbac\Container\RoleServiceFactory; use LmcRbac\Options\ModuleOptions; use LmcRbac\Role\InMemoryRoleProvider; +use LmcRbac\Service\RoleServiceFactory; use PHPUnit\Framework\TestCase; /** - * @covers \LmcRbac\Container\RoleServiceFactory + * @covers \LmcRbac\Service\RoleServiceFactory */ class RoleServiceFactoryTest extends TestCase {