From 3da7a898244aeb3c2fafbca54d5f4e4106807273 Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Mon, 8 Apr 2019 13:45:59 +0200 Subject: [PATCH] fix robots filter session problem #1914 --- core/CHANGELOG.md | 3 ++- core/web/filters/RobotsFilter.php | 38 +++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 3f513496c..de5714403 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -2,8 +2,9 @@ All notable changes to this project will be documented in this file. This project make usage of the [Yii Versioning Strategy](https://github.com/yiisoft/yii2/blob/master/docs/internals/versions.md). In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md). -## 1.0.13 (in progress) +## 1.0.17 (in progress) ++ [#1914](https://github.com/luyadev/luya/issues/1914) Fixed problem with `RobotsFilter` when used in multiple forms on the same CMS page. + [#1912](https://github.com/luyadev/luya/issues/1912) Added `ObjectHelper::isTraitInstanceOf` method to check whether an object contains a certain trait or not. ## 1.0.16 (1. April 2019) diff --git a/core/web/filters/RobotsFilter.php b/core/web/filters/RobotsFilter.php index 18467002a..3a200b3c9 100644 --- a/core/web/filters/RobotsFilter.php +++ b/core/web/filters/RobotsFilter.php @@ -6,6 +6,7 @@ use yii\base\ActionFilter; use yii\base\InvalidCallException; use yii\helpers\VarDumper; +use yii\base\Controller; use luya\helpers\ArrayHelper; /** @@ -49,6 +50,12 @@ class RobotsFilter extends ActionFilter */ public $delay = 2.5; + /** + * @var string|null A string which identifiers the current robots filter in case you have multiple controllers on the same page with robot filters enabled. + * @since 1.0.17 + */ + public $sessionKey; + const ROBOTS_FILTER_SESSION_IDENTIFIER = '__robotsFilterRenderTime'; /** @@ -56,7 +63,13 @@ class RobotsFilter extends ActionFilter */ protected function getRenderTime() { - return Yii::$app->session->get(self::ROBOTS_FILTER_SESSION_IDENTIFIER, time()); + $value = Yii::$app->session->get(self::ROBOTS_FILTER_SESSION_IDENTIFIER, time()); + + if (isset($value[$this->getSessionKeyByOwner()])) { + return $value[$this->getSessionKeyByOwner]; + } + + return $value; } /** @@ -66,7 +79,28 @@ protected function getRenderTime() */ protected function setRenderTime($time) { - Yii::$app->session->set(self::ROBOTS_FILTER_SESSION_IDENTIFIER, $time); + Yii::$app->session->set(self::ROBOTS_FILTER_SESSION_IDENTIFIER, [$this->getSessionKeyByOwner() => $time]); + } + + /** + * Get a specific key for the current robots filter session array. + * + * This ensures that when multiple forms are on the same page, only the robot check is handeld for the given module name. + * + * @return string + * @since 1.0.17 + */ + protected function getSessionKeyByOwner() + { + if ($this->sessionKey) { + return $this->sessionKey; + } + + if ($this->owner instanceof Controller) { + return $this->owner->module->id; + } + + return 'generic'; } /**