Skip to content

Commit

Permalink
RBAC addChild fix
Browse files Browse the repository at this point in the history
Closes issue laminas#3. Original fix by @ezimuel

Signed-off-by: Gennadiy Litvinyuk <[email protected]>
  • Loading branch information
gennadiylitvinyuk committed Apr 3, 2021
1 parent 1c6a932 commit f07f461
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function addRole($role, $parents = null) : void
);
}

if ($this->createMissingRoles) {
foreach ($role->getChildren() as $child) {
$this->addRole($child);
}
}

if ($parents) {
$parents = is_array($parents) ? $parents : [$parents];
foreach ($parents as $parent) {
Expand Down
20 changes: 20 additions & 0 deletions test/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ public function testAddCustomChildRole()
{
$role = $this->getMockForAbstractClass(Rbac\RoleInterface::class);
$this->rbac->setCreateMissingRoles(true);
$role->expects($this->any())
->method('getChildren')
->will($this->returnValue([]));

$this->rbac->addRole($role, 'parent');

$role->expects($this->any())
Expand Down Expand Up @@ -304,4 +308,20 @@ public function testEmptyRoles()
{
$this->assertEquals([], $this->rbac->getRoles());
}


/**
* @see https://github.com/laminas/laminas-permissions-rbac/issues/45
*/
public function testHasRoleWithChildrenRoles()
{
$foo = new Rbac\Role('foo');
$bar = new Rbac\Role('bar');

$this->rbac->setCreateMissingRoles(true);
$foo->addChild($bar);
$this->rbac->addRole($foo);

$this->assertTrue($this->rbac->hasRole('bar'));
}
}

0 comments on commit f07f461

Please sign in to comment.