diff --git a/core/core.services.yml b/core/core.services.yml index 5d51509..dbb27fb 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1200,6 +1200,8 @@ services: arguments: ['@private_key', '@cache.default'] current_user: class: Drupal\Core\Session\AccountProxy + tags: + - { name: needs_destruction } session_configuration: class: Drupal\Core\Session\SessionConfiguration arguments: ['%session.storage.options%'] diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Session/AccountProxy.php index de33340..bc36c46 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Session/AccountProxy.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Session; +use Drupal\Core\DestructableInterface; use Drupal\Core\Site\Settings; /** @@ -20,7 +21,7 @@ * 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, DestructableInterface { /** * The instantiated account. @@ -46,7 +47,6 @@ public function setAccount(AccountInterface $account) { $account = $account->getAccount(); } $this->account = $account; - $this->updateLastAccessedTime(); date_default_timezone_set(drupal_get_user_timezone()); } @@ -186,6 +186,18 @@ public function setInitialAccountId($account_id) { } /** + * {@inheritdoc} + */ + public function destruct() { + // Update last access time (no more than once per 180 seconds). + if ($this->account->isAuthenticated() && REQUEST_TIME - $this->account->getLastAccessedTime() > Settings::get('session_write_interval', 180)) { + /** @var \Drupal\user\UserStorageInterface $storage */ + $storage = \Drupal::entityManager()->getStorage('user'); + $storage->updateLastAccessTimestamp($this->account, REQUEST_TIME); + } + } + + /** * Load a user entity. * * The entity manager requires additional initialization code and cache @@ -208,16 +220,4 @@ protected function loadUserEntity($account_id) { return \Drupal::entityManager()->getStorage('user')->load($account_id); } - /** - * Updates the access field of the user entity. - */ - protected function updateLastAccessedTime() { - // Likewise, do not update access time more than once per 180 seconds. - if ($this->account->isAuthenticated() && REQUEST_TIME - $this->account->getLastAccessedTime() > Settings::get('session_write_interval', 180)) { - /** @var \Drupal\user\UserStorageInterface $storage */ - $storage = \Drupal::entityManager()->getStorage('user'); - $storage->updateLastAccessTimestamp($this->account, REQUEST_TIME); - } - } - }