UserSwitch.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\models;
  8. use Yii;
  9. use yii\base\Model;
  10. use yii\web\IdentityInterface;
  11. use yii\web\User;
  12. /**
  13. * UserSwitch is a model used to temporary logging in another user
  14. *
  15. * @property User $mainUser This property is read-only.
  16. * @property null|User $user This property is read-only.
  17. *
  18. * @author Semen Dubina <yii2debug@sam002.net>
  19. * @since 2.0.10
  20. */
  21. class UserSwitch extends Model
  22. {
  23. /**
  24. * @var User user which we are currently switched to
  25. */
  26. private $_user;
  27. /**
  28. * @var User the main user who was originally logged in before switching.
  29. */
  30. private $_mainUser;
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['user', 'mainUser'], 'safe']
  38. ];
  39. }
  40. /**
  41. * @return array customized attribute labels
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'user' => 'Current User',
  47. 'mainUser' => 'frontend', 'Main User',
  48. ];
  49. }
  50. /**
  51. * Get current user
  52. * @return null|User
  53. */
  54. public function getUser()
  55. {
  56. if ($this->_user === null) {
  57. $this->_user = Yii::$app->get('user');
  58. }
  59. return $this->_user;
  60. }
  61. /**
  62. * Get main user
  63. * @return User
  64. */
  65. public function getMainUser()
  66. {
  67. $currentUser = $this->getUser();
  68. if ($this->_mainUser === null && $currentUser->getIsGuest() === false) {
  69. $session = Yii::$app->getSession();
  70. if ($session->has('main_user')) {
  71. $mainUserId = $session->get('main_user');
  72. $mainIdentity = call_user_func([$currentUser->identityClass, 'findIdentity'], $mainUserId);
  73. } else {
  74. $mainIdentity = $currentUser->identity;
  75. }
  76. $mainUser = clone $currentUser;
  77. $mainUser->setIdentity($mainIdentity);
  78. $this->_mainUser = $mainUser;
  79. }
  80. return $this->_mainUser;
  81. }
  82. /**
  83. * Switch user
  84. * @param User $user
  85. */
  86. public function setUser(User $user)
  87. {
  88. // Check if user is currently active one
  89. $isCurrent = ($user->getId() === $this->getMainUser()->getId());
  90. // Switch identity
  91. Yii::$app->getUser()->switchIdentity($user->identity);
  92. if (!$isCurrent) {
  93. Yii::$app->getSession()->set('main_user', $this->getMainUser()->getId());
  94. } else {
  95. Yii::$app->getSession()->remove('main_user');
  96. }
  97. }
  98. /**
  99. * Switch to user by identity
  100. * @param IdentityInterface $identity
  101. */
  102. public function setUserByIdentity(IdentityInterface $identity)
  103. {
  104. $user = clone $this->getUser();
  105. $user->setIdentity($identity);
  106. $this->setUser($user);
  107. }
  108. /**
  109. * Reset to main user
  110. */
  111. public function reset()
  112. {
  113. $this->setUser($this->getMainUser());
  114. }
  115. /**
  116. * Checks if current user is main or not.
  117. * @return bool
  118. */
  119. public function isMainUser()
  120. {
  121. $user = $this->getUser();
  122. if ($user->getIsGuest()) {
  123. return true;
  124. }
  125. return ($user->getId() === $this->getMainUser()->getId());
  126. }
  127. }