Fixes #11230: Include defaultRoles in yii\rbac\DbManager->getRolesByUser() results

This commit is contained in:
Elvira Sheina
2017-02-15 00:26:33 +02:00
committed by Alexander Makarov
parent 7db178d8bb
commit 3a4505ac08
8 changed files with 22 additions and 3 deletions

View File

@ -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

View File

@ -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
*/

View File

@ -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);
}

View File

@ -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) {

View File

@ -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']]);
}
}

View File

@ -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()

View File

@ -19,6 +19,7 @@ class MySQLManagerCacheTest extends MySQLManagerTest
return new DbManager([
'db' => $this->getConnection(),
'cache' => new FileCache(['cachePath' => '@yiiunit/runtime/cache']),
'defaultRoles' => ['myDefaultRole']
]);
}
}

View File

@ -64,6 +64,7 @@ class PhpManagerTest extends ManagerTestCase
'itemFile' => $this->getItemFile(),
'assignmentFile' => $this->getAssignmentFile(),
'ruleFile' => $this->getRuleFile(),
'defaultRoles' => ['myDefaultRole']
]);
}