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