Added new onBeforeSessionStart() Event

This commit is contained in:
Andy Miller 2023-01-02 10:46:32 -07:00
parent 3f13d81c6f
commit 44c819b021
No known key found for this signature in database
GPG Key ID: 9F2CF38AEBDB0AE0
5 changed files with 55 additions and 7 deletions

View File

@ -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)

View File

@ -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);
}

View File

@ -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
*/

View File

@ -0,0 +1,36 @@
<?php
/**
* @package Grav\Events
*
* @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Events;
use Grav\Framework\Session\SessionInterface;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Plugins Loaded Event
*
* This event is called from $grav['session']->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;
}
}

View File

@ -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
{
}