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 ...@@ -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) { foreach ($oldData['items'] as $name => $data) {
if (isset($data['assignments'])) { if (isset($data['assignments'])) {
foreach ($data['assignments'] as $userId => $assignmentData) { foreach ($data['assignments'] as $userId => $assignmentData) {
$assignments[$userId] = $assignmentData['roleName']; $assignments[$userId][] = $assignmentData['roleName'];
} }
unset($data['assignments']); unset($data['assignments']);
} }
......
...@@ -644,12 +644,14 @@ class PhpManager extends BaseManager ...@@ -644,12 +644,14 @@ class PhpManager extends BaseManager
} }
} }
foreach ($assignments as $userId => $role) { foreach ($assignments as $userId => $roles) {
$this->assignments[$userId][$role] = new Assignment([ foreach ($roles as $role) {
'userId' => $userId, $this->assignments[$userId][$role] = new Assignment([
'roleName' => $role, 'userId' => $userId,
'createdAt' => $assignmentsMtime, 'roleName' => $role,
]); 'createdAt' => $assignmentsMtime,
]);
}
} }
foreach ($rules as $name => $ruleData) { foreach ($rules as $name => $ruleData) {
...@@ -730,7 +732,7 @@ class PhpManager extends BaseManager ...@@ -730,7 +732,7 @@ class PhpManager extends BaseManager
foreach ($this->assignments as $userId => $assignments) { foreach ($this->assignments as $userId => $assignments) {
foreach ($assignments as $name => $assignment) { foreach ($assignments as $name => $assignment) {
/* @var $assignment Assignment */ /* @var $assignment Assignment */
$assignmentData[$userId] = $assignment->roleName; $assignmentData[$userId][] = $assignment->roleName;
} }
} }
$this->saveToFile($assignmentData, $this->assignmentFile); $this->saveToFile($assignmentData, $this->assignmentFile);
......
...@@ -70,8 +70,7 @@ abstract class DbManagerTestCase extends ManagerTestCase ...@@ -70,8 +70,7 @@ abstract class DbManagerTestCase extends ManagerTestCase
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
$this->auth = new DbManager(['db' => $this->getConnection()]); $this->auth = $this->createManager();
} }
protected function tearDown() protected function tearDown()
...@@ -105,4 +104,12 @@ abstract class DbManagerTestCase extends ManagerTestCase ...@@ -105,4 +104,12 @@ abstract class DbManagerTestCase extends ManagerTestCase
} }
return static::$db; 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 ...@@ -17,6 +17,11 @@ abstract class ManagerTestCase extends TestCase
*/ */
protected $auth; protected $auth;
/**
* @return \yii\rbac\ManagerInterface
*/
abstract protected function createManager();
public function testCreateRole() public function testCreateRole()
{ {
$role = $this->auth->createRole('admin'); $role = $this->auth->createRole('admin');
...@@ -241,6 +246,26 @@ abstract class ManagerTestCase extends TestCase ...@@ -241,6 +246,26 @@ abstract class ManagerTestCase extends TestCase
$roles = $this->auth->getRolesByUser('reader A'); $roles = $this->auth->getRolesByUser('reader A');
$this->assertTrue(reset($roles) instanceof Role); $this->assertTrue(reset($roles) instanceof Role);
$this->assertEquals($roles['reader']->name, 'reader'); $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 ...@@ -32,6 +32,9 @@ class PhpManagerTest extends ManagerTestCase
@unlink($this->getRuleFile()); @unlink($this->getRuleFile());
} }
/**
* @inheritdoc
*/
protected function createManager() protected function createManager()
{ {
return new ExposedPhpManager([ 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