mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Merge pull request #10355 from yiisoft/rbac-get-user-ids-by-role
Fixes #9573: Added `yii\rbac\ManagerInterface::getUserIDsByRole()` and implementations
This commit is contained in:
@ -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 #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 #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 #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 #9635: Added default CSS class for `\yii\grid\ActionColumn` header (arogachev, dynasource)
|
||||||
- Enh #9643: Added migrations for DB cache (mdmunir)
|
- Enh #9643: Added migrations for DB cache (mdmunir)
|
||||||
- Enh #9711: Added `yii\widgets\LinkPager::$pageCssClass` that allows to set default page class (ShNURoK42)
|
- Enh #9711: Added `yii\widgets\LinkPager::$pageCssClass` that allows to set default page class (ShNURoK42)
|
||||||
|
@ -36,6 +36,7 @@ the event.
|
|||||||
with global variable `yii` instead.
|
with global variable `yii` instead.
|
||||||
* Traversable objects are now formatted as arrays in XML response to support SPL objects and Generators. Previous
|
* 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`.
|
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
|
Upgrade from Yii 2.0.5
|
||||||
----------------------
|
----------------------
|
||||||
|
@ -955,4 +955,21 @@ class DbManager extends BaseManager
|
|||||||
|
|
||||||
$this->cache->set($this->cacheKey, [$this->items, $this->rules, $this->parents]);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -210,6 +210,13 @@ interface ManagerInterface
|
|||||||
*/
|
*/
|
||||||
public function getAssignments($userId);
|
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.
|
* Removes all authorization data, including roles, permissions, rules, and assignments.
|
||||||
*/
|
*/
|
||||||
|
@ -809,4 +809,20 @@ class PhpManager extends BaseManager
|
|||||||
}
|
}
|
||||||
$this->saveToFile($rules, $this->ruleFile);
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,4 +298,18 @@ abstract class ManagerTestCase extends TestCase
|
|||||||
$this->assertEquals(1, count($this->auth->getAssignments(42)));
|
$this->assertEquals(1, count($this->auth->getAssignments(42)));
|
||||||
$this->assertEquals(2, count($this->auth->getAssignments(1337)));
|
$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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user