getAssignments() public method

Returns all role assignment information for the specified user.
public getAssignments ( string | integer $userId ) : Assignment[]
$userId string | integer the user ID (see [[\yii\web\User::id]])
return Assignment[] the assignments indexed by role names. An empty array will be returned if there is no role assigned to the user.
 public function testAssignmentsToIntegerId()
 {
     $this->prepareData();
     $reader = $this->auth->getRole('reader');
     $author = $this->auth->getRole('author');
     $this->auth->assign($reader, 42);
     $this->auth->assign($author, 1337);
     $this->auth->assign($reader, 1337);
     $this->auth = $this->createManager();
     $this->assertEquals(0, count($this->auth->getAssignments(0)));
     $this->assertEquals(1, count($this->auth->getAssignments(42)));
     $this->assertEquals(2, count($this->auth->getAssignments(1337)));
 }
Beispiel #2
0
 /**
  * Get all available and assigned roles and permissions
  *
  * @return array
  */
 public function getItems()
 {
     $available = [];
     $assigned = [];
     foreach (array_keys($this->manager->getRoles()) as $name) {
         $available[$name] = 'role';
     }
     foreach (array_keys($this->manager->getPermissions()) as $name) {
         if ($name[0] != '/') {
             $available[$name] = 'permission';
         }
     }
     foreach ($this->manager->getAssignments($this->userId) as $item) {
         $assigned[$item->roleName] = $available[$item->roleName];
         unset($available[$item->roleName]);
     }
     return ['available' => $available, 'assigned' => $assigned];
 }