currentUser = $current_user; $this->writeSafeHandler = $write_safe_handler; } /** * {@inheritdoc} */ public function switchTo(AccountInterface $account) { // Prevent session information from being saved and push previous account. if (!isset($this->originalSessionSaving)) { // Ensure that only the first session saving status is saved. $this->originalSessionSaving = $this->writeSafeHandler->isSessionWritable(); } $this->writeSafeHandler->setSessionWritable(FALSE); array_push($this->accountStack, $this->currentUser->getAccount()); $this->currentUser->setAccount($account); return $this; } /** * {@inheritdoc} */ public function switchBack() { // Restore the previous account from the stack. if (!empty($this->accountStack)) { $this->currentUser->setAccount(array_pop($this->accountStack)); } else { throw new \RuntimeException('No more accounts to revert to.'); } // Restore original session saving status if all account switches are // reverted. if (empty($this->accountStack)) { if ($this->originalSessionSaving) { $this->writeSafeHandler->setSessionWritable(TRUE); } } return $this; } }