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

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ public function setRule($operation, $type, $roles = null, $resources = null,
if (!is_array($resources)) {
if (null === $resources && count($this->resources) > 0) {
$resources = array_keys($this->resources);
// Passing a null resource; make sure "global" permission is also set!
if (!in_array(null, $resources)) {
array_unshift($resources, null);
}
} else {
$resources = array($resources);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Role/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Permissions\Acl\Role;

use Traversable;
use Zend\Permissions\Acl\Exception;

class Registry
Expand All @@ -34,8 +35,8 @@ class Registry
* will have the least priority, and the last parent added will have the
* highest priority.
*
* @param RoleInterface $role
* @param RoleInterface|string|array $parents
* @param RoleInterface $role
* @param RoleInterface|string|array|Traversable $parents
* @throws Exception\InvalidArgumentException
* @return Registry Provides a fluent interface
*/
Expand All @@ -53,7 +54,7 @@ public function add(RoleInterface $role, $parents = null)
$roleParents = array();

if (null !== $parents) {
if (!is_array($parents)) {
if (!is_array($parents) && !$parents instanceof Traversable) {
$parents = array($parents);
}
foreach ($parents as $parent) {
Expand Down
48 changes: 46 additions & 2 deletions test/AclTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ public function testRoleRegistryInherits()
}

/**
* Tests basic Role multiple inheritance
* Tests basic Role multiple inheritance with array
*
* @return void
*/
public function testRoleRegistryInheritsMultiple()
public function testRoleRegistryInheritsMultipleArray()
{
$roleParent1 = new Role\GenericRole('parent1');
$roleParent2 = new Role\GenericRole('parent2');
Expand All @@ -211,6 +211,39 @@ public function testRoleRegistryInheritsMultiple()
$this->assertTrue($roleRegistry->inherits($roleChild, $roleParent2));
}

/**
* Tests basic Role multiple inheritance with traversable object
*
* @return void
*/
public function testRoleRegistryInheritsMultipleTraversable()
{
$roleParent1 = new Role\GenericRole('parent1');
$roleParent2 = new Role\GenericRole('parent2');
$roleChild = new Role\GenericRole('child');
$roleRegistry = new Role\Registry();
$roleRegistry->add($roleParent1)
->add($roleParent2)
->add(
$roleChild,
new \ArrayIterator(array($roleParent1, $roleParent2))
);
$roleChildParents = $roleRegistry->getParents($roleChild);
$this->assertTrue(2 === count($roleChildParents));
$i = 1;
foreach ($roleChildParents as $roleParentId => $roleParent) {
$this->assertTrue("parent$i" === $roleParentId);
$i++;
}
$this->assertTrue($roleRegistry->inherits($roleChild, $roleParent1));
$this->assertTrue($roleRegistry->inherits($roleChild, $roleParent2));
$roleRegistry->remove($roleParent1);
$roleChildParents = $roleRegistry->getParents($roleChild);
$this->assertTrue(1 === count($roleChildParents));
$this->assertTrue(isset($roleChildParents['parent2']));
$this->assertTrue($roleRegistry->inherits($roleChild, $roleParent2));
}

/**
* Ensures that the same Role cannot be registered more than once to the registry
*
Expand Down Expand Up @@ -1339,4 +1372,15 @@ public function testSetRuleWorksWithResourceInterface()

$this->_acl->setRule(Acl\Acl::OP_ADD, Acl\Acl::TYPE_ALLOW, $roleGuest, $resourceFoo);
}

/**
* @group 4226
*/
public function testAllowNullPermissionAfterResourcesExistShouldAllowAllPermissionsForRole()
{
$this->_acl->addRole('admin');
$this->_acl->addResource('newsletter');
$this->_acl->allow('admin');
$this->assertTrue($this->_acl->isAllowed('admin'));
}
}

0 comments on commit d531e0a

Please sign in to comment.