You are here

public function UserSwitch::switchToOther in User Switch 8

User account switch.

File

src/UserSwitch.php, line 101

Class

UserSwitch
Defines a UserSwitch service to switch user account.

Namespace

Drupal\userswitch

Code

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;
}