/
UserSwitch.php
152 lines (136 loc) · 3.64 KB
/
UserSwitch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\debug\models;
use Yii;
use yii\base\Model;
use yii\web\IdentityInterface;
use yii\web\User;
/**
* UserSwitch is a model used to temporary logging in another user
*
* @author Semen Dubina <yii2debug@sam002.net>
* @since 2.0.10
*/
class UserSwitch extends Model
{
/**
* @var User user which we are currently switched to
*/
private $_user;
/**
* @var User the main user who was originally logged in before switching.
*/
private $_mainUser;
/**
* @var string|User ID of the user component or a user object
* @since 2.0.13
*/
public $userComponent = 'user';
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user', 'mainUser'], 'safe']
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'user' => 'Current User',
'mainUser' => 'Main User',
];
}
/**
* Get current user
* @return null|User
* @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;
}
/**
* Get main user
* @return User
* @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;
}
/**
* Switch user
* @param User $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');
}
}
/**
* Switch to user by identity
* @param IdentityInterface $identity
* @throws \yii\base\InvalidConfigException
*/
public function setUserByIdentity(IdentityInterface $identity)
{
$user = clone $this->getUser();
$user->setIdentity($identity);
$this->setUser($user);
}
/**
* Reset to main user
*/
public function reset()
{
$this->setUser($this->getMainUser());
}
/**
* Checks if current user is main or not.
* @return bool
* @throws \yii\base\InvalidConfigException
*/
public function isMainUser()
{
$user = $this->getUser();
if ($user->getIsGuest()) {
return true;
}
return ($user->getId() === $this->getMainUser()->getId());
}
}