From 4ebc5b28f80d28586bac24d6f20e193d22f4c2ae Mon Sep 17 00:00:00 2001 From: Enrico Zimuel Date: Tue, 6 Aug 2019 10:38:07 +0200 Subject: [PATCH] Fix #45 --- src/Rbac.php | 6 ++++++ test/RbacTest.php | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Rbac.php b/src/Rbac.php index 65385c65..0783a6b3 100644 --- a/src/Rbac.php +++ b/src/Rbac.php @@ -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) { diff --git a/test/RbacTest.php b/test/RbacTest.php index bc2d8804..41e2fc89 100644 --- a/test/RbacTest.php +++ b/test/RbacTest.php @@ -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()) @@ -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')); + } }