UserSwitch.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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' => 'frontend', 'Main User',
  53. ];
  54. }
  55. /**
  56. * Get current user
  57. * @return null|User
  58. */
  59. public function getUser()
  60. {
  61. if ($this->_user === null) {
  62. /* @var $user User */
  63. $this->_user = is_string($this->userComponent) ? Yii::$app->get($this->userComponent, false) : $this->userComponent;
  64. }
  65. return $this->_user;
  66. }
  67. /**
  68. * Get main user
  69. * @return User
  70. */
  71. public function getMainUser()
  72. {
  73. $currentUser = $this->getUser();
  74. if ($this->_mainUser === null && $currentUser->getIsGuest() === false) {
  75. $session = Yii::$app->getSession();
  76. if ($session->has('main_user')) {
  77. $mainUserId = $session->get('main_user');
  78. $mainIdentity = call_user_func([$currentUser->identityClass, 'findIdentity'], $mainUserId);
  79. } else {
  80. $mainIdentity = $currentUser->identity;
  81. }
  82. $mainUser = clone $currentUser;
  83. $mainUser->setIdentity($mainIdentity);
  84. $this->_mainUser = $mainUser;
  85. }
  86. return $this->_mainUser;
  87. }
  88. /**
  89. * Switch user
  90. * @param User $user
  91. */
  92. public function setUser(User $user)
  93. {
  94. // Check if user is currently active one
  95. $isCurrent = ($user->getId() === $this->getMainUser()->getId());
  96. // Switch identity
  97. $this->getUser()->switchIdentity($user->identity);
  98. if (!$isCurrent) {
  99. Yii::$app->getSession()->set('main_user', $this->getMainUser()->getId());
  100. } else {
  101. Yii::$app->getSession()->remove('main_user');
  102. }
  103. }
  104. /**
  105. * Switch to user by identity
  106. * @param IdentityInterface $identity
  107. */
  108. public function setUserByIdentity(IdentityInterface $identity)
  109. {
  110. $user = clone $this->getUser();
  111. $user->setIdentity($identity);
  112. $this->setUser($user);
  113. }
  114. /**
  115. * Reset to main user
  116. */
  117. public function reset()
  118. {
  119. $this->setUser($this->getMainUser());
  120. }
  121. /**
  122. * Checks if current user is main or not.
  123. * @return bool
  124. */
  125. public function isMainUser()
  126. {
  127. $user = $this->getUser();
  128. if ($user->getIsGuest()) {
  129. return true;
  130. }
  131. return ($user->getId() === $this->getMainUser()->getId());
  132. }
  133. }