diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b2a6db5..79cb95da1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ 1. [](#improved) * Updated `bin/composer.phar` to latest `2.4.4` version [#3627](https://github.com/getgrav/grav/issues/3627) 1. [](#bugfix) - * Get around a bug where messages were not always being set on session during redirect + * New `onBeforeSessionStart()` event to be used to store data lost during session regeneration (e.g. login) * Don't fail hard if pages recurse with same path * Github workflows security hardening [#3624](https://github.com/getgrav/grav/pull/3624) diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index c3d570c3e..d2fce7351 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -439,12 +439,6 @@ class Grav extends Container { $response = $this->getRedirectResponse($route, $code); - $session = $this['session'] ?? null; - $messages = $this['messages'] ?? null; - if (!isset($session->messages) && $session && $messages) { - $session->messages = $messages; - } - $this->close($response); } diff --git a/system/src/Grav/Common/Session.php b/system/src/Grav/Common/Session.php index 513143e09..6dbee000a 100644 --- a/system/src/Grav/Common/Session.php +++ b/system/src/Grav/Common/Session.php @@ -10,6 +10,7 @@ namespace Grav\Common; use Grav\Common\Form\FormFlash; +use Grav\Events\BeforeSessionStartEvent; use Grav\Events\SessionStartEvent; use Grav\Plugin\Form\Forms; use JsonException; @@ -177,6 +178,17 @@ class Session extends \Grav\Framework\Session\Session return null; } + /** + * @return void + */ + protected function onBeforeSessionStart(): void + { + $event = new BeforeSessionStartEvent($this); + + $grav = Grav::instance(); + $grav->dispatchEvent($event); + } + /** * @return void */ diff --git a/system/src/Grav/Events/BeforeSessionStartEvent.php b/system/src/Grav/Events/BeforeSessionStartEvent.php new file mode 100644 index 000000000..fc78304bf --- /dev/null +++ b/system/src/Grav/Events/BeforeSessionStartEvent.php @@ -0,0 +1,36 @@ +start() right before session_start() call. + * + * @property SessionInterface $session Session instance. + */ +class BeforeSessionStartEvent extends Event +{ + /** @var SessionInterface */ + public $session; + + public function __construct(SessionInterface $session) + { + $this->session = $session; + } + + public function __debugInfo(): array + { + return (array)$this; + } +} diff --git a/system/src/Grav/Framework/Session/Session.php b/system/src/Grav/Framework/Session/Session.php index 108563e37..d1ca7e2ad 100644 --- a/system/src/Grav/Framework/Session/Session.php +++ b/system/src/Grav/Framework/Session/Session.php @@ -317,6 +317,8 @@ class Session implements SessionInterface $this->removeCookie(); + $this->onBeforeSessionStart(); + $success = @session_start($this->options); if (!$success) { $last = error_get_last(); @@ -456,6 +458,10 @@ class Session implements SessionInterface return \PHP_SAPI !== 'cli' ? \PHP_SESSION_ACTIVE === session_status() : false; } + protected function onBeforeSessionStart(): void + { + } + protected function onSessionStart(): void { }