Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #380 from svycka/plugin-managers-refactor
Browse files Browse the repository at this point in the history
Plugin managers refactor and more improvements
  • Loading branch information
basz authored Feb 26, 2018
2 parents 1859bc1 + e52eb56 commit 68691c4
Show file tree
Hide file tree
Showing 27 changed files with 320 additions and 407 deletions.
3 changes: 1 addition & 2 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class Config extends PhpCsFixerConfig
'@PHP71Migration' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'align_double_arrow' => true,
'align_equals' => false,
'default' => 'single_space',
],
'blank_line_after_opening_tag' => true,
'blank_line_after_namespace' => true,
Expand Down
14 changes: 6 additions & 8 deletions config/dependencies.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
return [
'dependencies' => [
'factories' => [
ZfcRbac\Assertion\AssertionPluginManager::class => ZfcRbac\Container\AssertionPluginManagerFactory::class,
ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class,
ZfcRbac\Role\RoleProviderPluginManager::class => ZfcRbac\Container\RoleProviderPluginManagerFactory::class,
ZfcRbac\Assertion\AssertionContainerInterface::class => ZfcRbac\Container\AssertionContainerFactory::class,
ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class,
ZfcRbac\Role\InMemoryRoleProvider::class => ZfcRbac\Container\InMemoryRoleProviderFactory::class,
ZfcRbac\Role\ObjectRepositoryRoleProvider::class => ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class,
ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class,
ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class,
ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class,
ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
],
],

'zfc_rbac' => [
// Role provider plugin manager
'role_provider_manager' => [],

// Assertion plugin manager
'assertion_manager' => [],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
*
* @author Aeneas Rekkas
* @licence MIT
*
* @method AssertionInterface get($name)
*/
class AssertionPluginManager extends AbstractPluginManager
final class AssertionContainer extends AbstractPluginManager implements AssertionContainerInterface
{
protected $instanceOf = AssertionInterface::class;

/**
* {@inheritdoc}
*/
public function get($name, array $options = null): AssertionInterface
{
return parent::get($name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,11 @@

declare(strict_types=1);

namespace ZfcRbac\Role;
namespace ZfcRbac\Assertion;

use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Factory\InvokableFactory;
use ZfcRbac\Container\ObjectRepositoryRoleProviderFactory;
use Psr\Container\ContainerInterface;

/**
* Plugin manager to create role providers
*
* @method RoleProviderInterface get($name)
*
* @author Michaël Gallego <mic.gallego@gmail.com>
* @license MIT
*/
final class RoleProviderPluginManager extends AbstractPluginManager
interface AssertionContainerInterface extends ContainerInterface
{
protected $instanceOf = RoleProviderInterface::class;

/**
* @var array
*/
protected $factories = [
InMemoryRoleProvider::class => InvokableFactory::class,
ObjectRepositoryRoleProvider::class => ObjectRepositoryRoleProviderFactory::class,
];
public function get($name): AssertionInterface;
}
14 changes: 7 additions & 7 deletions src/Assertion/AssertionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ final class AssertionSet implements AssertionInterface
const CONDITION_AND = 'condition_and';

/**
* @var AssertionPluginManager
* @var AssertionContainerInterface
*/
private $assertionPluginManager;
private $assertionContainer;

/**
* @var array
*/
private $assertions = [];
private $assertions;

private $condition = self::CONDITION_AND;

public function __construct(AssertionPluginManager $assertionPluginManager, array $assertions)
public function __construct(AssertionContainerInterface $assertionContainer, array $assertions)
{
if (isset($assertions['condition'])) {
if ($assertions['condition'] !== AssertionSet::CONDITION_AND
Expand All @@ -58,7 +58,7 @@ public function __construct(AssertionPluginManager $assertionPluginManager, arra
}

$this->assertions = $assertions;
$this->assertionPluginManager = $assertionPluginManager;
$this->assertionContainer = $assertionContainer;
}

public function assert(string $permission, IdentityInterface $identity = null, $context = null): bool
Expand All @@ -78,12 +78,12 @@ public function assert(string $permission, IdentityInterface $identity = null, $
$asserted = $assertion->assert($permission, $identity, $context);
break;
case is_string($assertion):
$this->assertions[$index] = $assertion = $this->assertionPluginManager->get($assertion);
$this->assertions[$index] = $assertion = $this->assertionContainer->get($assertion);

$asserted = $assertion->assert($permission, $identity, $context);
break;
case is_array($assertion):
$this->assertions[$index] = $assertion = new AssertionSet($this->assertionPluginManager, $assertion);
$this->assertions[$index] = $assertion = new AssertionSet($this->assertionContainer, $assertion);
$asserted = $assertion->assert($permission, $identity, $context);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
namespace ZfcRbac\Container;

use Psr\Container\ContainerInterface;
use ZfcRbac\Assertion\AssertionPluginManager;
use ZfcRbac\Assertion\AssertionContainer;

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

return new AssertionPluginManager($container, $config);
return new AssertionContainer($container, $config);
}
}
4 changes: 2 additions & 2 deletions src/Container/AuthorizationServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace ZfcRbac\Container;

use Psr\Container\ContainerInterface;
use ZfcRbac\Assertion\AssertionPluginManager;
use ZfcRbac\Assertion\AssertionContainerInterface;
use ZfcRbac\Options\ModuleOptions;
use ZfcRbac\Rbac;
use ZfcRbac\Service\AuthorizationService;
Expand All @@ -43,7 +43,7 @@ public function __invoke(ContainerInterface $container): AuthorizationService
return new AuthorizationService(
$container->get(Rbac::class),
$container->get(RoleServiceInterface::class),
$container->get(AssertionPluginManager::class),
$container->get(AssertionContainerInterface::class),
$moduleOptions->getAssertionMap()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@
namespace ZfcRbac\Container;

use Psr\Container\ContainerInterface;
use ZfcRbac\Role\RoleProviderPluginManager;
use ZfcRbac\Options\ModuleOptions;
use ZfcRbac\Role\InMemoryRoleProvider;

/**
* Factory to create a role provider plugin manager
* Factory used to create an in memory role provider
*
* @author Michaël Gallego <mic.gallego@gmail.com>
* @author Vytautas Stankus
* @licence MIT
*/
final class RoleProviderPluginManagerFactory
final class InMemoryRoleProviderFactory
{
public function __invoke(ContainerInterface $container): RoleProviderPluginManager
public function __invoke(ContainerInterface $container): InMemoryRoleProvider
{
$config = $container->get('config')['zfc_rbac']['role_provider_manager'];
$moduleOptions = $container->get(ModuleOptions::class);

return new RoleProviderPluginManager($container, $config);
return new InMemoryRoleProvider(
$moduleOptions->getRoleProvider()[InMemoryRoleProvider::class] ?? []
);
}
}
6 changes: 5 additions & 1 deletion src/Container/ObjectRepositoryRoleProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

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

/**
Expand All @@ -33,8 +34,11 @@
*/
final class ObjectRepositoryRoleProviderFactory
{
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = []): ObjectRepositoryRoleProvider
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');
}
Expand Down
16 changes: 2 additions & 14 deletions src/Container/RoleServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
namespace ZfcRbac\Container;

use Psr\Container\ContainerInterface;
use ZfcRbac\Exception\RuntimeException;
use ZfcRbac\Options\ModuleOptions;
use ZfcRbac\Role\RoleProviderPluginManager;
use ZfcRbac\Role\RoleProviderInterface;
use ZfcRbac\Service\RoleService;

/**
Expand All @@ -39,17 +38,6 @@ public function __invoke(ContainerInterface $container): RoleService
{
$moduleOptions = $container->get(ModuleOptions::class);

$roleProviderConfig = $moduleOptions->getRoleProvider();

if (empty($roleProviderConfig)) {
throw new RuntimeException('No role provider has been set for ZfcRbac');
}

$pluginManager = $container->get(RoleProviderPluginManager::class);
$roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig));
$roleService = new RoleService($roleProvider);
$roleService->setGuestRole($moduleOptions->getGuestRole());

return $roleService;
return new RoleService($container->get(RoleProviderInterface::class), $moduleOptions->getGuestRole());
}
}
8 changes: 0 additions & 8 deletions src/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace ZfcRbac\Options;

use Zend\Stdlib\AbstractOptions;
use ZfcRbac\Exception;

/**
* Options for ZfcRbac module
Expand Down Expand Up @@ -111,16 +110,9 @@ public function getGuestRole(): string
* Set the configuration for the role provider
*
* @param array $roleProvider
* @throws Exception\RuntimeException
*/
public function setRoleProvider(array $roleProvider): void
{
if (count($roleProvider) > 1) {
throw new Exception\RuntimeException(
'You can only have one role provider'
);
}

$this->roleProvider = $roleProvider;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Service/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace ZfcRbac\Service;

use ZfcRbac\Assertion\AssertionPluginManager;
use ZfcRbac\Assertion\AssertionContainerInterface;
use ZfcRbac\Assertion\AssertionSet;
use ZfcRbac\Identity\IdentityInterface;
use ZfcRbac\Rbac;
Expand All @@ -46,9 +46,9 @@ final class AuthorizationService implements AuthorizationServiceInterface
private $roleService;

/**
* @var AssertionPluginManager
* @var AssertionContainerInterface
*/
private $assertionPluginManager;
private $assertionContainer;

/**
* @var array
Expand All @@ -58,12 +58,12 @@ final class AuthorizationService implements AuthorizationServiceInterface
public function __construct(
Rbac $rbac,
RoleServiceInterface $roleService,
AssertionPluginManager $assertionPluginManager,
AssertionContainerInterface $assertionContainer,
array $assertions = []
) {
$this->rbac = $rbac;
$this->roleService = $roleService;
$this->assertionPluginManager = $assertionPluginManager;
$this->assertionContainer = $assertionContainer;
$this->assertions = $assertions;
}

Expand All @@ -86,7 +86,7 @@ public function isGranted(?IdentityInterface $identity, string $permission, $con
$permissionAssertions = [$this->assertions[$permission]];
}

$assertionSet = new AssertionSet($this->assertionPluginManager, $permissionAssertions);
$assertionSet = new AssertionSet($this->assertionContainer, $permissionAssertions);

return $assertionSet->assert($permission, $identity, $context);
}
Expand Down
20 changes: 3 additions & 17 deletions src/Service/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,19 @@ final class RoleService implements RoleServiceInterface
/**
* @var RoleProviderInterface
*/
protected $roleProvider;
private $roleProvider;

/**
* @var string
*/
protected $guestRole = '';
private $guestRole;

public function __construct(RoleProviderInterface $roleProvider)
public function __construct(RoleProviderInterface $roleProvider, string $guestRole)
{
$this->roleProvider = $roleProvider;
}

public function setRoleProvider(RoleProviderInterface $roleProvider): void
{
$this->roleProvider = $roleProvider;
}

public function setGuestRole(string $guestRole): void
{
$this->guestRole = $guestRole;
}

public function getGuestRole(): string
{
return $this->guestRole;
}

/**
* Get the identity roles from the current identity, applying some more logic
*
Expand Down
Loading

0 comments on commit 68691c4

Please sign in to comment.