Class yii\debug\models\UserSwitch
| Inheritance | yii\debug\models\UserSwitch » yii\base\Model |
|---|---|
| Available since extension's version | 2.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
| Property | Type | Description | Defined By |
|---|---|---|---|
| $mainUser | \yii\web\User | This property is read-only. | yii\debug\models\UserSwitch |
| $user | null|\yii\web\User | This property is read-only. | yii\debug\models\UserSwitch |
| $userComponent | string|\yii\web\User | ID of the user component or a user object | yii\debug\models\UserSwitch |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| attributeLabels() | yii\debug\models\UserSwitch | |
| getMainUser() | Get main user | yii\debug\models\UserSwitch |
| getUser() | Get current user | yii\debug\models\UserSwitch |
| isMainUser() | Checks if current user is main or not. | yii\debug\models\UserSwitch |
| reset() | Reset to main user | yii\debug\models\UserSwitch |
| rules() | yii\debug\models\UserSwitch | |
| setUser() | Switch user | yii\debug\models\UserSwitch |
| setUserByIdentity() | Switch to user by identity | yii\debug\models\UserSwitch |
Property Details
ID of the user component or a user object
Method Details
| public void attributeLabels ( ) |
public function attributeLabels()
{
return [
'user' => 'Current User',
'mainUser' => 'frontend', 'Main User',
];
}
Get main user
| public \yii\web\User getMainUser ( ) |
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;
}
Get current user
| public null|\yii\web\User getUser ( ) |
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;
}
Checks if current user is main or not.
| public boolean isMainUser ( ) |
public function isMainUser()
{
$user = $this->getUser();
if ($user->getIsGuest()) {
return true;
}
return ($user->getId() === $this->getMainUser()->getId());
}
Reset to main user
| public void reset ( ) |
public function reset()
{
$this->setUser($this->getMainUser());
}
Switch user
| public void setUser ( \yii\web\User $user ) | ||
| $user | \yii\web\User | |
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');
}
}
Switch to user by identity
| public void setUserByIdentity ( \yii\web\IdentityInterface $identity ) | ||
| $identity | \yii\web\IdentityInterface | |
public function setUserByIdentity(IdentityInterface $identity)
{
$user = clone $this->getUser();
$user->setIdentity($identity);
$this->setUser($user);
}