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 https://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 8 changed files with 64 additions and 14 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
}
},
"require": {
"php": ">=5.3.3"
"php": ">=5.3.23"
},
"extra": {
"branch-alias": {
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
"dev-master": "2.3-dev",
"dev-develop": "2.4-dev"
}
},
"autoload-dev": {
Expand Down
8 changes: 7 additions & 1 deletion src/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
12 changes: 10 additions & 2 deletions src/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,22 @@ 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) {
if ((is_string($objectOrName) && $leaf->getName() == $objectOrName) || $leaf == $objectOrName) {
/** @var RoleInterface $leaf */
if ($leaf->getName() == $requiredRole) {
return $leaf;
}
}

throw new Exception\InvalidArgumentException(sprintf(
'No child with name "%s" could be found',
'No role with name "%s" could be found',
is_object($objectOrName) ? $objectOrName->getName() : $objectOrName
));
}
Expand All @@ -127,6 +134,7 @@ public function getRole($objectOrName)
* @param RoleInterface|string $role
* @param string $permission
* @param AssertionInterface|Callable|null $assert
* @throws Exception\InvalidArgumentException
* @return bool
*/
public function isGranted($role, $permission, $assert = null)
Expand Down
23 changes: 18 additions & 5 deletions test/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @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;

use Zend\Permissions\Rbac;
use ZendTest\Permissions\Rbac\TestAsset;

/**
* @group Zend_Rbac
Expand Down Expand Up @@ -83,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
2 changes: 1 addition & 1 deletion test/TestAsset/RoleMustMatchAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
23 changes: 23 additions & 0 deletions test/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;
}
}
2 changes: 1 addition & 1 deletion test/TestAsset/SimpleFalseAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
2 changes: 1 addition & 1 deletion test/TestAsset/SimpleTrueAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down

0 comments on commit 9a952d8

Please sign in to comment.