You are here

class UserSwitch in User Switch 8

Defines a UserSwitch service to switch user account.

Hierarchy

Expanded class hierarchy of UserSwitch

1 file declares its use of UserSwitch
UserSwitchController.php in src/Controller/UserSwitchController.php
2 string references to 'UserSwitch'
userswitch.info.yml in ./userswitch.info.yml
userswitch.info.yml
userswitch.services.yml in ./userswitch.services.yml
userswitch.services.yml
1 service uses UserSwitch
userswitch in ./userswitch.services.yml
Drupal\userswitch\UserSwitch

File

src/UserSwitch.php, line 15

Namespace

Drupal\userswitch
View source
class UserSwitch {
  use StringTranslationTrait;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The session manager.
   *
   * @var \Drupal\Core\Session\SessionManagerInterface
   */
  protected $sessionManager;

  /**
   * The session manager.
   *
   * @var Symfony\Component\HttpFoundation\Session\Session
   */
  protected $session;

  /**
   * Constructs.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Session\SessionManagerInterface $session_manager
   *   The session manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param Symfony\Component\HttpFoundation\Session\Session $session
   *   The session manager.
   */
  public function __construct(AccountInterface $current_user, ModuleHandlerInterface $module_handler, SessionManagerInterface $session_manager, EntityTypeManagerInterface $entityTypeManager, Session $session) {
    $this->moduleHandler = $module_handler;
    $this->currentUser = $current_user;
    $this->sessionManager = $session_manager;
    $this->entityTypeManager = $entityTypeManager;
    $this->session = $session;
  }

  /**
   * Check session.
   *
   * @return bool
   *   TRUE when user switch account, FALSE otherwise.
   */
  public function isSwitchUser() {
    return !empty($_SESSION['SwitchCurrentUser']);
  }

  /**
   * Return original user id, FALSE otherwise.
   */
  public function getUserId() {
    if (isset($_SESSION['SwitchCurrentUser'])) {
      return $_SESSION['SwitchCurrentUser'];
    }
    else {
      return FALSE;
    }
  }

  /**
   * User account switch.
   */
  public function switchToOther($target_account) {
    $account = $this->currentUser
      ->getAccount();
    $this->moduleHandler
      ->invokeAll('user_logout', [
      $account,
    ]);
    $this->sessionManager
      ->regenerate();
    $_SESSION['SwitchCurrentUser'] = $account
      ->id();
    $t_account = $this->entityTypeManager
      ->getStorage('user')
      ->load($target_account);
    $this->currentUser
      ->setAccount($t_account);
    $this->session
      ->set('uid', $t_account
      ->id());
    $this->moduleHandler
      ->invokeAll('user_login', [
      $t_account,
    ]);
    return TRUE;
  }

  /**
   * Switching back to previous user.
   *
   * @return bool
   *   TRUE when switched back previous account.
   */
  public function switchUserBack() {
    if (empty($_SESSION['SwitchCurrentUser'])) {
      return FALSE;
    }
    $new_user = $this->entityTypeManager
      ->getStorage('user')
      ->load($_SESSION['SwitchCurrentUser']);
    unset($_SESSION['SwitchCurrentUser']);
    if (!$new_user) {
      return FALSE;
    }
    $account = $this->currentUser
      ->getAccount();
    $this->moduleHandler
      ->invokeAll('user_logout', [
      $account,
    ]);
    $this->sessionManager
      ->regenerate();
    $this->currentUser
      ->setAccount($new_user);
    $this->session
      ->set('uid', $new_user
      ->id());
    $this->moduleHandler
      ->invokeAll('user_login', [
      $new_user,
    ]);
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UserSwitch::$currentUser protected property The current user.
UserSwitch::$entityTypeManager protected property The entity type manager.
UserSwitch::$moduleHandler protected property The module handler.
UserSwitch::$session protected property The session manager.
UserSwitch::$sessionManager protected property The session manager.
UserSwitch::getUserId public function Return original user id, FALSE otherwise.
UserSwitch::isSwitchUser public function Check session.
UserSwitch::switchToOther public function User account switch.
UserSwitch::switchUserBack public function Switching back to previous user.
UserSwitch::__construct public function Constructs.