mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Fixes #11230: Include defaultRoles
in yii\rbac\DbManager->getRolesByUser()
results
This commit is contained in:

committed by
Alexander Makarov

parent
7db178d8bb
commit
3a4505ac08
@ -7,6 +7,7 @@ Yii Framework 2 Change Log
|
||||
- Enh #13523: Plural rule for pasta (developeruz)
|
||||
- Bug #13538: Fixed `yii\db\BaseActiveRecord::deleteAll()` changes method signature declared by `yii\db\ActiveRecordInterface::deleteAll()` (klimov-paul)
|
||||
- Enh #13278: `yii\caching\DbQueryDependency` created allowing specification of the cache dependency via `yii\db\QueryInterface` (klimov-paul)
|
||||
- Bug #11230: Include `defaultRoles` in `yii\rbac\DbManager->getRolesByUser()` results (developeruz)
|
||||
|
||||
|
||||
2.0.11.2 February 08, 2017
|
||||
|
@ -189,6 +189,20 @@ abstract class BaseManager extends Component implements ManagerInterface
|
||||
return $this->getItems(Item::TYPE_ROLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns defaultRoles as array of Role objects
|
||||
* @since 2.0.12
|
||||
* @return Role[] default roles. The array is indexed by the role names
|
||||
*/
|
||||
public function getDefaultRoles()
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->defaultRoles as $roleName) {
|
||||
$result[$roleName] = $this->createRole($roleName);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
@ -466,7 +466,7 @@ class DbManager extends BaseManager
|
||||
->andWhere(['a.user_id' => (string) $userId])
|
||||
->andWhere(['b.type' => Item::TYPE_ROLE]);
|
||||
|
||||
$roles = [];
|
||||
$roles = $this->getDefaultRoles();
|
||||
foreach ($query->all($this->db) as $row) {
|
||||
$roles[$row['name']] = $this->populateItem($row);
|
||||
}
|
||||
|
@ -392,7 +392,7 @@ class PhpManager extends BaseManager
|
||||
*/
|
||||
public function getRolesByUser($userId)
|
||||
{
|
||||
$roles = [];
|
||||
$roles = $this->getDefaultRoles();
|
||||
foreach ($this->getAssignments($userId) as $name => $assignment) {
|
||||
$role = $this->items[$assignment->roleName];
|
||||
if ($role->type === Item::TYPE_ROLE) {
|
||||
|
@ -114,6 +114,6 @@ abstract class DbManagerTestCase extends ManagerTestCase
|
||||
*/
|
||||
protected function createManager()
|
||||
{
|
||||
return new DbManager(['db' => $this->getConnection()]);
|
||||
return new DbManager(['db' => $this->getConnection(), 'defaultRoles' => ['myDefaultRole']]);
|
||||
}
|
||||
}
|
||||
|
@ -299,6 +299,8 @@ abstract class ManagerTestCase extends TestCase
|
||||
$roles = $this->auth->getRolesByUser(123);
|
||||
$this->assertTrue(reset($roles) instanceof Role);
|
||||
$this->assertEquals($roles['reader']->name, 'reader');
|
||||
|
||||
$this->assertContains('myDefaultRole', array_keys($roles));
|
||||
}
|
||||
|
||||
public function testGetChildRoles()
|
||||
|
@ -19,6 +19,7 @@ class MySQLManagerCacheTest extends MySQLManagerTest
|
||||
return new DbManager([
|
||||
'db' => $this->getConnection(),
|
||||
'cache' => new FileCache(['cachePath' => '@yiiunit/runtime/cache']),
|
||||
'defaultRoles' => ['myDefaultRole']
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -64,6 +64,7 @@ class PhpManagerTest extends ManagerTestCase
|
||||
'itemFile' => $this->getItemFile(),
|
||||
'assignmentFile' => $this->getAssignmentFile(),
|
||||
'ruleFile' => $this->getRuleFile(),
|
||||
'defaultRoles' => ['myDefaultRole']
|
||||
]);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user