Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Use new RememberLoginHash::onAfterRenewSession extension point #214

Open
wants to merge 1 commit into
base: 3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Extensions/RememberLoginHashExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\Security\RememberLoginHash;
use SilverStripe\SessionManager\Models\LoginSession;
use SilverStripe\SessionManager\Security\LogInAuthenticationHandler;
use SilverStripe\SessionManager\Middleware\LoginSessionMiddleware;

/**
* @method LoginSession LoginSession()
Expand All @@ -33,9 +34,15 @@ protected function onAfterGenerateToken(): void
}

/**
* Overwrites the core session variable with the LoginSession record ID
* during session renewal when the user selects 'remember me' (ALC).
* This works in tandem with LoginSessionMiddleware, and avoids the
* overhead of an additional DB query.
*
* @see LoginSessionMiddleware
* @return void
*/
protected function onAfterRenewToken(): void
protected function onAfterRenewSession(): void
{
$loginHandler = Injector::inst()->get(LogInAuthenticationHandler::class);
$request = Injector::inst()->get(HTTPRequest::class);
Expand Down
2 changes: 2 additions & 0 deletions src/Middleware/LoginSessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function process(HTTPRequest $request, callable $delegate)
}

try {
// Extract the session identifier (when this module is installed, the session identifier is set to the
// LoginSession ID rather than the RememberLoginHash ID, to avoid an extra query to get the related model.)
$loginSessionID = $request->getSession()->get($loginHandler->getSessionVariable());
$loginSession = LoginSession::get_by_id($loginSessionID);

Expand Down
2 changes: 2 additions & 0 deletions src/Security/LogInAuthenticationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public function logIn(Member $member, $persistent = false, HTTPRequest $request
$rememberLoginHash->write();
}

// Overwrite the session identifier, storing the LoginSession ID instead of the RememberLoginHash ID.
// This is read by LoginSessionMiddleware, and avoids an extra query to fetch the related model.
if ($request) {
$request->getSession()->set($this->getSessionVariable(), $loginSession->ID);
}
Expand Down
Loading