UserSwitch.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * @var string|User ID of the user component or a user object
  33. * @since 2.0.13
  34. */
  35. public $userComponent = 'user';
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['user', 'mainUser'], 'safe']
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'user' => 'Current User',
  52. 'mainUser' => 'Main User',
  53. ];
  54. }
  55. /**
  56. * Get current user
  57. * @return null|User
  58. * @throws \yii\base\InvalidConfigException
  59. */
  60. public function getUser()
  61. {
  62. if ($this->_user === null) {
  63. /* @var $user User */
  64. $this->_user = is_string($this->userComponent) ? Yii::$app->get($this->userComponent,
  65. false) : $this->userComponent;
  66. }
  67. return $this->_user;
  68. }
  69. /**
  70. * Get main user
  71. * @return User
  72. * @throws \yii\base\InvalidConfigException
  73. */
  74. public function getMainUser()
  75. {
  76. $currentUser = $this->getUser();
  77. if ($this->_mainUser === null && $currentUser->getIsGuest() === false) {
  78. $session = Yii::$app->getSession();
  79. if ($session->has('main_user')) {
  80. $mainUserId = $session->get('main_user');
  81. $mainIdentity = call_user_func([$currentUser->identityClass, 'findIdentity'], $mainUserId);
  82. } else {
  83. $mainIdentity = $currentUser->identity;
  84. }
  85. $mainUser = clone $currentUser;
  86. $mainUser->setIdentity($mainIdentity);
  87. $this->_mainUser = $mainUser;
  88. }
  89. return $this->_mainUser;
  90. }
  91. /**
  92. * Switch user
  93. * @param User $user
  94. * @throws \yii\base\InvalidConfigException
  95. */
  96. public function setUser(User $user)
  97. {
  98. // Check if user is currently active one
  99. $isCurrent = ($user->getId() === $this->getMainUser()->getId());
  100. // Switch identity
  101. $this->getUser()->switchIdentity($user->identity);
  102. if (!$isCurrent) {
  103. Yii::$app->getSession()->set('main_user', $this->getMainUser()->getId());
  104. } else {
  105. Yii::$app->getSession()->remove('main_user');
  106. }
  107. }
  108. /**
  109. * Switch to user by identity
  110. * @param IdentityInterface $identity
  111. * @throws \yii\base\InvalidConfigException
  112. */
  113. public function setUserByIdentity(IdentityInterface $identity)
  114. {
  115. $user = clone $this->getUser();
  116. $user->setIdentity($identity);
  117. $this->setUser($user);
  118. }
  119. /**
  120. * Reset to main user
  121. */
  122. public function reset()
  123. {
  124. $this->setUser($this->getMainUser());
  125. }
  126. /**
  127. * Checks if current user is main or not.
  128. * @return bool
  129. * @throws \yii\base\InvalidConfigException
  130. */
  131. public function isMainUser()
  132. {
  133. $user = $this->getUser();
  134. if ($user->getIsGuest()) {
  135. return true;
  136. }
  137. return ($user->getId() === $this->getMainUser()->getId());
  138. }
  139. }