diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index c9dad00bdc..23359c7f52 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -59,6 +59,7 @@ Yii Framework 2 Change Log - Enh #9337: Added `yii\db\ColumnSchemaBuilder::defaultExpression()` to support DB Expression as default value (kotchuprik) - Enh #9465: ./yii migrate/create now generates code based on migration name and --fields (pana1990) - Enh #9476: Added DI injection via controller action method signature (mdmunir) +- Enh #9573: Added `yii\rbac\ManagerInterface::getUserIDsByRole()` and implementations (samdark) - Enh #9635: Added default CSS class for `\yii\grid\ActionColumn` header (arogachev, dynasource) - Enh #9643: Added migrations for DB cache (mdmunir) - Enh #9711: Added `yii\widgets\LinkPager::$pageCssClass` that allows to set default page class (ShNURoK42) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index c53c9224c9..16989310f4 100644 --- a/framework/UPGRADE.md +++ b/framework/UPGRADE.md @@ -36,6 +36,7 @@ the event. with global variable `yii` instead. * Traversable objects are now formatted as arrays in XML response to support SPL objects and Generators. Previous behavior could be turned on by setting `XmlResponseFormatter::$useTraversableAsArray` to `false`. +* If you've implemented `yii\rbac\ManagerInterface` you need to implement additional method `getUserIDsByRole($roleName)`. Upgrade from Yii 2.0.5 ---------------------- diff --git a/framework/rbac/DbManager.php b/framework/rbac/DbManager.php index 9fa9c7e065..0b86449d21 100644 --- a/framework/rbac/DbManager.php +++ b/framework/rbac/DbManager.php @@ -955,4 +955,21 @@ class DbManager extends BaseManager $this->cache->set($this->cacheKey, [$this->items, $this->rules, $this->parents]); } + + /** + * Returns all role assignment information for the specified role. + * @param string $roleName + * @return Assignment[] the assignments. An empty array will be + * returned if role is not assigned to any user. + */ + public function getUserIDsByRole($roleName) + { + if (empty($roleName)) { + return []; + } + + return (new Query)->select('[[user_id]]') + ->from($this->assignmentTable) + ->where(['item_name' => $roleName])->column($this->db); + } } diff --git a/framework/rbac/ManagerInterface.php b/framework/rbac/ManagerInterface.php index 072bbd898e..1a870c03ab 100644 --- a/framework/rbac/ManagerInterface.php +++ b/framework/rbac/ManagerInterface.php @@ -210,6 +210,13 @@ interface ManagerInterface */ public function getAssignments($userId); + /** + * Returns all user IDs assigned to the role specified. + * @param string $roleName + * @return array array of user ID strings + */ + public function getUserIDsByRole($roleName); + /** * Removes all authorization data, including roles, permissions, rules, and assignments. */ diff --git a/framework/rbac/PhpManager.php b/framework/rbac/PhpManager.php index da95a0aa98..0b0ddca223 100644 --- a/framework/rbac/PhpManager.php +++ b/framework/rbac/PhpManager.php @@ -809,4 +809,20 @@ class PhpManager extends BaseManager } $this->saveToFile($rules, $this->ruleFile); } + + /** + * @inheritdoc + */ + public function getUserIDsByRole($roleName) + { + $result = []; + foreach ($this->assignments as $userID => $assignments) { + foreach ($assignments as $userAssignment) { + if ($userAssignment->roleName === $roleName && $userAssignment->userId === $userID) { + $result[] = (string)$userID; + } + } + } + return $result; + } } diff --git a/tests/framework/rbac/ManagerTestCase.php b/tests/framework/rbac/ManagerTestCase.php index a94a615a67..fca21dbe98 100644 --- a/tests/framework/rbac/ManagerTestCase.php +++ b/tests/framework/rbac/ManagerTestCase.php @@ -298,4 +298,18 @@ abstract class ManagerTestCase extends TestCase $this->assertEquals(1, count($this->auth->getAssignments(42))); $this->assertEquals(2, count($this->auth->getAssignments(1337))); } + + public function testGetAssignmentsByRole() + { + $this->prepareData(); + $reader = $this->auth->getRole('reader'); + $this->auth->assign($reader, 123); + + $this->auth = $this->createManager(); + + $this->assertEquals([], $this->auth->getUserIDsByRole('nonexisting')); + $this->assertEquals(['reader A', '123'], $this->auth->getUserIDsByRole('reader'), '', 0.0, 10, true); + $this->assertEquals(['author B'], $this->auth->getUserIDsByRole('author')); + $this->assertEquals(['admin C'], $this->auth->getUserIDsByRole('admin')); + } }