You are here

public function UserSwitch::switchUserBack in User Switch 8

Switching back to previous user.

Return value

bool TRUE when switched back previous account.

File

src/UserSwitch.php, line 121

Class

UserSwitch
Defines a UserSwitch service to switch user account.

Namespace

Drupal\userswitch

Code

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