diff --git a/README.md b/README.md index b16a1cf..f21c7f9 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Add `ripaclub/aclman` to your `composer.json`. ``` { "require": { - "ripaclub/aclman": "v0.1.0" + "ripaclub/aclman": "~0.2.0" } } ``` diff --git a/library/Acl/AclAwareTrait.php b/library/Acl/AclAwareTrait.php index 20c7a4f..8530118 100644 --- a/library/Acl/AclAwareTrait.php +++ b/library/Acl/AclAwareTrait.php @@ -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; diff --git a/library/Assertion/AssertionAwareTrait.php b/library/Assertion/AssertionAwareTrait.php index 490a2f0..0d01338 100644 --- a/library/Assertion/AssertionAwareTrait.php +++ b/library/Assertion/AssertionAwareTrait.php @@ -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; diff --git a/library/Assertion/AssertionManagerFactory.php b/library/Assertion/AssertionManagerFactory.php index 4885151..e2dcc35 100644 --- a/library/Assertion/AssertionManagerFactory.php +++ b/library/Assertion/AssertionManagerFactory.php @@ -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; @@ -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); } } diff --git a/library/Exception/ExceptionInterface.php b/library/Exception/ExceptionInterface.php index b1b0bab..f18b80d 100644 --- a/library/Exception/ExceptionInterface.php +++ b/library/Exception/ExceptionInterface.php @@ -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; diff --git a/library/Exception/InvalidAssertException.php b/library/Exception/InvalidAssertException.php index 2b8cf80..165f38f 100644 --- a/library/Exception/InvalidAssertException.php +++ b/library/Exception/InvalidAssertException.php @@ -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; diff --git a/library/Exception/InvalidParameterException.php b/library/Exception/InvalidParameterException.php index 4b98cde..7c83b24 100644 --- a/library/Exception/InvalidParameterException.php +++ b/library/Exception/InvalidParameterException.php @@ -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; diff --git a/library/Exception/ResourceAlreadyExistException.php b/library/Exception/ResourceAlreadyExistException.php index bfcd402..6c1ab81 100644 --- a/library/Exception/ResourceAlreadyExistException.php +++ b/library/Exception/ResourceAlreadyExistException.php @@ -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; diff --git a/library/Exception/ResourceNotExistException.php b/library/Exception/ResourceNotExistException.php index e3233cd..968cfdd 100644 --- a/library/Exception/ResourceNotExistException.php +++ b/library/Exception/ResourceNotExistException.php @@ -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; diff --git a/library/Exception/RoleAlreadyExistException.php b/library/Exception/RoleAlreadyExistException.php index d928229..6a384a6 100644 --- a/library/Exception/RoleAlreadyExistException.php +++ b/library/Exception/RoleAlreadyExistException.php @@ -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; diff --git a/library/Exception/RoleNotExistException.php b/library/Exception/RoleNotExistException.php index 8af830d..7f55ed0 100644 --- a/library/Exception/RoleNotExistException.php +++ b/library/Exception/RoleNotExistException.php @@ -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; diff --git a/library/Exception/ServiceNotCreatedException.php b/library/Exception/ServiceNotCreatedException.php index e8890cb..76b87e5 100644 --- a/library/Exception/ServiceNotCreatedException.php +++ b/library/Exception/ServiceNotCreatedException.php @@ -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; diff --git a/library/Permission/GenericPermission.php b/library/Permission/GenericPermission.php index 469ab19..d915048 100644 --- a/library/Permission/GenericPermission.php +++ b/library/Permission/GenericPermission.php @@ -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; diff --git a/library/Permission/PermissionCheckTrait.php b/library/Permission/PermissionCheckTrait.php index df763e3..54fa647 100644 --- a/library/Permission/PermissionCheckTrait.php +++ b/library/Permission/PermissionCheckTrait.php @@ -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; @@ -16,7 +16,7 @@ trait PermissionCheckTrait { /** - * @param $permission + * @param PermissionInterface|array $permission * @return GenericPermission * @throws InvalidParameterException */ diff --git a/library/Permission/PermissionInterface.php b/library/Permission/PermissionInterface.php index e809544..b949961 100644 --- a/library/Permission/PermissionInterface.php +++ b/library/Permission/PermissionInterface.php @@ -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; diff --git a/library/Resource/ResourceCheckTrait.php b/library/Resource/ResourceCheckTrait.php index e6dd1e9..a975106 100644 --- a/library/Resource/ResourceCheckTrait.php +++ b/library/Resource/ResourceCheckTrait.php @@ -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; diff --git a/library/Role/RoleAwareTrait.php b/library/Role/RoleAwareTrait.php index 3f2ff7a..41f0350 100644 --- a/library/Role/RoleAwareTrait.php +++ b/library/Role/RoleAwareTrait.php @@ -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; diff --git a/library/Role/RoleCheckTrait.php b/library/Role/RoleCheckTrait.php index cfa7717..917bcfe 100644 --- a/library/Role/RoleCheckTrait.php +++ b/library/Role/RoleCheckTrait.php @@ -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; diff --git a/library/Service/Service.php b/library/Service/Service.php index ed73d36..05213e2 100644 --- a/library/Service/Service.php +++ b/library/Service/Service.php @@ -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; diff --git a/library/Service/ServiceAbstract.php b/library/Service/ServiceAbstract.php index 5a14bb8..e2f64d6 100644 --- a/library/Service/ServiceAbstract.php +++ b/library/Service/ServiceAbstract.php @@ -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; @@ -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 @@ -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) @@ -70,8 +73,8 @@ public function getRoles() } /** - * @param string|Role $role - * @return Role\RoleInterface + * @param string|RoleInterface $role + * @return RoleInterface */ public function getRole($role) { @@ -83,31 +86,34 @@ 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)) { @@ -115,12 +121,12 @@ public function loadResource($role = null, $resource = null) } $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 @@ -128,21 +134,13 @@ public function loadResource($role = null, $resource = null) $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; } @@ -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 { diff --git a/library/Service/ServiceFactory.php b/library/Service/ServiceFactory.php index 7c0237d..b21125f 100644 --- a/library/Service/ServiceFactory.php +++ b/library/Service/ServiceFactory.php @@ -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; diff --git a/library/Service/ServiceInterface.php b/library/Service/ServiceInterface.php index 44c7153..6b7b202 100644 --- a/library/Service/ServiceInterface.php +++ b/library/Service/ServiceInterface.php @@ -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; diff --git a/library/Storage/Adapter/ArrayAdapter/ArrayAdapter.php b/library/Storage/Adapter/ArrayAdapter/ArrayAdapter.php index b61e631..68e62be 100644 --- a/library/Storage/Adapter/ArrayAdapter/ArrayAdapter.php +++ b/library/Storage/Adapter/ArrayAdapter/ArrayAdapter.php @@ -3,12 +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\Storage\Adapter\ArrayAdapter; use AclMan\Exception\InvalidParameterException; +use AclMan\Exception\ResourceAlreadyExistException; use AclMan\Exception\ResourceNotExistException; use AclMan\Exception\RoleAlreadyExistException; use AclMan\Exception\RoleNotExistException; @@ -17,7 +18,6 @@ use AclMan\Permission\PermissionInterface; use AclMan\Resource\ResourceCheckTrait; use AclMan\Role\RoleCheckTrait; -use AclMan\Exception\ResourceAlreadyExistException; use AclMan\Storage\StorageInterface; use Zend\Permissions\Acl\Resource\GenericResource; use Zend\Permissions\Acl\Resource\ResourceInterface; @@ -77,6 +77,10 @@ public function __construct($config = null) $this->addPermission($perm); } } else { + if (!is_array($permission)) { + $permission = []; + } + $permission['role'] = $role; $permission['resource'] = $resource; $this->addPermission($permission); @@ -119,10 +123,12 @@ public function addRole($role, array $parents = []) $role = $this->checkRole($role); if ($this->hasRole($role)) { - throw new RoleAlreadyExistException(sprintf( - 'Role %s already stored', - $role->getRoleId() - )); + throw new RoleAlreadyExistException( + sprintf( + 'Role %s already stored', + $role->getRoleId() + ) + ); } $this->roles[$role->getRoleId()][self::NODE_PARENTS_ROLE] = $this->extractRoleParents($parents); @@ -157,6 +163,8 @@ public function hasRole($role) } /** + * FIXME: Review, it is useful? Upserts parents? + * * @param $role * @param array $parents * @return bool @@ -173,36 +181,37 @@ public function addParentRoles($role, array $parents) if ($this->hasRole($parentId)) { array_push($this->roles[$roleId][self::NODE_PARENTS_ROLE], $parentId); } else { - throw new RoleNotExistException(sprintf( - 'Role parent %s not stored', - $roleId - )); + throw new RoleNotExistException( + sprintf( + 'Role parent %s not stored', + $roleId + ) + ); } } return true; } else { - throw new RoleNotExistException(sprintf( - 'Role %s not stored', - $roleId - )); + throw new RoleNotExistException( + sprintf( + 'Role %s not stored', + $roleId + ) + ); } } - /** * @param $role * @return array - * @throws RoleNotExistException */ public function getParentRoles($role) { $role = $this->checkRole($role); $roleId = $role->getRoleId(); - if (array_key_exists($roleId, $this->roles)) { - return $this->roles[$roleId][self::NODE_PARENTS_ROLE]; - } else { - throw new RoleNotExistException(sprintf('Role %s not stored', $roleId)); + if (!array_key_exists($roleId, $this->roles)) { + return []; } + return $this->roles[$roleId][self::NODE_PARENTS_ROLE]; } /** @@ -235,8 +244,8 @@ public function getResources() /** * @param array $resources - * @return self - * @throws InvalidParameterException + * @return $this + * @throws ResourceAlreadyExistException */ public function addResources(array $resources) { @@ -256,10 +265,12 @@ public function addResource($resource) { $resource = $this->checkResource($resource); if ($resource && $this->hasResource($resource)) { - throw new ResourceAlreadyExistException(sprintf( - 'Resource %s already stored', - $resource->getResourceId() - )); + throw new ResourceAlreadyExistException( + sprintf( + 'Resource %s already stored', + $resource->getResourceId() + ) + ); } $this->resources[$resource->getResourceId()] = []; @@ -290,26 +301,30 @@ public function getPermissions($role = null, $resource = null) $result = []; if ($resource) { $resource = $this->checkResource($resource); - if(isset($this->permission[$role->getRoleId()][self::NODE_RESOURCES][$resource->getResourceId()][self::NODE_PERMISSION])) { + if (isset($this->permission[$role->getRoleId()][self::NODE_RESOURCES][$resource->getResourceId()][self::NODE_PERMISSION])) { $listPermission = $this->permission[$role->getRoleId()][self::NODE_RESOURCES][$resource->getResourceId()][self::NODE_PERMISSION]; foreach ($listPermission as $permission) { - - $permission['role'] = ($role->getRoleId() == StorageInterface::ALL_ROLES) ? null : $role->getRoleId(); - $permission['resource'] = ($resource->getResourceId() == StorageInterface::ALL_RESOURCES) ? null : $resource->getResourceId(); + $permission['role'] = ($role->getRoleId() == StorageInterface::ALL_ROLES) ? + null : + $role->getRoleId(); + $permission['resource'] = ($resource->getResourceId() == StorageInterface::ALL_RESOURCES) ? + null : + $resource->getResourceId(); $obj = new GenericPermission($permission); array_push($result, $obj); } - } } else { - if(isset($this->permission[$role->getRoleId()][self::NODE_RESOURCES])) { + if (isset($this->permission[$role->getRoleId()][self::NODE_RESOURCES])) { $listResource = $this->permission[$role->getRoleId()][self::NODE_RESOURCES]; foreach ($listResource as $keyResource => $listPermission) { foreach ($listPermission[self::NODE_PERMISSION] as $permission) { - - $permission['role'] = ($role->getRoleId() == StorageInterface::ALL_ROLES) ? null : $role->getRoleId(); - $permission['resource'] = ($keyResource == StorageInterface::ALL_RESOURCES) ? null : $keyResource; + $permission['role'] = ($role->getRoleId() == StorageInterface::ALL_ROLES) ? + null : + $role->getRoleId(); + $permission['resource'] = ($keyResource == StorageInterface::ALL_RESOURCES) ? + null : $keyResource; $obj = new GenericPermission($permission); array_push($result, $obj); @@ -321,7 +336,7 @@ public function getPermissions($role = null, $resource = null) } /** - * @param PermissionInterface $permission + * @param PermissionInterface|array $permission * @return $this|bool */ public function addPermission($permission) @@ -335,8 +350,8 @@ public function addPermission($permission) $newPermission = [ 'assert' => $permission->getAssertion(), - 'allow' => $permission->isAllow(), - 'privilege' => $permission->getPrivilege() + 'allow' => $permission->isAllow(), + 'privilege' => $permission->getPrivilege() ]; $roleId = $role->getRoleId(); @@ -348,7 +363,7 @@ public function addPermission($permission) /** * @param array $permissions - * @return self + * @return $this */ public function addPermissions(array $permissions) { @@ -385,7 +400,11 @@ protected function getResourcePermission(GenericPermission $permission) { if ($permission->getResourceId()) { // Check if resource is already stored - if ($permission->getResourceId() && !$this->hasResource(new GenericResource($permission->getResourceId()))) { + if ( + $permission->getResourceId() && !$this->hasResource( + new GenericResource($permission->getResourceId()) + ) + ) { $this->addResource($permission->getResourceId()); } $resource = new GenericResource($permission->getResourceId()); diff --git a/library/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactory.php b/library/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactory.php index fe2ec6c..0224791 100644 --- a/library/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactory.php +++ b/library/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactory.php @@ -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\Storage\Adapter\ArrayAdapter; @@ -19,7 +19,7 @@ class ArrayAdapterAbstractServiceFactory implements AbstractFactoryInterface /** * @var string */ - protected $configKey = 'aclManArrayAdapterStorage'; // FIXME: change config node name + protected $configKey = 'aclManArrayAdapterStorage'; // FIXME: change config node name (0.3.x) /** * @var array @@ -59,8 +59,8 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator */ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - $config = $this->getConfig($serviceLocator)[$requestedName]; - return new ArrayAdapter($config); + $config = $this->getConfig($serviceLocator)[$requestedName]; + return new ArrayAdapter($config); } /** diff --git a/library/Storage/StorageAwareTrait.php b/library/Storage/StorageAwareTrait.php index 6bbb016..c3a5a0f 100644 --- a/library/Storage/StorageAwareTrait.php +++ b/library/Storage/StorageAwareTrait.php @@ -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\Storage; diff --git a/library/Storage/StorageFactory.php b/library/Storage/StorageFactory.php index d41224d..0565824 100644 --- a/library/Storage/StorageFactory.php +++ b/library/Storage/StorageFactory.php @@ -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\Storage; @@ -20,7 +20,7 @@ class StorageFactory implements AbstractFactoryInterface * Config Key * @var string */ - protected $configKey = 'aclman_storage'; // TODO: check config node + protected $configKey = 'aclman_storage'; /** * Config @@ -107,5 +107,4 @@ protected function getConfig(ServiceLocatorInterface $serviceLocator) $this->config = $config[$this->configKey]; return $this->config; } - } diff --git a/library/Storage/StorageInterface.php b/library/Storage/StorageInterface.php index 031220a..2fb86c3 100644 --- a/library/Storage/StorageInterface.php +++ b/library/Storage/StorageInterface.php @@ -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\Storage; @@ -24,17 +24,16 @@ interface StorageInterface const ALL_ROLES = ''; const ALL_RESOURCES = ''; - //const ALL_PRIVILEGES = '#privileges#'; /** - * @param $role + * @param string|RoleInterface $role * @param array $parents * @return bool */ public function addParentRoles($role, array $parents); /** - * @param $role + * @param string|RoleInterface $role * @return array */ public function getParentRoles($role); @@ -45,14 +44,14 @@ public function getParentRoles($role); public function getRoles(); /** - * @param RoleInterface|string $role + * @param string|RoleInterface $role * @param array $parents * @return $this */ public function addRole($role, array $parents = []); /** - * @param RoleInterface|string $role + * @param string|RoleInterface $role * @return bool */ public function hasRole($role); @@ -63,20 +62,20 @@ public function hasRole($role); public function getResources(); /** - * @param ResourceInterface|string $resource + * @param string|RoleInterface $resource * @return bool */ public function addResource($resource); /** - * @param ResourceInterface|string $resource + * @param string|RoleInterface $resource * @return bool */ public function hasResource($resource); /** - * @param null $role - * @param null $resource + * @param null|string|RoleInterface $role + * @param null|string|ResourceInterface $resource * @return array */ public function getPermissions($role = null, $resource = null); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2353061..331c02f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,11 @@ library + + + integration + + diff --git a/tests/Acl/AclAwareTraitTest.php b/tests/Acl/AclAwareTraitTest.php index 75886ba..a48932b 100644 --- a/tests/Acl/AclAwareTraitTest.php +++ b/tests/Acl/AclAwareTraitTest.php @@ -3,12 +3,11 @@ * 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 AclManTest\Acl; -use AclMan\Acl\AclAwareTrait; use AclManTest\AclManTestCase; /** diff --git a/tests/AclManTestCase.php b/tests/AclManTestCase.php index 022a02c..2d54956 100644 --- a/tests/AclManTestCase.php +++ b/tests/AclManTestCase.php @@ -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 AclManTest; diff --git a/tests/Assertion/AssertionAwareTraitTest.php b/tests/Assertion/AssertionAwareTraitTest.php index e03f35d..b47de81 100644 --- a/tests/Assertion/AssertionAwareTraitTest.php +++ b/tests/Assertion/AssertionAwareTraitTest.php @@ -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 AclManTest\Assertion; diff --git a/tests/Assertion/AssertionManagerFactoryTest.php b/tests/Assertion/AssertionManagerFactoryTest.php index a38a64c..5609a0e 100644 --- a/tests/Assertion/AssertionManagerFactoryTest.php +++ b/tests/Assertion/AssertionManagerFactoryTest.php @@ -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 AclManTest\Assertion; use AclManTest\AclManTestCase; -use AclManTest\Assertion\TestAsset\MockAssertionPluginManager; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\Permissions\Acl\Assertion\AssertionManager; use Zend\ServiceManager; @@ -52,7 +51,8 @@ public function testAssetPluginManagerConfig() 'assertManager' => 'AclMan\Assertion\AssertionManagerFactory', ], 'aclman-assertion-manager' => [ - 'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1' => 'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1', + 'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1' => + 'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1', 'invokables' => [ 'assert' => 'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1', ] @@ -77,5 +77,4 @@ public function testAssetPluginManagerConfig() $pluginManager->get('assert') ); } - } diff --git a/tests/Assertion/TestAsset/Assertion/MockAssertion1.php b/tests/Assertion/TestAsset/Assertion/MockAssertion1.php index 05dae5a..8431511 100644 --- a/tests/Assertion/TestAsset/Assertion/MockAssertion1.php +++ b/tests/Assertion/TestAsset/Assertion/MockAssertion1.php @@ -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 AclManTest\Assertion\TestAsset\Assertion; @@ -22,9 +22,11 @@ class MockAssertion1 implements AssertionInterface, ServiceLocatorAwareInterface { use ServiceLocatorAwareTrait; + /** + * {@inheritdoc} + */ public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null) { // TODO: implement assert() method. } - } diff --git a/tests/Assertion/TestAsset/Assertion/MockWrongAssertion.php b/tests/Assertion/TestAsset/Assertion/MockWrongAssertion.php index 4ed7fbf..aef206a 100644 --- a/tests/Assertion/TestAsset/Assertion/MockWrongAssertion.php +++ b/tests/Assertion/TestAsset/Assertion/MockWrongAssertion.php @@ -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 AclManTest\Assertion\TestAsset\Assertion; diff --git a/tests/Integration/Service/ServiceAbstractTest.php b/tests/Integration/Service/ServiceAbstractTest.php index a35a543..c5baac0 100644 --- a/tests/Integration/Service/ServiceAbstractTest.php +++ b/tests/Integration/Service/ServiceAbstractTest.php @@ -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 AclManTest\Integration\Service; @@ -11,18 +11,17 @@ use AclMan\Service\Service; use AclMan\Storage\StorageInterface; use AclManTest\AclManTestCase; -use AclMan\Storage\Adapter\ArrayAdapter\ArrayAdapter; use AclManTest\Integration\Service\TestAsset\Assertion\Assertion1; use AclManTest\Integration\Service\TestAsset\Assertion\Assertion2; -use Zend\Permissions\Acl\Acl; use Zend\Mvc\Service\ServiceManagerConfig; +use Zend\Permissions\Acl\Acl; use Zend\Permissions\Acl\Assertion\AssertionManager; use Zend\ServiceManager; /** * Class ServiceAbstractTest * - * @grop integration + * @group integration */ class ServiceAbstractTest extends AclManTestCase { @@ -56,8 +55,14 @@ public function setUp() 'view' ] ] - ] - ] + ], + 'resource3' => [ + [ + 'allow' => true, + 'privileges' => ['PUT'] + ] + ], + ], ], 'role2' => [ 'parents' => [ @@ -148,6 +153,36 @@ public function setUp() ] ] ], + 'AclStorageAllRolesAllResources' => [ + 'roles' => [ + StorageInterface::ALL_ROLES => [ + 'resources' => [ + StorageInterface::ALL_RESOURCES => [ + [ + 'allow' => true, + ] + ] + ] + ], + ] + ], + + 'AclStorageAllRolesAllResourcesWithPrivilege' => [ + 'roles' => [ + StorageInterface::ALL_ROLES => [ + 'resources' => [ + StorageInterface::ALL_RESOURCES => [ + [ + 'allow' => true, + 'privileges' => [ + 'view' + ] + ] + ] + ] + ], + ] + ], ], 'aclman_services' => [ 'AclService' => [ @@ -166,6 +201,14 @@ public function setUp() 'storage' => 'AclStorage4', 'plugin_manager' => 'assertManager', ], + 'AclServiceAllRolesAllResources' => [ + 'storage' => 'AclStorageAllRolesAllResources', + 'plugin_manager' => 'assertManager', + ], + 'AclServiceAllRolesAllResourcesWithPrivilege' => [ + 'storage' => 'AclStorageAllRolesAllResourcesWithPrivilege', + 'plugin_manager' => 'assertManager', + ], ], 'aclman-assertion-manager' => [ 'invokables' => [ @@ -197,24 +240,26 @@ public function setUp() } - public function _testHasService() + public function testHasService() { $this->assertTrue($this->serviceManager->has('AclService')); } - public function _testIsAllowed() + public function testIsAllowed() { - $acl = new Acl(); + $acl = new Acl; $acl->addRole('role1'); $acl->addResource('resource1'); $acl->allow('role1', 'resource1', 'view'); $acl->addResource('resource2'); - $acl->allow('role1', 'resource2', 'view', new Assertion1()); + $acl->addResource('resource3'); + $acl->allow('role1', 'resource2', 'view', new Assertion1); + $acl->allow('role1', 'resource3', 'PUT'); $acl->addRole('role2', ['role1']); $acl->allow('role2', 'resource1', 'add'); $acl->deny('role2', 'resource1', 'view'); $acl->allow('role2', 'resource2', 'add'); - $acl->allow('role2', 'resource2', 'view', new Assertion2()); + $acl->allow('role2', 'resource2', 'view', new Assertion2); /** @var $service Service */ $service = $this->serviceManager->get('AclService'); @@ -236,11 +281,15 @@ public function _testIsAllowed() $acl->isAllowed('role2', 'resource1', 'add'), $service->isAllowed('role2', 'resource1', 'add') ); + $this->assertSame( + $acl->isAllowed('role2', 'resource3', 'PUT'), + $service->isAllowed('role2', 'resource3', 'PUT') + ); } - public function _testAllRolesIsAllowed() + public function testAllRolesAreAllowed() { - $acl = new Acl(); + $acl = new Acl; $acl->addRole('role1'); $acl->addRole('role2'); $acl->addRole('role3'); @@ -323,7 +372,7 @@ public function testAllResourceIsAllowed() ); } - public function _testOverrideIsAllowed() + public function testOverrideIsAllowed() { $acl = new Acl(); $acl->addRole('role1'); @@ -352,4 +401,30 @@ public function _testOverrideIsAllowed() $service->isAllowed('role1', 'resource2', 'view') ); } + + public function testAllRolesAllResources() + { + $service = $this->serviceManager->get('AclServiceAllRolesAllResources'); + + + $this->assertTrue($service->isAllowed('role1', 'resource1', 'add')); + $this->assertTrue($service->isAllowed(null, 'resource1', 'add')); + $this->assertTrue($service->isAllowed('role1', null, 'add')); + $this->assertTrue($service->isAllowed('role1', 'resource1', null)); + $this->assertTrue($service->isAllowed(null, null, 'add')); + $this->assertTrue($service->isAllowed('role1', null, null)); + $this->assertTrue($service->isAllowed(null, null, null)); + + + + $service = $this->serviceManager->get('AclServiceAllRolesAllResourcesWithPrivilege'); + + $this->assertTrue($service->isAllowed('role1', 'resource1', 'view')); + $this->assertTrue($service->isAllowed(null, 'resource1', 'view')); + $this->assertTrue($service->isAllowed('role1', null, 'view')); + $this->assertFalse($service->isAllowed('role1', 'resource1', null)); + $this->assertTrue($service->isAllowed(null, null, 'view')); + $this->assertFalse($service->isAllowed('role1', null, null)); + $this->assertFalse($service->isAllowed(null, null, null)); + } } diff --git a/tests/Integration/Service/TestAsset/Assertion/Assertion1.php b/tests/Integration/Service/TestAsset/Assertion/Assertion1.php index 21b2c4a..ee8910f 100644 --- a/tests/Integration/Service/TestAsset/Assertion/Assertion1.php +++ b/tests/Integration/Service/TestAsset/Assertion/Assertion1.php @@ -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 AclManTest\Integration\Service\TestAsset\Assertion; diff --git a/tests/Integration/Service/TestAsset/Assertion/Assertion2.php b/tests/Integration/Service/TestAsset/Assertion/Assertion2.php index 920c5bd..ec85b76 100644 --- a/tests/Integration/Service/TestAsset/Assertion/Assertion2.php +++ b/tests/Integration/Service/TestAsset/Assertion/Assertion2.php @@ -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 AclManTest\Integration\Service\TestAsset\Assertion; diff --git a/tests/Integration/Service/TestAsset/MockAssertionPluginManager.php b/tests/Integration/Service/TestAsset/MockAssertionPluginManager.php index 4779409..ee7697d 100644 --- a/tests/Integration/Service/TestAsset/MockAssertionPluginManager.php +++ b/tests/Integration/Service/TestAsset/MockAssertionPluginManager.php @@ -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 AclManTest\Integration\Service\TestAsset; diff --git a/tests/Permission/GenericPermissionTest.php b/tests/Permission/GenericPermissionTest.php index 6696930..50d0b87 100644 --- a/tests/Permission/GenericPermissionTest.php +++ b/tests/Permission/GenericPermissionTest.php @@ -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 AclManTest\Permission; @@ -54,7 +54,6 @@ public function testConstruct() $this->assertFalse($this->permission->isAllow()); $this->assertSame('test', $this->permission->getAssertion()); $this->assertSame('add', $this->permission->getPrivilege()); - } public function testSetterRoleResource() diff --git a/tests/Resource/ResourceCheckTraitTest.php b/tests/Resource/ResourceCheckTraitTest.php index 3ea2154..47d0063 100644 --- a/tests/Resource/ResourceCheckTraitTest.php +++ b/tests/Resource/ResourceCheckTraitTest.php @@ -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 AclManTest\Role; @@ -34,7 +34,10 @@ public function testcheckResource() $this->assertNull($reflMethod->invoke($this->mockTrait)); - $this->assertInstanceOf('Zend\Permissions\Acl\Resource\ResourceInterface', $reflMethod->invoke($this->mockTrait, 'role')); + $this->assertInstanceOf( + 'Zend\Permissions\Acl\Resource\ResourceInterface', + $reflMethod->invoke($this->mockTrait, 'role') + ); } /** diff --git a/tests/Role/RoleAwareTraitTest.php b/tests/Role/RoleAwareTraitTest.php index 109b245..d174782 100644 --- a/tests/Role/RoleAwareTraitTest.php +++ b/tests/Role/RoleAwareTraitTest.php @@ -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 AclManTest\Role; diff --git a/tests/Role/RoleCheckTraitTest.php b/tests/Role/RoleCheckTraitTest.php index 5dc43f4..1e809da 100644 --- a/tests/Role/RoleCheckTraitTest.php +++ b/tests/Role/RoleCheckTraitTest.php @@ -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 AclManTest\Role; @@ -34,7 +34,10 @@ public function testCheckRole() $this->assertNull($reflMethod->invoke($this->mockTrait)); - $this->assertInstanceOf('Zend\Permissions\Acl\Role\RoleInterface', $reflMethod->invoke($this->mockTrait, 'role')); + $this->assertInstanceOf( + 'Zend\Permissions\Acl\Role\RoleInterface', + $reflMethod->invoke($this->mockTrait, 'role') + ); } /** diff --git a/tests/Service/ServiceAbstractTest.php b/tests/Service/ServiceAbstractTest.php index 856d11c..267ae8f 100644 --- a/tests/Service/ServiceAbstractTest.php +++ b/tests/Service/ServiceAbstractTest.php @@ -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 AclManTest\Service; @@ -11,7 +11,6 @@ use AclMan\Service\Service; use AclManTest\AclManTestCase; use AclManTest\Assertion\TestAsset\Assertion\MockAssertion1; -use AclManTest\Assertion\TestAsset\MockAssertionPluginManager; use Zend\Permissions\Acl\Acl; use Zend\Permissions\Acl\Assertion\AssertionManager; @@ -27,9 +26,8 @@ class ServiceAbstractTest extends AclManTestCase public function setUp() { - $this->service = new Service(); - - $this->service->setAcl(new Acl()); + $this->service = new Service; + $this->service->setAcl(new Acl); } public function testHasRole() @@ -42,6 +40,12 @@ public function testAddRole() $this->assertSame($this->service, $this->service->addRole('role1')); } + public function testAddRoleWithParent() + { + $this->service->addRole('role1'); + $this->assertSame($this->service, $this->service->addRole('role2', 'role1')); + } + /** * @depends testAddRole */ @@ -70,7 +74,7 @@ public function testLoadResourceNotFound() { $mockStorage = $this->getMockBuilder('AclMan\Storage\Adapter\ArrayAdapter\ArrayAdapter') ->disableOriginalConstructor() - ->setMethods(['hasResource', 'getPermissions']) + ->setMethods(['hasResource', 'getPermissions', 'getParentRoles']) ->getMock(); $mockStorage->expects($this->any()) @@ -95,7 +99,7 @@ public function testLoadResourceAllow() $mockStorage = $this->getMockBuilder('AclMan\Storage\Adapter\ArrayAdapter\ArrayAdapter') ->disableOriginalConstructor() - ->setMethods(['hasResource', 'getPermissions', 'hasRole']) + ->setMethods(['hasResource', 'getPermissions', 'hasRole', 'getParentRoles']) ->getMock(); $mockStorage->expects($this->any()) @@ -145,7 +149,7 @@ public function testLoadResourceDeny() $mockStorage = $this->getMockBuilder('AclMan\Storage\Adapter\ArrayAdapter\ArrayAdapter') ->disableOriginalConstructor() - ->setMethods(['hasResource', 'getPermissions', 'hasRole']) + ->setMethods(['hasResource', 'getPermissions', 'hasRole', 'getParentRoles']) ->getMock(); $mockStorage->expects($this->any()) @@ -199,7 +203,7 @@ public function testLoadResourceAssert() $mockStorage = $this->getMockBuilder('AclMan\Storage\Adapter\ArrayAdapter\ArrayAdapter') ->disableOriginalConstructor() - ->setMethods(['hasResource', 'getPermissions', 'hasRole']) + ->setMethods(['hasResource', 'getPermissions', 'hasRole', 'getParentRoles']) ->getMock(); $mockStorage->expects($this->any()) @@ -258,17 +262,21 @@ public function testHasResource() public function testIsAllowed() { $this->service->addRole('role1'); - $this->service->addRole('role2'); + $this->service->addRole('role2', 'role1'); $mockStorage = $this->getMockBuilder('AclMan\Storage\Adapter\ArrayAdapter\ArrayAdapter') ->disableOriginalConstructor() - ->setMethods(['hasResource', 'getPermissions', 'hasRole']) + ->setMethods(['hasResource', 'getPermissions', 'hasRole', 'getParentRoles']) ->getMock(); $mockStorage->expects($this->any()) ->method('hasResource') ->will($this->returnValue(true)); + $mockStorage->expects($this->any()) + ->method('getParentRoles') + ->will($this->returnValue(['role1'])); + $permission = $this->getMockBuilder('AclMan\Permission\GenericPermission') ->disableOriginalConstructor() ->setMethods(['getAssertion', 'isAllow', 'getPrivilege', 'getResourceId', 'getRoleId']) @@ -306,7 +314,7 @@ public function testIsAllowed() $this->service->setStorage($mockStorage); - $this->assertFalse($this->service->isAllowed('role2', 'resource1', 'view')); + $this->assertTrue($this->service->isAllowed('role2', 'resource1', 'view')); $this->assertTrue($this->service->isAllowed('role1', 'resource1', 'view')); } } diff --git a/tests/Service/ServiceFactoryTest.php b/tests/Service/ServiceFactoryTest.php index 85e5695..87b89b4 100644 --- a/tests/Service/ServiceFactoryTest.php +++ b/tests/Service/ServiceFactoryTest.php @@ -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 AclManTest\Service; diff --git a/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactoryTest.php b/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactoryTest.php index 74f0da9..6317490 100644 --- a/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactoryTest.php +++ b/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterAbstractServiceFactoryTest.php @@ -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 AclManTest\Storage\Adapter\ArrayAdapter; use AclManTest\AclManTestCase; -use Zend\Permissions\Acl\Role\GenericRole; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager; @@ -25,7 +24,6 @@ class ArrayAdapterAbstractServiceFactoryTest extends AclManTestCase public function setUp() { - $config = [ 'aclManArrayAdapterStorage' => [ 'ArrayStorage' => [ diff --git a/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterTest.php b/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterTest.php index 7a00099..36fd2b4 100644 --- a/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterTest.php +++ b/tests/Storage/Adapter/ArrayAdapter/ArrayAdapterTest.php @@ -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 AclManTest\Storage\Adapter\ArrayAdapter; @@ -193,12 +193,9 @@ public function testGetParentRoles() $this->assertCount(1, $this->adapter->getParentRoles('role2')); } - /** - * @expectedException \AclMan\Exception\RoleNotExistException - */ - public function testGetParentRolesException() + public function testGetParentRolesEmptyArray() { - $this->adapter->getParentRoles('role1'); + $this->assertEmpty($this->adapter->getParentRoles('role1')); } /** @@ -470,7 +467,7 @@ public function testAddPermissions() /** * @depends testAddPermission */ - public function testGetPermission() + public function testGetPermissions() { $this->adapter->addRoles(['role1']); $this->assertCount(0, $this->adapter->getPermissions('role1')); @@ -567,7 +564,7 @@ public function testConstructNestedPrivileges() $this->assertTrue($adapter->hasResource('resource1')); $this->assertTrue($adapter->hasResource('resource2')); - $role1Privileges = $adapter->getPermissions('role1'); + $role1Privileges = $adapter->getPermissions('role1'); $this->assertCount(2, $role1Privileges); @@ -585,7 +582,7 @@ public function testConstructNestedPrivileges() $this->assertTrue($permission->isAllow()); $this->assertNull($permission->getAssertion()); - $role2Privileges = $adapter->getPermissions('role2'); + $role2Privileges = $adapter->getPermissions('role2'); $this->assertCount(2, $role2Privileges); $permission = $role2Privileges[0]; diff --git a/tests/Storage/StorageAwareTraitTest.php b/tests/Storage/StorageAwareTraitTest.php index d0d78c4..bc4414a 100644 --- a/tests/Storage/StorageAwareTraitTest.php +++ b/tests/Storage/StorageAwareTraitTest.php @@ -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 AclManTest\Storage; diff --git a/tests/Storage/StorageFactoryTest.php b/tests/Storage/StorageFactoryTest.php index 79ba00a..7965a1c 100644 --- a/tests/Storage/StorageFactoryTest.php +++ b/tests/Storage/StorageFactoryTest.php @@ -3,11 +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 AclManTest\Storage; +use AclMan\Storage\StorageInterface; use AclManTest\AclManTestCase; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager; @@ -50,7 +51,18 @@ public function setUp() ] ] ] - ] + ], + 'AclStorage2' => [ + 'roles' => [ + StorageInterface::ALL_ROLES => [ + 'resources' => [ + StorageInterface::ALL_RESOURCES => [ + 'allow' => true, + ], + ], + ], + ] + ], ], ]; @@ -70,6 +82,7 @@ public function testHasService() $serviceLocator = $this->serviceManager; $this->assertTrue($serviceLocator->has('AclStorage')); $this->assertTrue($serviceLocator->has('AclStorage1')); + $this->assertTrue($serviceLocator->has('AclStorage2')); } public function testGetService() @@ -77,6 +90,7 @@ public function testGetService() $serviceLocator = $this->serviceManager; $this->assertInstanceOf('AclMan\Storage\StorageInterface', $serviceLocator->get('AclStorage')); $this->assertInstanceOf('AclMan\Storage\StorageInterface', $serviceLocator->get('AclStorage1')); + $this->assertInstanceOf('AclMan\Storage\StorageInterface', $serviceLocator->get('AclStorage2')); } public function testHasServiceWithoutConfig() diff --git a/tests/Storage/TestAsset/MockAdapter.php b/tests/Storage/TestAsset/MockAdapter.php index 836c81c..3376b57 100644 --- a/tests/Storage/TestAsset/MockAdapter.php +++ b/tests/Storage/TestAsset/MockAdapter.php @@ -3,15 +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 AclManTest\Storage\TestAsset; -use AclMan\Permission\PermissionInterface; use AclMan\Storage\StorageInterface; -use Zend\Permissions\Acl\Resource\ResourceInterface; -use Zend\Permissions\Acl\Role\RoleInterface; /** * Class MockAdapter @@ -97,5 +94,4 @@ public function addPermission($permission) { // TODO: implement addPermission() method. } - } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c13a390..fa7914f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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 */ chdir(__DIR__);