[WIP] migrates SessionLockRequestComponent to new MiddlewareInterface

This commit is contained in:
Marvin Kuhl 2022-05-19 11:53:26 +02:00
parent d7dbff9fc2
commit 28c6c8ef59
4 changed files with 18 additions and 18 deletions

View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace DigiComp\FlowSessionLock\Aspects; namespace DigiComp\FlowSessionLock\Aspects;
use DigiComp\FlowSessionLock\Http\SessionLockRequestComponent; use DigiComp\FlowSessionLock\Http\SessionLockRequestMiddleware;
use Neos\Flow\Annotations as Flow; use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface; use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Core\Bootstrap; use Neos\Flow\Core\Bootstrap;
@ -52,9 +52,8 @@ class ReadOnlyAspect
$this->readOnly = true; $this->readOnly = true;
/** @var Lock|null $lock */ /** @var Lock|null $lock */
$lock = $activeRequestHandler->getComponentContext()->getParameter( $lock = $activeRequestHandler->getHttpRequest()->getAttribute(
SessionLockRequestComponent::class, SessionLockRequestMiddleware::class . '.' . SessionLockRequestMiddleware::PARAMETER_NAME
SessionLockRequestComponent::PARAMETER_NAME
); );
if ($lock !== null) { if ($lock !== null) {
$this->logger->debug('SessionLock: Release, as this is marked read only.'); $this->logger->debug('SessionLock: Release, as this is marked read only.');

View file

@ -5,14 +5,16 @@ declare(strict_types=1);
namespace DigiComp\FlowSessionLock\Http; namespace DigiComp\FlowSessionLock\Http;
use Neos\Flow\Annotations as Flow; use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Component\ComponentContext; use Psr\Http\Message\ResponseInterface;
use Neos\Flow\Http\Component\ComponentInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Lock\Exception\LockAcquiringException; use Symfony\Component\Lock\Exception\LockAcquiringException;
use Symfony\Component\Lock\Key; use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\LockFactory;
class SessionLockRequestComponent implements ComponentInterface class SessionLockRequestMiddleware implements MiddlewareInterface
{ {
public const PARAMETER_NAME = 'sessionLock'; public const PARAMETER_NAME = 'sessionLock';
@ -55,13 +57,13 @@ class SessionLockRequestComponent implements ComponentInterface
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function handle(ComponentContext $componentContext): void public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
$sessionCookieName = $this->sessionSettings['name']; $sessionCookieName = $this->sessionSettings['name'];
$cookies = $componentContext->getHttpRequest()->getCookieParams(); $cookies = $request->getCookieParams();
if (!isset($cookies[$sessionCookieName])) { if (!isset($cookies[$sessionCookieName])) {
return; return $handler->handle($request);
} }
// TODO: sessionIdentifier might be wrong, probably it should probably be storage identifier // TODO: sessionIdentifier might be wrong, probably it should probably be storage identifier
@ -69,7 +71,7 @@ class SessionLockRequestComponent implements ComponentInterface
$lock = $this->lockFactory->createLockFromKey($key, $this->timeToLive, $this->autoRelease); $lock = $this->lockFactory->createLockFromKey($key, $this->timeToLive, $this->autoRelease);
$componentContext->setParameter(SessionLockRequestComponent::class, static::PARAMETER_NAME, $lock); $request = $request->withAttribute(static::class . '.' . static::PARAMETER_NAME, $lock);
$this->logger->debug('SessionLock: Try to get "' . $key . '"'); $this->logger->debug('SessionLock: Try to get "' . $key . '"');
$timedOut = \time() + $this->secondsToWait; $timedOut = \time() + $this->secondsToWait;
@ -83,5 +85,6 @@ class SessionLockRequestComponent implements ComponentInterface
\usleep(100000); \usleep(100000);
} }
$this->logger->debug('SessionLock: Acquired "' . $key . '"'); $this->logger->debug('SessionLock: Acquired "' . $key . '"');
return $handler->handle($request);
} }
} }

View file

@ -9,9 +9,7 @@ DigiComp:
Neos: Neos:
Flow: Flow:
http: http:
chain: middlewares:
preprocess:
chain:
lockSession: lockSession:
position: "before getSessionCookieFromRequest" position: "before session"
component: "DigiComp\\FlowSessionLock\\Http\\SessionLockRequestComponent" middleware: "DigiComp\\FlowSessionLock\\Http\\SessionLockRequestMiddleware"

View file

@ -3,7 +3,7 @@
"description": "Session locking for Neos Flow - it secures the session becoming corrupted by concurrent access to the same session by different requests", "description": "Session locking for Neos Flow - it secures the session becoming corrupted by concurrent access to the same session by different requests",
"type": "neos-package", "type": "neos-package",
"require": { "require": {
"neos/flow": "^6.3.0", "neos/flow": "^7.3.0",
"php": ">=7.4", "php": ">=7.4",
"symfony/lock": "^5.2.0" "symfony/lock": "^5.2.0"
}, },