diff --git a/core/lib/Drupal/Core/TempStore/PrivateTempStore.php b/core/lib/Drupal/Core/TempStore/PrivateTempStore.php index 83bcd80afe..714021fd30 100644 --- a/core/lib/Drupal/Core/TempStore/PrivateTempStore.php +++ b/core/lib/Drupal/Core/TempStore/PrivateTempStore.php @@ -118,6 +118,18 @@ public function get($key) { * Thrown when a lock for the backend storage could not be acquired. */ public function set($key, $value) { + if ($this->currentUser->isAnonymous()) { + // Ensure that an anonymous user has a session created for them, as + // otherwise subsequent page loads will not be able to retrieve their + // tempstore data. Note this has to be done before the key is created as + // the owner is used in key creation. + $this->startSession(); + $session = $this->requestStack->getCurrentRequest()->getSession(); + if (!$session->has('core.tempstore.private.owner')) { + $session->set('core.tempstore.private.owner', Crypt::randomBytesBase64()); + } + } + $key = $this->createkey($key); if (!$this->lockBackend->acquire($key)) { $this->lockBackend->wait($key); @@ -210,14 +222,9 @@ protected function createkey($key) { protected function getOwner() { $owner = $this->currentUser->id(); if ($this->currentUser->isAnonymous()) { - // Ensure that an anonymous user has a session created for them, as - // otherwise subsequent page loads will not be able to retrieve their - // tempstore data. + // Check to see if an owner key exists in the session. $this->startSession(); $session = $this->requestStack->getCurrentRequest()->getSession(); - if (!$session->has('core.tempstore.private.owner')) { - $session->set('core.tempstore.private.owner', Crypt::randomBytesBase64()); - } $owner = $session->get('core.tempstore.private.owner'); } return $owner; diff --git a/core/lib/Drupal/Core/TempStore/SharedTempStore.php b/core/lib/Drupal/Core/TempStore/SharedTempStore.php index c131a80fd2..064ae1c1b2 100644 --- a/core/lib/Drupal/Core/TempStore/SharedTempStore.php +++ b/core/lib/Drupal/Core/TempStore/SharedTempStore.php @@ -147,7 +147,11 @@ public function setIfNotExists($key, $value) { 'data' => $value, 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'), ]; - return $this->storage->setWithExpireIfNotExists($key, $value, $this->expire); + $set = $this->storage->setWithExpireIfNotExists($key, $value, $this->expire); + if ($set) { + $this->ensureAnonymousSession(); + } + return $set; } /** @@ -205,6 +209,7 @@ public function set($key, $value) { 'data' => $value, 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'), ]; + $this->ensureAnonymousSession(); $this->storage->setWithExpire($key, $value, $this->expire); $this->lockBackend->release($key); } @@ -276,4 +281,15 @@ public function deleteIfOwner($key) { return FALSE; } + /** + * Stores the owner in session if the user is anonymous. + * + * This method should be called when a value is set. + */ + protected function ensureAnonymousSession() { + if (\Drupal::currentUser()->isAnonymous()) { + $this->requestStack->getCurrentRequest()->getSession()->set('core.tempstore.shared.owner', $this->owner); + } + } + } diff --git a/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php b/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php index a1a9e46319..438e8beb57 100644 --- a/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php +++ b/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php @@ -88,9 +88,8 @@ public function get($collection, $owner = NULL) { $session = \Drupal::service('session'); $this->requestStack->getCurrentRequest()->setSession($session); $session->start(); - $session->set('core.tempstore.shared.owner', Crypt::randomBytesBase64()); } - $owner = $this->requestStack->getCurrentRequest()->getSession()->get('core.tempstore.shared.owner'); + $owner = $this->requestStack->getCurrentRequest()->getSession()->get('core.tempstore.shared.owner', Crypt::randomBytesBase64()); } }