diff --git a/composer.json b/composer.json index 35155f58..21b19f0c 100644 --- a/composer.json +++ b/composer.json @@ -32,9 +32,9 @@ "zendframework/zend-stdlib": "^3.1" }, "require-dev": { - "malukenho/docheader": "^0.1", - "phpunit/phpunit": "^6.0", - "friendsofphp/php-cs-fixer": "^2.1", + "malukenho/docheader": "^0.1.7", + "phpunit/phpunit": "^7.5.6", + "friendsofphp/php-cs-fixer": "^2.9.3", "doctrine/common": "^2.4", "satooshi/php-coveralls": "^2.0" }, @@ -54,15 +54,15 @@ "dev-develop": "3.0-dev" } }, - "scripts": { - "check": [ + "scripts": { + "check": [ "@cs", "@test", "@header" ], - "cs": "php-cs-fixer fix -v --diff --dry-run", - "cs-fix": "php-cs-fixer fix -v --diff", - "test": "phpunit", - "header": "docheader check src test" + "cs": "php-cs-fixer fix -v --diff --dry-run", + "cs-fix": "php-cs-fixer fix -v --diff", + "test": "phpunit", + "header": "docheader check src test" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 17fe824f..c1af5ea2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,6 @@ stopOnFailure="false" processIsolation="false" backupGlobals="false" - syntaxCheck="true" > ./test @@ -20,7 +19,6 @@ - + \ No newline at end of file diff --git a/src/Identity/IdentityInterface.php b/src/Identity/IdentityInterface.php index 1151e167..57cf4e5f 100644 --- a/src/Identity/IdentityInterface.php +++ b/src/Identity/IdentityInterface.php @@ -36,5 +36,5 @@ interface IdentityInterface * * @return string[]|RoleInterface[] */ - public function getRoles(); + public function getRoles(): iterable; } diff --git a/src/ModuleConfig.php b/src/ModuleConfig.php index c0a2982a..7cfe8a6c 100644 --- a/src/ModuleConfig.php +++ b/src/ModuleConfig.php @@ -27,7 +27,7 @@ * @author Florent Blaison * @licence MIT */ -class ModuleConfig +final class ModuleConfig { public function __invoke(): array { diff --git a/src/Options/ModuleOptions.php b/src/Options/ModuleOptions.php index 9bf69d41..4157d53c 100644 --- a/src/Options/ModuleOptions.php +++ b/src/Options/ModuleOptions.php @@ -109,7 +109,7 @@ public function getGuestRole(): string /** * Set the configuration for the role provider * - * @param array $roleProvider + * @param array $roleProvider */ public function setRoleProvider(array $roleProvider): void { diff --git a/src/Role/HierarchicalRole.php b/src/Role/HierarchicalRole.php index 2737fcbf..c9b70374 100644 --- a/src/Role/HierarchicalRole.php +++ b/src/Role/HierarchicalRole.php @@ -24,19 +24,54 @@ /** * Simple implementation for a hierarchical role */ -final class HierarchicalRole extends Role implements HierarchicalRoleInterface +final class HierarchicalRole implements HierarchicalRoleInterface { + /** + * @var string + */ + private $name; + + /** + * @var string[] + */ + private $permissions = []; + /** * @var array|RoleInterface[] */ private $children = []; + public function __construct(string $name) + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } + + public function getPermissions(): array + { + return $this->permissions; + } + + public function addPermission(string $permission): void + { + $this->permissions[$permission] = $permission; + } + + public function hasPermission(string $permission): bool + { + return isset($this->permissions[$permission]); + } + public function hasChildren(): bool { return ! empty($this->children); } - public function getChildren(): array + public function getChildren(): iterable { return $this->children; } diff --git a/src/Role/HierarchicalRoleInterface.php b/src/Role/HierarchicalRoleInterface.php index 12ac0175..3068d812 100644 --- a/src/Role/HierarchicalRoleInterface.php +++ b/src/Role/HierarchicalRoleInterface.php @@ -33,5 +33,5 @@ public function hasChildren(): bool; /** * @return RoleInterface[] */ - public function getChildren(): array; + public function getChildren(): iterable; } diff --git a/src/Role/InMemoryRoleProvider.php b/src/Role/InMemoryRoleProvider.php index 6b04005c..01bd87d5 100644 --- a/src/Role/InMemoryRoleProvider.php +++ b/src/Role/InMemoryRoleProvider.php @@ -57,7 +57,7 @@ public function __construct(array $rolesConfig) /** * {@inheritdoc} */ - public function getRoles(array $roleNames): array + public function getRoles(iterable $roleNames): iterable { $roles = []; diff --git a/src/Role/ObjectRepositoryRoleProvider.php b/src/Role/ObjectRepositoryRoleProvider.php index d6dfc8a5..dd2e6c89 100644 --- a/src/Role/ObjectRepositoryRoleProvider.php +++ b/src/Role/ObjectRepositoryRoleProvider.php @@ -58,7 +58,7 @@ public function clearRoleCache(): void $this->roleCache = []; } - public function getRoles(array $roleNames): array + public function getRoles(iterable $roleNames): iterable { $key = implode($roleNames); diff --git a/src/Role/Role.php b/src/Role/Role.php index 101a67d1..a0d75446 100644 --- a/src/Role/Role.php +++ b/src/Role/Role.php @@ -25,17 +25,17 @@ * Simple implementation for a role without hierarchy * and using strings as permissions */ -class Role implements RoleInterface +final class Role implements RoleInterface { /** * @var string */ - protected $name; + private $name; /** * @var string[] */ - protected $permissions = []; + private $permissions = []; public function __construct(string $name) { diff --git a/src/Role/RoleProviderInterface.php b/src/Role/RoleProviderInterface.php index 25c34b3e..0d77b28b 100644 --- a/src/Role/RoleProviderInterface.php +++ b/src/Role/RoleProviderInterface.php @@ -38,5 +38,5 @@ interface RoleProviderInterface * @param string[] $roleNames * @return RoleInterface[] */ - public function getRoles(array $roleNames): array; + public function getRoles(iterable $roleNames): iterable; } diff --git a/src/Service/AuthorizationService.php b/src/Service/AuthorizationService.php index 58a505ae..7bbea9c8 100644 --- a/src/Service/AuthorizationService.php +++ b/src/Service/AuthorizationService.php @@ -79,18 +79,18 @@ public function isGranted(?IdentityInterface $identity, string $permission, $con return false; } - if (! empty($this->assertions[$permission])) { - if (\is_array($this->assertions[$permission])) { - $permissionAssertions = $this->assertions[$permission]; - } else { - $permissionAssertions = [$this->assertions[$permission]]; - } - - $assertionSet = new AssertionSet($this->assertionContainer, $permissionAssertions); + if (empty($this->assertions[$permission])) { + return true; + } - return $assertionSet->assert($permission, $identity, $context); + if (\is_array($this->assertions[$permission])) { + $permissionAssertions = $this->assertions[$permission]; + } else { + $permissionAssertions = [$this->assertions[$permission]]; } - return true; + $assertionSet = new AssertionSet($this->assertionContainer, $permissionAssertions); + + return $assertionSet->assert($permission, $identity, $context); } } diff --git a/src/Service/RoleService.php b/src/Service/RoleService.php index 9fe64424..fab62d16 100644 --- a/src/Service/RoleService.php +++ b/src/Service/RoleService.php @@ -57,7 +57,7 @@ public function __construct(RoleProviderInterface $roleProvider, string $guestRo * @param null $context * @return RoleInterface[] */ - public function getIdentityRoles(IdentityInterface $identity = null, $context = null): array + public function getIdentityRoles(IdentityInterface $identity = null, $context = null): iterable { if (null === $identity) { return $this->convertRoles([$this->guestRole]); @@ -72,12 +72,8 @@ public function getIdentityRoles(IdentityInterface $identity = null, $context = * @param array|Traversable $roles * @return RoleInterface[] */ - private function convertRoles($roles): array + private function convertRoles(iterable $roles): iterable { - if ($roles instanceof Traversable) { - $roles = iterator_to_array($roles); - } - $collectedRoles = []; $toCollect = []; diff --git a/src/Service/RoleServiceInterface.php b/src/Service/RoleServiceInterface.php index 4da601f8..1df5e4d3 100644 --- a/src/Service/RoleServiceInterface.php +++ b/src/Service/RoleServiceInterface.php @@ -39,5 +39,5 @@ interface RoleServiceInterface * @param mixed $context * @return RoleInterface[] */ - public function getIdentityRoles(IdentityInterface $identity = null, $context = null): array; + public function getIdentityRoles(IdentityInterface $identity = null, $context = null): iterable; } diff --git a/test/Asset/FlatRole.php b/test/Asset/FlatRole.php index 2f9b4aa5..26142e66 100644 --- a/test/Asset/FlatRole.php +++ b/test/Asset/FlatRole.php @@ -23,13 +23,13 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; -use ZfcRbac\Role\Role; +use ZfcRbac\Role\RoleInterface; /** * @ORM\Entity * @ORM\Table(name="roles") */ -class FlatRole extends Role +class FlatRole implements RoleInterface { /** * @var int|null @@ -57,8 +57,7 @@ class FlatRole extends Role */ public function __construct(string $name) { - parent::__construct($name); - + $this->name = $name; $this->permissions = new ArrayCollection(); } @@ -72,19 +71,13 @@ public function getId() return $this->id; } - /** - * Add a permission - * - * @param string $permission - * @return void - */ - public function addPermission(string $permission): void + public function getName(): string { - if (is_string($permission)) { - $name = $permission; - $permission = new Permission($name); - } + return $this->name; + } - $this->permissions[$permission->getName()] = $permission; + public function hasPermission(string $permission): bool + { + return isset($this->permissions[$permission]); } } diff --git a/test/Asset/HierarchicalRole.php b/test/Asset/HierarchicalRole.php index b9e473c6..433bec4a 100644 --- a/test/Asset/HierarchicalRole.php +++ b/test/Asset/HierarchicalRole.php @@ -88,7 +88,7 @@ public function hasChildren(): bool return ! empty($this->children); } - public function getChildren(): array + public function getChildren(): iterable { return $this->children; } diff --git a/test/Asset/Identity.php b/test/Asset/Identity.php index 78b67748..b05fea37 100644 --- a/test/Asset/Identity.php +++ b/test/Asset/Identity.php @@ -35,7 +35,7 @@ public function __construct(array $roles = []) $this->roles = $roles; } - public function getRoles(): array + public function getRoles(): iterable { return $this->roles; }