Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Aug 6, 2019
1 parent 956c753 commit 4ebc5b2
Show file tree
Hide file tree
Showing 2 changed files with 25 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 @@ -53,6 +53,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
19 changes: 19 additions & 0 deletions test/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,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 @@ -303,4 +307,19 @@ public function testEmptyRoles()
{
$this->assertEquals([], $this->rbac->getRoles());
}

/**
* @see https://github.com/zendframework/zend-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 4ebc5b2

Please sign in to comment.