diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Authentication/AccountProxy.php similarity index 95% copy from core/lib/Drupal/Core/Session/AccountProxy.php copy to core/lib/Drupal/Core/Authentication/AccountProxy.php index 463f00f..3d915f0 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Authentication/AccountProxy.php @@ -2,12 +2,10 @@ /** * @file - * Contains \Drupal\Core\Session\AccountProxy. + * Contains \Drupal\Core\Authentication\AccountProxy. */ -namespace Drupal\Core\Session; -use Drupal\Core\Authentication\AccountInterface; -use Drupal\Core\Authentication\AnonymousUserSession; +namespace Drupal\Core\Authentication; /** * A proxied implementation of AccountInterface. diff --git a/core/lib/Drupal/Core/Session/AccountProxyInterface.php b/core/lib/Drupal/Core/Authentication/AccountProxyInterface.php similarity index 90% copy from core/lib/Drupal/Core/Session/AccountProxyInterface.php copy to core/lib/Drupal/Core/Authentication/AccountProxyInterface.php index d3cf56a..2ffa241 100644 --- a/core/lib/Drupal/Core/Session/AccountProxyInterface.php +++ b/core/lib/Drupal/Core/Authentication/AccountProxyInterface.php @@ -2,12 +2,10 @@ /** * @file - * Contains \Drupal\Core\Session\AccountProxyInterface. + * Contains \Drupal\Core\Authentication\AccountProxyInterface. */ -namespace Drupal\Core\Session; - -use Drupal\Core\Authentication\AccountInterface; +namespace Drupal\Core\Authentication; /** * Defines an interface for a service which has the current account stored. diff --git a/core/lib/Drupal/Core/Session/AccountSwitcher.php b/core/lib/Drupal/Core/Authentication/AccountSwitcher.php similarity index 89% copy from core/lib/Drupal/Core/Session/AccountSwitcher.php copy to core/lib/Drupal/Core/Authentication/AccountSwitcher.php index d105b21..decbc03 100644 --- a/core/lib/Drupal/Core/Session/AccountSwitcher.php +++ b/core/lib/Drupal/Core/Authentication/AccountSwitcher.php @@ -2,11 +2,12 @@ /** * @file - * Contains \Drupal\Core\Session\AccountSwitcher. + * Contains \Drupal\Core\Authentication\AccountSwitcher. */ -namespace Drupal\Core\Session; -use Drupal\Core\Authentication\AccountInterface; +namespace Drupal\Core\Authentication; + +use Drupal\Core\Session\WriteSafeSessionHandlerInterface; /** * An implementation of AccountSwitcherInterface. @@ -27,7 +28,7 @@ class AccountSwitcher implements AccountSwitcherInterface { /** * The current user service. * - * @var \Drupal\Core\Session\AccountProxyInterface + * @var \Drupal\Core\Authentication\AccountProxyInterface */ protected $currentUser = array(); @@ -48,7 +49,7 @@ class AccountSwitcher implements AccountSwitcherInterface { /** * Constructs a new AccountSwitcher. * - * @param \Drupal\Core\Session\AccountProxyInterface $current_user + * @param \Drupal\Core\Authentication\AccountProxyInterface $current_user * The current user service. * @param \Drupal\Core\Session\WriteSafeSessionHandlerInterface $write_safe_handler * The write-safe session handler. diff --git a/core/lib/Drupal/Core/Session/AccountSwitcherInterface.php b/core/lib/Drupal/Core/Authentication/AccountSwitcherInterface.php similarity index 75% copy from core/lib/Drupal/Core/Session/AccountSwitcherInterface.php copy to core/lib/Drupal/Core/Authentication/AccountSwitcherInterface.php index b2eb289..23b05c4 100644 --- a/core/lib/Drupal/Core/Session/AccountSwitcherInterface.php +++ b/core/lib/Drupal/Core/Authentication/AccountSwitcherInterface.php @@ -2,11 +2,10 @@ /** * @file - * Contains \Drupal\Core\Session\AccountSwitcherInterface. + * Contains \Drupal\Core\Authentication\AccountSwitcherInterface. */ -namespace Drupal\Core\Session; -use Drupal\Core\Authentication\AccountInterface; +namespace Drupal\Core\Authentication; /** * Defines an interface for a service for safe account switching. @@ -25,7 +24,7 @@ * @param \Drupal\Core\Authentication\AccountInterface $account * The account to switch to. * - * @return \Drupal\Core\Session\AccountSwitcherInterface + * @return \Drupal\Core\Authentication\AccountSwitcherInterface * $this. */ public function switchTo(AccountInterface $account); @@ -33,7 +32,7 @@ public function switchTo(AccountInterface $account); /** * Reverts to a previous account after switching. * - * @return \Drupal\Core\Session\AccountSwitcherInterface + * @return \Drupal\Core\Authentication\AccountSwitcherInterface * $this. * * @throws \RuntimeException diff --git a/core/lib/Drupal/Core/Session/AccountInterface.php b/core/lib/Drupal/Core/Session/AccountInterface.php new file mode 100644 index 0000000..7fc2644 --- /dev/null +++ b/core/lib/Drupal/Core/Session/AccountInterface.php @@ -0,0 +1,19 @@ +getAccount(); - } - $this->account = $account; - date_default_timezone_set(drupal_get_user_timezone()); - } - - /** - * {@inheritdoc} - */ - public function getAccount() { - if (!isset($this->account)) { - if ($this->initialAccountId) { - // After the container is rebuilt, DrupalKernel sets the initial - // account to the id of the logged in user. This is necessary in order - // to refresh the user account reference here. - $this->account = $this->loadUserEntity($this->initialAccountId); - } - else { - $this->account = new AnonymousUserSession(); - } - } - - return $this->account; - } - - /** - * {@inheritdoc} - */ - public function id() { - return $this->getAccount()->id(); - } - - /** - * {@inheritdoc} - */ - public function getRoles($exclude_locked_roles = FALSE) { - return $this->getAccount()->getRoles($exclude_locked_roles); - } - - /** - * {@inheritdoc} - */ - public function hasPermission($permission) { - return $this->getAccount()->hasPermission($permission); - } - - /** - * {@inheritdoc} - */ - public function isAuthenticated() { - return $this->getAccount()->isAuthenticated(); - } - - /** - * {@inheritdoc} - */ - public function isAnonymous() { - return $this->getAccount()->isAnonymous(); - } - - /** - * {@inheritdoc} - */ - public function getPreferredLangcode($fallback_to_default = TRUE) { - return $this->getAccount()->getPreferredLangcode($fallback_to_default); - } - - /** - * {@inheritdoc} - */ - public function getPreferredAdminLangcode($fallback_to_default = TRUE) { - return $this->getAccount()->getPreferredAdminLangcode($fallback_to_default); - } - - /** - * {@inheritdoc} - */ - public function getUsername() { - return $this->getAccount()->getUsername(); - } - - /** - * {@inheritdoc} - */ - public function getEmail() { - return $this->getAccount()->getEmail(); - } - - /** - * {@inheritdoc} - */ - public function getTimeZone() { - return $this->getAccount()->getTimeZone(); - } - - /** - * {@inheritdoc} - */ - public function getLastAccessedTime() { - return $this->getAccount()->getLastAccessedTime(); - } - - /** - * {@inheritdoc} - */ - public function setInitialAccountId($account_id) { - if (isset($this->account)) { - throw new \LogicException('AccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy'); - } - - $this->initialAccountId = $account_id; - } - - /** - * Load a user entity. - * - * The entity manager requires additional initialization code and cache - * clearing after the list of modules is changed. Therefore it is necessary to - * retrieve it as late as possible. - * - * Because of serialization issues it is currently not possible to inject the - * container into the AccountProxy. Thus it is necessary to retrieve the - * entity manager statically. - * - * @see https://www.drupal.org/node/2430447 - * - * @param int $account_id - * The id of an account to load. - * - * @return \Drupal\Core\Authentication\AccountInterface|NULL - * An account or NULL if none is found. - */ - protected function loadUserEntity($account_id) { - return \Drupal::entityManager()->getStorage('user')->load($account_id); - } - +class AccountProxy extends AuthenticationAccountProxy { } diff --git a/core/lib/Drupal/Core/Session/AccountProxyInterface.php b/core/lib/Drupal/Core/Session/AccountProxyInterface.php index d3cf56a..c2bd905 100644 --- a/core/lib/Drupal/Core/Session/AccountProxyInterface.php +++ b/core/lib/Drupal/Core/Session/AccountProxyInterface.php @@ -7,47 +7,13 @@ namespace Drupal\Core\Session; -use Drupal\Core\Authentication\AccountInterface; +use Drupal\Core\Authentication\AccountProxyInterface as AuthenticationAccountProxyInterface; /** - * Defines an interface for a service which has the current account stored. + * Provides BC wrapper for \Drupal\Core\Authentication\AccountProxyInterface. * - * @ingroup user_api + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.0. Use + * \Drupal\Core\Authentication\AccountProxyInterface instead. */ -interface AccountProxyInterface extends AccountInterface { - - /** - * Sets the currently wrapped account. - * - * Setting the current account is highly discouraged! Instead, make sure to - * inject the desired user object into the dependent code directly. - * - * A preferable method of account impersonation is to use - * \Drupal\Core\Session\AccountSwitcherInterface::switchTo() and - * \Drupal\Core\Session\AccountSwitcherInterface::switchBack(). - * - * @param \Drupal\Core\Authentication\AccountInterface $account - * The current account. - */ - public function setAccount(AccountInterface $account); - - /** - * Gets the currently wrapped account. - * - * @return \Drupal\Core\Authentication\AccountInterface - * The current account. - */ - public function getAccount(); - - /** - * Sets the id of the initial account. - * - * Never use this method, its sole purpose is to work around weird effects - * during mid-request container rebuilds. - * - * @param int $account_id - * The id of the initial account. - */ - public function setInitialAccountId($account_id); - +interface AccountProxyInterface extends AuthenticationAccountProxyInterface { } diff --git a/core/lib/Drupal/Core/Session/AccountSwitcher.php b/core/lib/Drupal/Core/Session/AccountSwitcher.php index d105b21..5da9029 100644 --- a/core/lib/Drupal/Core/Session/AccountSwitcher.php +++ b/core/lib/Drupal/Core/Session/AccountSwitcher.php @@ -6,92 +6,14 @@ */ namespace Drupal\Core\Session; -use Drupal\Core\Authentication\AccountInterface; + +use Drupal\Core\Authentication\AccountSwitcher as AuthenticationAccountSwitcher; /** - * An implementation of AccountSwitcherInterface. + * Provides BC wrapper for \Drupal\Core\Authentication\AccountSwitcher. * - * This allows for safe switching of user accounts by ensuring that session - * data for one user is not leaked in to others. It also provides a stack that - * allows reverting to a previous user after switching. + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.0. Use + * \Drupal\Core\Authentication\AccountSwitcher instead. */ -class AccountSwitcher implements AccountSwitcherInterface { - - /** - * A stack of previous overridden accounts. - * - * @var \Drupal\Core\Authentication\AccountInterface[] - */ - protected $accountStack = array(); - - /** - * The current user service. - * - * @var \Drupal\Core\Session\AccountProxyInterface - */ - protected $currentUser = array(); - - /** - * The write-safe session handler. - * - * @var \Drupal\Core\Session\WriteSafeSessionHandlerInterface - */ - protected $writeSafeHandler; - - /** - * The original state of session saving prior to account switching. - * - * @var bool - */ - protected $originalSessionSaving; - - /** - * Constructs a new AccountSwitcher. - * - * @param \Drupal\Core\Session\AccountProxyInterface $current_user - * The current user service. - * @param \Drupal\Core\Session\WriteSafeSessionHandlerInterface $write_safe_handler - * The write-safe session handler. - */ - public function __construct(AccountProxyInterface $current_user, WriteSafeSessionHandlerInterface $write_safe_handler) { - $this->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; - } - +class AccountSwitcher extends AuthenticationAccountSwitcher { } diff --git a/core/lib/Drupal/Core/Session/AccountSwitcherInterface.php b/core/lib/Drupal/Core/Session/AccountSwitcherInterface.php index b2eb289..48ae790 100644 --- a/core/lib/Drupal/Core/Session/AccountSwitcherInterface.php +++ b/core/lib/Drupal/Core/Session/AccountSwitcherInterface.php @@ -6,39 +6,14 @@ */ namespace Drupal\Core\Session; -use Drupal\Core\Authentication\AccountInterface; + +use Drupal\Core\Authentication\AccountSwitcherInterface as AuthenticationAccountSwitcherInterface; /** - * Defines an interface for a service for safe account switching. + * Provides BC wrapper for \Drupal\Core\Authentication\AccountSwitcherInterface. * - * @ingroup user_api + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.0. Use + * \Drupal\Core\Authentication\AccountSwitcherInterface instead. */ -interface AccountSwitcherInterface { - - /** - * Safely switches to another account. - * - * Each invocation of AccountSwitcherInterface::switchTo() must be - * matched by a corresponding invocation of - * AccountSwitcherInterface::switchBack() in the same function. - * - * @param \Drupal\Core\Authentication\AccountInterface $account - * The account to switch to. - * - * @return \Drupal\Core\Session\AccountSwitcherInterface - * $this. - */ - public function switchTo(AccountInterface $account); - - /** - * Reverts to a previous account after switching. - * - * @return \Drupal\Core\Session\AccountSwitcherInterface - * $this. - * - * @throws \RuntimeException - * When there are no more account switches to revert. - */ - public function switchBack(); - +interface AccountSwitcherInterface extends AuthenticationAccountSwitcherInterface { }