Skip to content

Commit

Permalink
[FEATURE] Provide PSR-14 event to modify redirect URL parameters
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
derhansen committed Jul 15, 2024
1 parent 59c2d8f commit 67579b3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
50 changes: 50 additions & 0 deletions Classes/Event/ModifyRedirectUrlParameterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Derhansen\FeChangePwd\Event;

use Psr\Http\Message\ServerRequestInterface;

/**
* This event is triggered before the change password redirect URL is created. Listeners can use this event
* to change the parameters and the redirectPid used to construct the redirect URL
*/
final class ModifyRedirectUrlParameterEvent
{
private ServerRequestInterface $request;
private int $redirectPid;
private array $parameter;

public function __construct(ServerRequestInterface $request, int $redirectPid, array $parameter)
{
$this->request = $request;
$this->redirectPid = $redirectPid;
$this->parameter = $parameter;
}

public function getRequest(): ServerRequestInterface
{
return $this->request;
}

public function getRedirectPid(): int
{
return $this->redirectPid;
}

public function setRedirectPid(int $redirectPid): void
{
$this->redirectPid = $redirectPid;
}

public function getParameter(): array
{
return $this->parameter;
}

public function setParameter(array $parameter): void
{
$this->parameter = $parameter;
}
}
15 changes: 11 additions & 4 deletions Classes/Middleware/ForcePasswordChangeRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Derhansen\FeChangePwd\Middleware;

use Derhansen\FeChangePwd\Event\ModifyRedirectUrlParameterEvent;
use Derhansen\FeChangePwd\Service\FrontendUserService;
use Derhansen\FeChangePwd\Service\PageAccessService;
use Derhansen\FeChangePwd\Service\SettingsService;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand All @@ -30,7 +32,8 @@ class ForcePasswordChangeRedirect implements MiddlewareInterface
public function __construct(
protected PageAccessService $pageAccessService,
protected FrontendUserService $frontendUserService,
protected SettingsService $settingsService
protected SettingsService $settingsService,
protected EventDispatcherInterface $eventDispatcher
) {}

/**
Expand Down Expand Up @@ -79,9 +82,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$site = $request->getAttribute('site');
$router = $site->getRouter();

$parameter = ['_language' => $language];
// @todo: Provide PSR-14 event to modify parameter
$url = (string)$router->generateUri($redirectPid, $parameter);
$event = $this->eventDispatcher->dispatch(new ModifyRedirectUrlParameterEvent(
$request,
$redirectPid,
['_language' => $language]
));

$url = (string)$router->generateUri($event->getRedirectPid(), $event->getParameter());
return new RedirectResponse($url, 307);
}

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ for version 1.5.0

### PSR-14 events

The extension currently contains the following PSR-14 event:
The extension currently contains the following PSR-14 events:

* Derhansen\FeChangePwd\Controller\PasswordController
* `AfterPasswordUpdatedEvent`
* Derhansen\FeChangePwd\Middleware\ForcePasswordChangeRedirect
* `ModifyRedirectUrlParameterEvent`

Additionally, the extension also dispatches the TYPO3 core PSR-14 event
`TYPO3\CMS\Core\PasswordPolicy\Event\EnrichPasswordValidationContextDataEvent`
Expand Down

0 comments on commit 67579b3

Please sign in to comment.