Note that child roles that are not assigned directly to the user will not be returned.
public getRolesByUser ( string | integer $userId ) : |
||
$userId | string | integer | the user ID (see [[\yii\web\User::id]]) |
Résultat | all roles directly assigned to the user. The array is indexed by the role names. |
public function testGetRolesByUser() { $this->prepareData(); $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)); }