Skip to content

Commit

Permalink
Merge pull request #50 from visto9259/2.x
Browse files Browse the repository at this point in the history
Refactored factories
  • Loading branch information
visto9259 authored May 24, 2024
2 parents 542e893 + aa95377 commit 1a9bac9
Show file tree
Hide file tree
Showing 22 changed files with 324 additions and 29 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 41 additions & 0 deletions src/Assertion/AssertionContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace LmcRbac\Assertion;

use LmcRbac\Assertion\AssertionContainer;
use Psr\Container\ContainerInterface;

/**
* Factory to create a assertion plugin manager
*
* @author Aeneas Rekkas
* @licence MIT
*/
final class AssertionContainerFactory
{
public function __invoke(ContainerInterface $container): AssertionContainer
{
$config = $container->get('config')['lmc_rbac']['assertion_manager'];

return new AssertionContainer($container, $config);
}
}
12 changes: 6 additions & 6 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
Expand Down
1 change: 1 addition & 0 deletions src/Container/AssertionContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* @author Aeneas Rekkas
* @licence MIT
* @deprecated Replaced by LmcRbac\Assertion\AssertionContainerFactory
*/
final class AssertionContainerFactory
{
Expand Down
1 change: 1 addition & 0 deletions src/Container/AuthorizationServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
* @deprecated Replaced by LmcRbac\Service\AuthorizationServiceFactory
*/
final class AuthorizationServiceFactory
{
Expand Down
1 change: 1 addition & 0 deletions src/Container/InMemoryRoleProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*
* @author Vytautas Stankus
* @licence MIT
* @deprecated Replaced by LmcRbac\Role\InMemoryRoleProviderFactory
*/
final class InMemoryRoleProviderFactory
{
Expand Down
1 change: 1 addition & 0 deletions src/Container/ModuleOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
* @deprecated Replaced by LmcRbac\Options\ModuleOptionsFactory
*/
final class ModuleOptionsFactory
{
Expand Down
1 change: 1 addition & 0 deletions src/Container/ObjectRepositoryRoleProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
* @deprecated Replaced by LmcRbac\Role\ObjectRepositoryRoleProviderFactory
*/
final class ObjectRepositoryRoleProviderFactory
{
Expand Down
1 change: 1 addition & 0 deletions src/Container/RoleServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
* @deprecated Replaced by LmcRbac\Service\RoleServiceFactory
*/
final class RoleServiceFactory
{
Expand Down
8 changes: 4 additions & 4 deletions src/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@
* @author Michaël Gallego <[email protected]>
* @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
* Defaults to InMemoryRoleProvider
*
* @var array
*/
protected $roleProvider = [
protected array $roleProvider = [
'LmcRbac\Role\InMemoryRoleProvider' => [],
];

Expand Down
39 changes: 39 additions & 0 deletions src/Options/ModuleOptionsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace LmcRbac\Options;

use LmcRbac\Options\ModuleOptions;
use Psr\Container\ContainerInterface;

/**
* Factory for the module options
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
final class ModuleOptionsFactory
{
public function __invoke(ContainerInterface $container): ModuleOptions
{
return new ModuleOptions($container->get('config')['lmc_rbac']);
}
}
44 changes: 44 additions & 0 deletions src/Role/InMemoryRoleProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace LmcRbac\Role;

use LmcRbac\Options\ModuleOptions;
use LmcRbac\Role\InMemoryRoleProvider;
use Psr\Container\ContainerInterface;

/**
* Factory used to create an in memory role provider
*
* @author Vytautas Stankus
* @licence MIT
*/
final class InMemoryRoleProviderFactory
{
public function __invoke(ContainerInterface $container): InMemoryRoleProvider
{
$moduleOptions = $container->get(ModuleOptions::class);

return new InMemoryRoleProvider(
$moduleOptions->getRoleProvider()[InMemoryRoleProvider::class] ?? []
);
}
}
64 changes: 64 additions & 0 deletions src/Role/ObjectRepositoryRoleProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace LmcRbac\Role;

use LmcRbac\Exception;
use LmcRbac\Options\ModuleOptions;
use LmcRbac\Role\ObjectRepositoryRoleProvider;
use Psr\Container\ContainerInterface;

/**
* Factory used to create an object repository role provider
*
* @author Michaël Gallego <[email protected]>
* @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?'
);
}
}
50 changes: 50 additions & 0 deletions src/Service/AuthorizationServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace LmcRbac\Service;

use LmcRbac\Assertion\AssertionContainerInterface;
use LmcRbac\Options\ModuleOptions;
use LmcRbac\Rbac;
use LmcRbac\Service\AuthorizationService;
use LmcRbac\Service\RoleServiceInterface;
use Psr\Container\ContainerInterface;

/**
* Factory to create the authorization service
*
* @author Michaël Gallego <[email protected]>
* @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()
);
}
}
Loading

0 comments on commit 1a9bac9

Please sign in to comment.