mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-25 03:01:21 +08:00
Fixes #4489: RBAC PhpManager wasn't saving multiple assignmentsFixes #4489: RBAC PhpManager wasn't saving multiple assignments properly
This commit is contained in:
@@ -117,7 +117,7 @@ new ones save the following code as `convert.php` that should be placed in the s
|
||||
foreach ($oldData['items'] as $name => $data) {
|
||||
if (isset($data['assignments'])) {
|
||||
foreach ($data['assignments'] as $userId => $assignmentData) {
|
||||
$assignments[$userId] = $assignmentData['roleName'];
|
||||
$assignments[$userId][] = $assignmentData['roleName'];
|
||||
}
|
||||
unset($data['assignments']);
|
||||
}
|
||||
|
||||
@@ -644,12 +644,14 @@ class PhpManager extends BaseManager
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($assignments as $userId => $role) {
|
||||
$this->assignments[$userId][$role] = new Assignment([
|
||||
'userId' => $userId,
|
||||
'roleName' => $role,
|
||||
'createdAt' => $assignmentsMtime,
|
||||
]);
|
||||
foreach ($assignments as $userId => $roles) {
|
||||
foreach ($roles as $role) {
|
||||
$this->assignments[$userId][$role] = new Assignment([
|
||||
'userId' => $userId,
|
||||
'roleName' => $role,
|
||||
'createdAt' => $assignmentsMtime,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($rules as $name => $ruleData) {
|
||||
@@ -730,7 +732,7 @@ class PhpManager extends BaseManager
|
||||
foreach ($this->assignments as $userId => $assignments) {
|
||||
foreach ($assignments as $name => $assignment) {
|
||||
/* @var $assignment Assignment */
|
||||
$assignmentData[$userId] = $assignment->roleName;
|
||||
$assignmentData[$userId][] = $assignment->roleName;
|
||||
}
|
||||
}
|
||||
$this->saveToFile($assignmentData, $this->assignmentFile);
|
||||
|
||||
@@ -70,8 +70,7 @@ abstract class DbManagerTestCase extends ManagerTestCase
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->auth = new DbManager(['db' => $this->getConnection()]);
|
||||
|
||||
$this->auth = $this->createManager();
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
@@ -105,4 +104,12 @@ abstract class DbManagerTestCase extends ManagerTestCase
|
||||
}
|
||||
return static::$db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\rbac\ManagerInterface
|
||||
*/
|
||||
protected function createManager()
|
||||
{
|
||||
return new DbManager(['db' => $this->getConnection()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,11 @@ abstract class ManagerTestCase extends TestCase
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
/**
|
||||
* @return \yii\rbac\ManagerInterface
|
||||
*/
|
||||
abstract protected function createManager();
|
||||
|
||||
public function testCreateRole()
|
||||
{
|
||||
$role = $this->auth->createRole('admin');
|
||||
@@ -241,6 +246,26 @@ abstract class ManagerTestCase extends TestCase
|
||||
$roles = $this->auth->getRolesByUser('reader A');
|
||||
$this->assertTrue(reset($roles) instanceof Role);
|
||||
$this->assertEquals($roles['reader']->name, 'reader');
|
||||
}
|
||||
|
||||
public function testAssignMultipleRoles()
|
||||
{
|
||||
$this->prepareData();
|
||||
|
||||
$reader = $this->auth->getRole('reader');
|
||||
$author = $this->auth->getRole('author');
|
||||
$this->auth->assign($reader, 'readingAuthor');
|
||||
$this->auth->assign($author, 'readingAuthor');
|
||||
|
||||
$this->auth = $this->createManager();
|
||||
|
||||
$roles = $this->auth->getRolesByUser('readingAuthor');
|
||||
$roleNames = [];
|
||||
foreach ($roles as $role) {
|
||||
$roleNames[] = $role->name;
|
||||
}
|
||||
|
||||
$this->assertContains('reader', $roleNames, 'Roles should contain reader. Currently it has: ' . implode(', ', $roleNames));
|
||||
$this->assertContains('author', $roleNames, 'Roles should contain author. Currently it has: ' . implode(', ', $roleNames));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,9 @@ class PhpManagerTest extends ManagerTestCase
|
||||
@unlink($this->getRuleFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function createManager()
|
||||
{
|
||||
return new ExposedPhpManager([
|
||||
|
||||
Reference in New Issue
Block a user