getRolesByUser() public method

Note that child roles that are not assigned directly to the user will not be returned.
public getRolesByUser ( string | integer $userId ) : Role[]
$userId string | integer the user ID (see [[\yii\web\User::id]])
return Role[] all roles directly assigned to the user. The array is indexed by the role names.
示例#1
0
 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));
 }