Skip to content

Commit

Permalink
Merge pull request #22 from zf-fr/simplfy
Browse files Browse the repository at this point in the history
Remove traversal strategy
  • Loading branch information
bakura10 committed Nov 8, 2015
2 parents b2bebea + c61193b commit 1a1fd91
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 519 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
sudo: false

language: php

php:
Expand All @@ -16,8 +18,4 @@ before_script:

script:
- ./vendor/bin/phpunit --coverage-clover=coverage.clover --group=Coverage
- ./vendor/bin/phpcs --standard=PSR2 ./src/

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
- ./vendor/bin/phpcs --standard=PSR2 ./src/
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## 2.0.0

* Minimum PHP dependency has been raised to 5.5.
* `PermissionInterface` has been removed from RBAC component. This has been moved to ZfcRbac.
* [BC] Minimum PHP dependency has been raised to 5.5.
* [BC] `PermissionInterface` has been removed from RBAC component. This has been moved to ZfcRbac. Rbac only accepts "mixed"
permission, and it is up to your implementation to decide what a permission is.
* [BC] Rbac will now throw an exception if permission is not a string. If you need to do more check, you could use assertions for instance.

## 1.2.0

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"autoload-dev": {
"psr-4": {
"RbacTest\\": "tests/"
"RbacTest\\": "test/"
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
syntaxCheck="true"
>
<testsuite name="ZfrRbac tests">
<directory>./tests</directory>
<directory>./test</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Rbac\Traversal\Strategy;

use Rbac\Role\RoleInterface;
use Traversable;
namespace Rbac\Exception;

/**
* Interface for a traversal strategy
* Base interface for Rbac
*/
interface TraversalStrategyInterface
interface ExceptionInterface
{
/**
* @param RoleInterface[]|Traversable
* @return Traversable
*/
public function getRolesIterator($roles);
}
16 changes: 16 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Rbac\Exception;

use RuntimeException as BaseRuntimeException;

class RuntimeException extends BaseRuntimeException implements ExceptionInterface
{
}
51 changes: 28 additions & 23 deletions src/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,38 @@

namespace Rbac;

use Generator;
use Rbac\Exception\RuntimeException;
use Rbac\Role\HierarchicalRoleInterface;
use Rbac\Role\RoleInterface;
use Rbac\Traversal\Strategy\TraversalStrategyInterface;
use Traversable;

/**
* Rbac object. It is used to check a permission against roles
*/
class Rbac
{
/**
* @var TraversalStrategyInterface
*/
protected $traversalStrategy;

/**
* @param TraversalStrategyInterface $strategy
*/
public function __construct(TraversalStrategyInterface $strategy)
{
$this->traversalStrategy = $strategy;
}

/**
* Determines if access is granted by checking the roles for permission.
*
* @param RoleInterface|RoleInterface[]|Traversable $roles
* @param mixed $permission
* @param string $permission
* @return bool
*/
public function isGranted($roles, $permission)
{
if (!is_string($permission)) {
throw new RuntimeException(sprintf(
'Permission must be a string, "%s" given',
is_object($permission) ? get_class($permission) : gettype($permission)
));
}

if ($roles instanceof RoleInterface) {
$roles = [$roles];
}

$iterator = $this->traversalStrategy->getRolesIterator($roles);

foreach ($iterator as $role) {
foreach ($this->flattenRoles($roles) as $role) {
/* @var RoleInterface $role */
if ($role->hasPermission($permission)) {
return true;
Expand All @@ -57,12 +51,23 @@ public function isGranted($roles, $permission)
}

/**
* Get the strategy.
*
* @return TraversalStrategyInterface
* @param RoleInterface[]|Traversable $roles
* @return Generator
*/
public function getTraversalStrategy()
protected function flattenRoles($roles)
{
return $this->traversalStrategy;
foreach ($roles as $role) {
yield $role;

if (!$role instanceof HierarchicalRoleInterface) {
continue;
}

$children = $this->flattenRoles($role->getChildren());

foreach ($children as $child) {
yield $child;
}
}
}
}
2 changes: 1 addition & 1 deletion src/Role/HierarchicalRoleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function hasChildren();
/**
* Get child roles
*
* @return array|RoleInterface[]|Traversable
* @return RoleInterface[]|Traversable
*/
public function getChildren();
}
2 changes: 1 addition & 1 deletion src/Role/RoleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getName();
/**
* Checks if a permission exists for this role (it does not check child roles)
*
* @param mixed $permission
* @param string $permission
* @return bool
*/
public function hasPermission($permission);
Expand Down
63 changes: 0 additions & 63 deletions src/Traversal/RecursiveRoleIterator.php

This file was deleted.

43 changes: 0 additions & 43 deletions src/Traversal/Strategy/GeneratorStrategy.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/Traversal/Strategy/RecursiveRoleIteratorStrategy.php

This file was deleted.

Loading

0 comments on commit 1a1fd91

Please sign in to comment.