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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug\controllers;
use Yii;
use yii\debug\models\UserSwitch;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\web\Response;
/**
* User controller
*
* @author Semen Dubina <yii2debug@sam002.net>
* @since 2.0.10
*/
class UserController extends Controller
{
/**
* @inheritdoc
*/
public function beforeAction($action)
{
Yii::$app->response->format = Response::FORMAT_JSON;
if (!Yii::$app->session->hasSessionId) {
throw new BadRequestHttpException('Need an active session');
}
return parent::beforeAction($action);
}
/**
* Set new identity, switch user
* @return \yii\web\User
*/
public function actionSetIdentity()
{
$user_id = Yii::$app->request->post('user_id');
$userSwitch = new UserSwitch();
$newIdentity = Yii::$app->user->identity->findIdentity($user_id);
$userSwitch->setUserByIdentity($newIdentity);
return Yii::$app->user;
}
/**
* Reset identity, switch to main user
* @return \yii\web\User
*/
public function actionResetIdentity()
{
$userSwitch = new UserSwitch();
$userSwitch->reset();
return Yii::$app->user;
}
}