Class yii\debug\models\UserSwitch

Inheritanceyii\debug\models\UserSwitch » yii\base\Model
Available since extension's version2.0.10
Source Code https://github.com/yiisoft/yii2-debug/blob/master/src/models/UserSwitch.php

UserSwitch is a model used to temporary logging in another user

Public Properties

Hide inherited properties

Property Type Description Defined By
$userComponent string|\yii\web\User ID of the user component or a user object yii\debug\models\UserSwitch

Property Details

Hide inherited properties

$userComponent public property (available since version 2.0.13)

ID of the user component or a user object

public string|\yii\web\User $userComponent 'user'

Method Details

Hide inherited methods

attributeLabels() public method

public void attributeLabels ( )

                public function attributeLabels()
{
    return [
        'user' => 'Current User',
        'mainUser' => 'Main User',
    ];
}

            
getMainUser() public method

Get main user

public \yii\web\User getMainUser ( )
throws \yii\base\InvalidConfigException

                public function getMainUser()
{
    $currentUser = $this->getUser();
    if ($this->_mainUser === null && $currentUser->getIsGuest() === false) {
        $session = Yii::$app->getSession();
        if ($session->has('main_user')) {
            $mainUserId = $session->get('main_user');
            $mainIdentity = call_user_func([$currentUser->identityClass, 'findIdentity'], $mainUserId);
        } else {
            $mainIdentity = $currentUser->identity;
        }
        $mainUser = clone $currentUser;
        $mainUser->setIdentity($mainIdentity);
        $this->_mainUser = $mainUser;
    }
    return $this->_mainUser;
}

            
getUser() public method

Get current user

public null|\yii\web\User getUser ( )
throws \yii\base\InvalidConfigException

                public function getUser()
{
    if ($this->_user === null) {
        /* @var $user User */
        $this->_user = is_string($this->userComponent) ? Yii::$app->get($this->userComponent,
            false) : $this->userComponent;
    }
    return $this->_user;
}

            
isMainUser() public method

Checks if current user is main or not.

public boolean isMainUser ( )
throws \yii\base\InvalidConfigException

                public function isMainUser()
{
    $user = $this->getUser();
    if ($user->getIsGuest()) {
        return true;
    }
    return ($user->getId() === $this->getMainUser()->getId());
}

            
reset() public method

Reset to main user

public void reset ( )

                public function reset()
{
    $this->setUser($this->getMainUser());
}

            
rules() public method

public void rules ( )

                public function rules()
{
    return [
        [['user', 'mainUser'], 'safe']
    ];
}

            
setUser() public method

Switch user

public void setUser ( \yii\web\User $user )
$user \yii\web\User
throws \yii\base\InvalidConfigException

                public function setUser(User $user)
{
    // Check if user is currently active one
    $isCurrent = ($user->getId() === $this->getMainUser()->getId());
    // Switch identity
    $this->getUser()->switchIdentity($user->identity);
    if (!$isCurrent) {
        Yii::$app->getSession()->set('main_user', $this->getMainUser()->getId());
    } else {
        Yii::$app->getSession()->remove('main_user');
    }
}

            
setUserByIdentity() public method

Switch to user by identity

public void setUserByIdentity ( \yii\web\IdentityInterface $identity )
$identity \yii\web\IdentityInterface
throws \yii\base\InvalidConfigException

                public function setUserByIdentity(IdentityInterface $identity)
{
    $user = clone $this->getUser();
    $user->setIdentity($identity);
    $this->setUser($user);
}