yii\rbac\PhpManager::getRolesByUser PHP Method

getRolesByUser() public method

public getRolesByUser ( $userId )
    public function getRolesByUser($userId)
    {
        $roles = [];
        foreach ($this->getAssignments($userId) as $name => $assignment) {
            $role = $this->items[$assignment->roleName];
            if ($role->type === Item::TYPE_ROLE) {
                $roles[$name] = $role;
            }
        }
        return $roles;
    }

Usage Example

Beispiel #1
0
 /**
  * Непосредственно привязка роли к юзеру
  *
  * @param $userid
  *
  * @return bool|string
  */
 public function userAssign($userid)
 {
     $role = $this->getItem($this->forassign, self::TYPE_ROLE);
     $oldroles = $this->_authMan->getRolesByUser($userid);
     $this->_authMan->revokeAll($userid);
     try {
         if ($this->beforeAssign($userid, $role, $oldroles)) {
             $this->_authMan->assign($role, $userid);
             $this->afterAssign($userid, $role, $oldroles);
             return true;
         } else {
             return false;
         }
     } catch (\yii\base\Exception $e) {
         foreach ($oldroles as $orole) {
             $this->_authMan->assign($orole, $userid);
         }
         return RbacModule::t('simplerbac', 'Can`t assign this item for this user');
     }
 }