From 73a30780b384d90605a7091d61ac302c902e6e4c Mon Sep 17 00:00:00 2001 From: Pavel Dovlatov Date: Mon, 24 Oct 2016 14:11:09 +0300 Subject: [PATCH] Fixes #12810: getChildRoles() throws an exception when role has no children --- framework/CHANGELOG.md | 1 + framework/rbac/DbManager.php | 2 +- framework/rbac/PhpManager.php | 2 +- tests/framework/rbac/ManagerTestCase.php | 8 ++++++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 79fdfea1d5..4fd6bc66d1 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -5,6 +5,7 @@ Yii Framework 2 Change Log ------------------------ - Bug #12791: Fixed `yii\behaviors\AttributeTypecastBehavior` unable to automatically detect `attributeTypes`, triggering PHP Fatal Error (klimov-paul) +- Bug #12810: Fixed `yii\rbac\DbManager::getChildRoles()` and `yii\rbac\PhpManager::getChildRoles()` throws an exception when role has no child roles (mysterydragon) - Enh #12790: Added `scrollToErrorOffset` option for `ActiveForm` which adds ability to specify offset in pixels when scrolling to error (mg-code) - Enh #12807: Added console controller checks for `yii\console\controllers\HelpController` (schmunk42) diff --git a/framework/rbac/DbManager.php b/framework/rbac/DbManager.php index 7eff65b785..24741b8690 100644 --- a/framework/rbac/DbManager.php +++ b/framework/rbac/DbManager.php @@ -481,7 +481,7 @@ class DbManager extends BaseManager throw new InvalidParamException("Role \"$roleName\" not found."); } - /** @var $result Item[] */ + $result = []; $this->getChildrenRecursive($roleName, $this->getChildrenList(), $result); $roles = [$roleName => $role]; diff --git a/framework/rbac/PhpManager.php b/framework/rbac/PhpManager.php index fcc3defd7c..f0fc4e5ae0 100644 --- a/framework/rbac/PhpManager.php +++ b/framework/rbac/PhpManager.php @@ -407,7 +407,7 @@ class PhpManager extends BaseManager throw new InvalidParamException("Role \"$roleName\" not found."); } - /** @var $result Item[] */ + $result = []; $this->getChildrenRecursive($roleName, $result); $roles = [$roleName => $role]; diff --git a/tests/framework/rbac/ManagerTestCase.php b/tests/framework/rbac/ManagerTestCase.php index 2313970b5b..67a32484f4 100644 --- a/tests/framework/rbac/ManagerTestCase.php +++ b/tests/framework/rbac/ManagerTestCase.php @@ -223,6 +223,9 @@ abstract class ManagerTestCase extends TestCase $updateAnyPost->description = 'update any post'; $this->auth->add($updateAnyPost); + $withoutChildren = $this->auth->createRole('withoutChildren'); + $this->auth->add($withoutChildren); + $reader = $this->auth->createRole('reader'); $this->auth->add($reader); $this->auth->addChild($reader, $readPost); @@ -292,6 +295,11 @@ abstract class ManagerTestCase extends TestCase { $this->prepareData(); + $roles = $this->auth->getChildRoles('withoutChildren'); + $this->assertCount(1, $roles); + $this->assertInstanceOf(Role::className(), reset($roles)); + $this->assertTrue(reset($roles)->name === 'withoutChildren'); + $roles = $this->auth->getChildRoles('reader'); $this->assertCount(1, $roles); $this->assertInstanceOf(Role::className(), reset($roles));