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

Commit

Permalink
Merge pull request #5973 in master
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w committed Mar 21, 2014
2 parents 2977bc9 + b5c9939 commit 2127acd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
8 changes: 7 additions & 1 deletion library/Zend/Permissions/Rbac/AbstractIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@

abstract class AbstractIterator implements RecursiveIterator
{
/**
* @var int
*/
protected $index = 0;
/**
* @var array
*/
protected $children = array();

/**
Expand Down Expand Up @@ -42,7 +48,7 @@ public function next()
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return scalar scalar on success, or null on failure.
* @return int|null scalar on success, or null on failure.
*/
public function key()
{
Expand Down
10 changes: 8 additions & 2 deletions library/Zend/Permissions/Rbac/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ public function getRole($objectOrName)
);
}

if (is_object($objectOrName)) {
$requiredRole = $objectOrName->getName();
} else {
$requiredRole = $objectOrName;
}

$it = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($it as $leaf) {
/* @var RoleInterface $leaf */
if ((is_string($objectOrName) && $leaf->getName() == $objectOrName) || $leaf == $objectOrName) {
/** @var RoleInterface $leaf */
if ($leaf->getName() == $requiredRole) {
return $leaf;
}
}
Expand Down
20 changes: 17 additions & 3 deletions tests/ZendTest/Permissions/Rbac/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,30 @@ public function testIsGrantedChildRoles()
$this->assertEquals(false, $this->rbac->isGranted('bar', 'can.baz'));
}

/**
* @covers Zend\Permissions\Rbac\Rbac::hasRole()
*/
public function testHasRole()
{
$foo = new Rbac\Role('foo');
$snafu = new TestAsset\RoleTest('snafu');

$this->rbac->addRole('bar');
$this->rbac->addRole($foo);
$this->rbac->addRole('snafu');

// check that the container has the same object $foo
$this->assertTrue($this->rbac->hasRole($foo));

// check that the container has the same string "bar"
$this->assertTrue($this->rbac->hasRole('bar'));

// check that the container do not have the string "baz"
$this->assertFalse($this->rbac->hasRole('baz'));

$this->assertEquals(true, $this->rbac->hasRole($foo));
$this->assertEquals(true, $this->rbac->hasRole('bar'));
$this->assertEquals(false, $this->rbac->hasRole('baz'));
// check that we can compare two different objects with same name
$this->assertNotEquals($this->rbac->getRole('snafu'), $snafu);
$this->assertTrue($this->rbac->hasRole($snafu));
}

public function testAddRoleFromString()
Expand Down
23 changes: 23 additions & 0 deletions tests/ZendTest/Permissions/Rbac/TestAsset/RoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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 ZendTest\Permissions\Rbac\TestAsset;

use Zend\Permissions\Rbac\AbstractRole;

class RoleTest extends AbstractRole
{
/**
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}
}

0 comments on commit 2127acd

Please sign in to comment.