UserController.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\controllers;
  8. use Yii;
  9. use yii\debug\models\UserSwitch;
  10. use yii\web\BadRequestHttpException;
  11. use yii\web\Controller;
  12. use yii\web\Response;
  13. /**
  14. * User controller
  15. *
  16. * @author Semen Dubina <yii2debug@sam002.net>
  17. * @since 2.0.10
  18. */
  19. class UserController extends Controller
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function beforeAction($action)
  25. {
  26. Yii::$app->response->format = Response::FORMAT_JSON;
  27. if (!Yii::$app->session->hasSessionId) {
  28. throw new BadRequestHttpException('Need an active session');
  29. }
  30. return parent::beforeAction($action);
  31. }
  32. /**
  33. * Set new identity, switch user
  34. * @return \yii\web\User
  35. */
  36. public function actionSetIdentity()
  37. {
  38. $user_id = Yii::$app->request->post('user_id');
  39. $userSwitch = new UserSwitch();
  40. $newIdentity = Yii::$app->user->identity->findIdentity($user_id);
  41. $userSwitch->setUserByIdentity($newIdentity);
  42. return Yii::$app->user;
  43. }
  44. /**
  45. * Reset identity, switch to main user
  46. * @return \yii\web\User
  47. */
  48. public function actionResetIdentity()
  49. {
  50. $userSwitch = new UserSwitch();
  51. $userSwitch->reset();
  52. return Yii::$app->user;
  53. }
  54. }