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

Commit

Permalink
Merge 4df85fc into 68691c4
Browse files Browse the repository at this point in the history
  • Loading branch information
svycka committed Feb 19, 2019
2 parents 68691c4 + 4df85fc commit b771067
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 58 deletions.
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
}
}
4 changes: 1 addition & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
stopOnFailure="false"
processIsolation="false"
backupGlobals="false"
syntaxCheck="true"
>
<testsuite name="ZfcRbac tests">
<directory>./test</directory>
Expand All @@ -20,7 +19,6 @@
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./build/coverage" charset="UTF-8"
yui="true" highlight="true" lowUpperBound="80" highLowerBound="90"/>
<log type="coverage-html" target="./build/coverage" lowUpperBound="80" highLowerBound="90"/>
</logging>
</phpunit>
2 changes: 1 addition & 1 deletion src/ModuleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @author Florent Blaison <florent.blaison@gmail.com>
* @licence MIT
*/
class ModuleConfig
final class ModuleConfig
{
public function __invoke(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
39 changes: 37 additions & 2 deletions src/Role/HierarchicalRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Role/HierarchicalRoleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public function hasChildren(): bool;
/**
* @return RoleInterface[]
*/
public function getChildren(): array;
public function getChildren(): iterable;
}
2 changes: 1 addition & 1 deletion src/Role/InMemoryRoleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(array $rolesConfig)
/**
* {@inheritdoc}
*/
public function getRoles(array $roleNames): array
public function getRoles(iterable $roleNames): iterable
{
$roles = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Role/ObjectRepositoryRoleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/Role/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Role/RoleProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ interface RoleProviderInterface
* @param string[] $roleNames
* @return RoleInterface[]
*/
public function getRoles(array $roleNames): array;
public function getRoles(iterable $roleNames): iterable;
}
20 changes: 10 additions & 10 deletions src/Service/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 2 additions & 6 deletions src/Service/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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 = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Service/RoleServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 9 additions & 16 deletions test/Asset/FlatRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,8 +57,7 @@ class FlatRole extends Role
*/
public function __construct(string $name)
{
parent::__construct($name);

$this->name = $name;
$this->permissions = new ArrayCollection();
}

Expand All @@ -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]);
}
}
2 changes: 1 addition & 1 deletion test/Asset/HierarchicalRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function hasChildren(): bool
return ! empty($this->children);
}

public function getChildren(): array
public function getChildren(): iterable
{
return $this->children;
}
Expand Down
2 changes: 1 addition & 1 deletion test/Asset/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(array $roles = [])
$this->roles = $roles;
}

public function getRoles(): array
public function getRoles(): iterable
{
return $this->roles;
}
Expand Down

0 comments on commit b771067

Please sign in to comment.