diff --git a/core/core.services.yml b/core/core.services.yml index 14a3bcf..8e19754 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1095,6 +1095,8 @@ services: arguments: ['@current_user', '@session_handler.write_safe'] current_user: class: Drupal\Core\Session\AccountProxy + calls: + - [setContainer, ['@service_container']] session_configuration: class: Drupal\Core\Session\SessionConfiguration arguments: ['%session.storage.options%'] diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index ec0d2a7..ed62270 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -738,10 +738,7 @@ protected function initializeContainer($rebuild = FALSE) { } if (!empty($current_user_id)) { - $account = $this->container->get('entity.manager')->getStorage('user')->load($current_user_id); - if ($account) { - $this->container->get('current_user')->setAccount($account); - } + $this->container->get('current_user')->setInitialAccountId($current_user_id); } \Drupal::setContainer($this->container); diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Session/AccountProxy.php index ca671a4..6155326 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Session/AccountProxy.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Session; +use Symfony\Component\DependencyInjection\ContainerAwareTrait; + /** * A proxied implementation of AccountInterface. * @@ -18,7 +20,9 @@ * allows legacy code to change the current user where the user cannot be * directly injected into dependent code. */ -class AccountProxy implements AccountProxyInterface { +class AccountProxy implements AccountProxyInterface, ContainerAwareAccountProxyInterface { + + use ContainerAwareTrait; /** * The instantiated account. @@ -28,11 +32,11 @@ class AccountProxy implements AccountProxyInterface { protected $account; /** - * Constructs a new AccountProxy. + * Initial account id. + * + * @var int */ - public function __construct() { - $this->account = new AnonymousUserSession(); - } + protected $initialAccountId; /** * {@inheritdoc} @@ -50,6 +54,18 @@ public function setAccount(AccountInterface $account) { * {@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 such that + // it is possible to refresh the user account reference here. + $this->account = $this->container->get('entity.manager')->getStorage('user')->load($this->initialAccountId); + } + else { + $this->account = new AnonymousUserSession(); + } + } + return $this->account; } @@ -158,4 +174,15 @@ public function getLastAccessedTime() { return $this->getAccount()->getLastAccessedTime(); } + /** + * {@inheritdoc} + */ + public function setInitialAccountId($account_id) { + if (isset($this->account)) { + throw new \LogicException('ContainerAwareAccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy'); + } + + $this->initialAccountId = $account_id; + } + } diff --git a/core/lib/Drupal/Core/Session/AccountProxyInterface.php b/core/lib/Drupal/Core/Session/AccountProxyInterface.php index 017bd8b..3de1e2e 100644 --- a/core/lib/Drupal/Core/Session/AccountProxyInterface.php +++ b/core/lib/Drupal/Core/Session/AccountProxyInterface.php @@ -38,4 +38,3 @@ public function setAccount(AccountInterface $account); public function getAccount(); } - diff --git a/core/lib/Drupal/Core/Session/ContainerAwareAccountProxyInterface.php b/core/lib/Drupal/Core/Session/ContainerAwareAccountProxyInterface.php new file mode 100644 index 0000000..361ab71 --- /dev/null +++ b/core/lib/Drupal/Core/Session/ContainerAwareAccountProxyInterface.php @@ -0,0 +1,25 @@ +