Commit d1ccb92a by Alexander Makarov

Fixes #4489: RBAC PhpManager wasn't saving multiple assignmentsFixes #4489: RBAC…

Fixes #4489: RBAC PhpManager wasn't saving multiple assignmentsFixes #4489: RBAC PhpManager wasn't saving multiple assignments properly
parent fedf8ecc
......@@ -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([
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment