From 4e925965119d61d7947bff522daa714ff704ab5d Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Tue, 19 Feb 2019 11:58:17 +0200 Subject: [PATCH 1/3] changed types from array to iterable nad other improovements --- composer.json | 12 +++---- src/ModuleConfig.php | 2 +- src/Options/ModuleOptions.php | 2 +- src/Role/HierarchicalRole.php | 39 +++++++++++++++++++++-- src/Role/HierarchicalRoleInterface.php | 2 +- src/Role/InMemoryRoleProvider.php | 2 +- src/Role/ObjectRepositoryRoleProvider.php | 2 +- src/Role/Role.php | 6 ++-- src/Role/RoleProviderInterface.php | 2 +- src/Service/AuthorizationService.php | 20 ++++++------ src/Service/RoleService.php | 8 ++--- src/Service/RoleServiceInterface.php | 2 +- test/Asset/FlatRole.php | 25 ++++++--------- test/Asset/HierarchicalRole.php | 2 +- test/Asset/Identity.php | 2 +- 15 files changed, 76 insertions(+), 52 deletions(-) diff --git a/composer.json b/composer.json index 35155f5..951b7b5 100644 --- a/composer.json +++ b/composer.json @@ -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/src/ModuleConfig.php b/src/ModuleConfig.php index c0a2982..7cfe8a6 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 9bf69d4..4157d53 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 2737fcb..c9b7037 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 12ac017..3068d81 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 6b04005..01bd87d 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 d6dfc8a..dd2e6c8 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 101a67d..a0d7544 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 25c34b3..0d77b28 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 58a505a..7bbea9c 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 9fe6442..fab62d1 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 4da601f..1df5e4d 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 2f9b4aa..26142e6 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 b9e473c..433bec4 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 78b6774..b05fea3 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; } From c5341e92176304016134849e375b630d9ab138b2 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Tue, 19 Feb 2019 12:46:56 +0200 Subject: [PATCH 2/3] update phpunit --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 951b7b5..f626954 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": "^6.5.14", + "friendsofphp/php-cs-fixer": "^2.9.3", "doctrine/common": "^2.4", "satooshi/php-coveralls": "^2.0" }, From 4df85fca1b4bbb20554670a1e8f1b1a47414485f Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Tue, 19 Feb 2019 12:52:35 +0200 Subject: [PATCH 3/3] update phpunit to supported version --- composer.json | 2 +- phpunit.xml.dist | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index f626954..21b19f0 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ }, "require-dev": { "malukenho/docheader": "^0.1.7", - "phpunit/phpunit": "^6.5.14", + "phpunit/phpunit": "^7.5.6", "friendsofphp/php-cs-fixer": "^2.9.3", "doctrine/common": "^2.4", "satooshi/php-coveralls": "^2.0" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 17fe824..c1af5ea 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