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

[hotfix/45] - recursive check for roles #46

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions src/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Permissions\Rbac;

use ZendTest\Permissions\Rbac\RoleTest;

class Rbac
{
/**
Expand Down Expand Up @@ -83,12 +85,71 @@ public function hasRole($role) : bool
}

if (is_string($role)) {
return isset($this->roles[$role]);
return $this->roleSearchIncludingChildren($this->roles, $role);
}

return $this->roleSearchIncludingChildren($this->roles, $role->getName());
}

/**
* @param $obj|Array
* @param $needle|String
* @return bool
*/
private function roleSearchIncludingChildren($obj, $needle) : bool
{
$rv = 0;

if (is_array($obj))
{

foreach ($obj as $role)
{

$roleName = $role->getName();
if ($roleName === $needle)
{
$rv++;
}

$rv += $this->roleSearchIncludingChildren($role, $needle);
}

}
else {

$children = $obj->getChildren();

// need to make sure the children are arrays (meaning they are added correctly)
if (! is_array($children)) {
return $rv ? true : false;
}

if ( ! count($children) )
{

return $rv ? true : false;

}
else {

foreach ($children as $child)
{

$roleName = $child->getName();

if ($roleName === $needle)
{
$rv++;
}

$rv += $this->roleSearchIncludingChildren($child, $needle);

}
}
}

$roleName = $role->getName();
return isset($this->roles[$roleName])
&& $this->roles[$roleName] === $role;
return $rv ? true : false;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion test/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ public function testHasRole()
// check that 'snafu' role and $snafu are different
$this->assertNotEquals($snafu, $this->rbac->getRole('snafu'));
$this->assertTrue($this->rbac->hasRole('snafu'));
$this->assertFalse($this->rbac->hasRole($snafu));
$this->assertTrue($this->rbac->hasRole($snafu));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is behaviour change, when we provide RoleInstance we expect that Rbac contains the same instance, so we can't compare just names, what I mean:

$role1 = new Role('role');
$role2 = new Role('role');

$role1 !== $role2;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Fixed this one.


// check child is found
$parent = new TestAsset\RoleTest('parent');
$child = new TestAsset\RoleTest('child');
$parent->addChild($child);
$this->rbac->addRole($parent);
$this->assertTrue($this->rbac->hasRole('child'));
}

public function testHasRoleWithInvalidElement()
Expand Down