Skip to content

Commit

Permalink
Merge pull request #12 from ripaclub/develop
Browse files Browse the repository at this point in the history
Parent roles support
  • Loading branch information
leodido committed Oct 1, 2015
2 parents 007c7fa + 7b021c4 commit 03eaaa3
Show file tree
Hide file tree
Showing 50 changed files with 299 additions and 185 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Add `ripaclub/aclman` to your `composer.json`.
```
{
"require": {
"ripaclub/aclman": "v0.1.0"
"ripaclub/aclman": "~0.2.0"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion library/Acl/AclAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Acl;
Expand Down
2 changes: 1 addition & 1 deletion library/Assertion/AssertionAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Assertion;
Expand Down
7 changes: 4 additions & 3 deletions library/Assertion/AssertionManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Assertion;

use Zend\Permissions\Acl\Assertion\AssertionManager;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand All @@ -35,7 +34,9 @@ class AssertionManagerFactory implements FactoryInterface
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config');
$configManager = (isset($config['aclman-assertion-manager'])) ? new Config($config['aclman-assertion-manager']) : null;
$configManager = (isset($config['aclman-assertion-manager'])) ? new Config(
$config['aclman-assertion-manager']
) : null;
return new AssertionManager($configManager);
}
}
2 changes: 1 addition & 1 deletion library/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/InvalidAssertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/InvalidParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/ResourceAlreadyExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/ResourceNotExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/RoleAlreadyExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/RoleNotExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/ServiceNotCreatedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Exception;
Expand Down
2 changes: 1 addition & 1 deletion library/Permission/GenericPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Permission;
Expand Down
4 changes: 2 additions & 2 deletions library/Permission/PermissionCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Permission;
Expand All @@ -16,7 +16,7 @@
trait PermissionCheckTrait
{
/**
* @param $permission
* @param PermissionInterface|array $permission
* @return GenericPermission
* @throws InvalidParameterException
*/
Expand Down
2 changes: 1 addition & 1 deletion library/Permission/PermissionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Permission;
Expand Down
2 changes: 1 addition & 1 deletion library/Resource/ResourceCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Resource;
Expand Down
2 changes: 1 addition & 1 deletion library/Role/RoleAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Role;
Expand Down
2 changes: 1 addition & 1 deletion library/Role/RoleCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Role;
Expand Down
2 changes: 1 addition & 1 deletion library/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Service;
Expand Down
64 changes: 31 additions & 33 deletions library/Service/ServiceAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Service;
Expand All @@ -14,8 +14,10 @@
use AclMan\Resource\ResourceCheckTrait;
use AclMan\Role\RoleCheckTrait;
use AclMan\Storage\StorageAwareTrait;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\Permissions\Acl\Resource;
use Zend\Permissions\Acl\Role;
use Zend\Permissions\Acl\Role\RoleInterface;
use Zend\Stdlib\ArrayUtils;

/**
* Class ServiceAbstract
Expand All @@ -41,19 +43,20 @@ class ServiceAbstract implements ServiceInterface
/**
* Add roles from storage
*
* @param string|Role $role
* @return self
* @param string|RoleInterface $role
* @param string|array|RoleInterface $parents
* @return $this
*/
public function addRole($role)
public function addRole($role, $parents = null)
{
$this->getAcl()->addRole($role);
$this->getAcl()->addRole($role, $parents);
return $this;
}

/**
* Check if exist role
*
* @param string|Role $role
* @param string|RoleInterface $role
* @return bool
*/
public function hasRole($role)
Expand All @@ -70,8 +73,8 @@ public function getRoles()
}

/**
* @param string|Role $role
* @return Role\RoleInterface
* @param string|RoleInterface $role
* @return RoleInterface
*/
public function getRole($role)
{
Expand All @@ -83,66 +86,61 @@ public function getRole($role)
*/
public function loadResource($role = null, $resource = null)
{
$role = ($role instanceof Role\RoleInterface) ? $role->getRoleId() : $role;
$role = ($role instanceof RoleInterface) ? $role->getRoleId() : $role;
$resource = ($resource instanceof Resource\ResourceInterface) ? $resource->getResourceId() : $resource;
// star recursion
// start recursion
if (isset($this->loaded[(string)$role]) && isset($this->loaded[(string)$role][(string)$resource])) {
return;
return true;
}


if (!isset($this->loaded[(string)$role])) {
$this->loaded[(string)$role] = [];
}
$this->loaded[(string)$role][(string)$resource] = true;

if ($role || $resource) {
$this->loadResource();
$parentRoles = [];
if ($role && ($parentRoles = $this->getStorage()->getParentRoles($role))) {
foreach ($parentRoles as $parentRole) {
$this->loadResource($parentRole, $resource);
}
}

if ($role && $resource) {
$this->loadResource(); // ensures loading for ALL_ROLES and ALL_RESOURCES
$this->loadResource(null, $resource);
$this->loadResource($role, null);
}
// end recursion

if ($role && !$this->getAcl()->hasRole($role)) {
$this->getAcl()->addRole($role);
$this->getAcl()->addRole($role, $parentRoles);
}

if ($resource && !$this->getAcl()->hasResource($resource)) {
$this->getAcl()->addResource($resource);
}

$permissions = $this->getStorage()->getPermissions($role, $resource);
//var_dump($permissions);
if (count($permissions) > 0) {
/* @var $permission GenericPermission */
foreach ($permissions as $permission) {
$assert = null;
if ($permission->getAssertion()) {
/** @var $assert AssertionInterface */
$assert = $this->getPluginManager()->get($permission->getAssertion());
}
// When load multiple resource
if ($permission->getResourceId() && !$this->getAcl()->hasResource($permission->getResourceId())) {
$this->getAcl()->addResource($permission->getResourceId());
}

if ($permission->isAllow()) {
$this->getAcl()->allow(
$permission->getRoleId(),
$permission->getResourceId(),
$permission->getPrivilege(),
$assert
);
} else {
$this->getAcl()->deny(
$permission->getRoleId(),
$permission->getResourceId(),
$permission->getPrivilege(),
$assert
);
}
$method = $permission->isAllow() ? 'allow' : 'deny';
$this->getAcl()->{$method}(
$permission->getRoleId(),
$permission->getResourceId(),
$permission->getPrivilege(),
$assert
);
}
return true;
}
Expand Down Expand Up @@ -171,7 +169,7 @@ public function isAllowed($role = null, $resource = null, $privilege = null)
*/
public function setAllowNotFoundResource($allowNotFoundResource)
{
$this->allowNotFoundResource = (boolean) $allowNotFoundResource;
$this->allowNotFoundResource = (boolean)$allowNotFoundResource;
if ($this->allowNotFoundResource) {
$this->getAcl()->allow();
} else {
Expand Down
4 changes: 2 additions & 2 deletions library/Service/ServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Service;

use AclMan\Storage\StorageInterface;
use AclMan\Exception\ServiceNotCreatedException;
use AclMan\Storage\StorageInterface;
use Zend\Permissions\Acl\Acl;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand Down
2 changes: 1 addition & 1 deletion library/Service/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* ACL Manager
*
* @link https://github.com/ripaclub/aclman
* @copyright Copyright (c) 2014, RipaClub
* @copyright Copyright (c) 2015, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Service;
Expand Down
Loading

0 comments on commit 03eaaa3

Please sign in to comment.