2022-12-09 17:31:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\EventListener;
|
|
|
|
|
|
|
|
use App\Hydra\HydraService;
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
use Symfony\Component\Security\Http\Event\LogoutEvent;
|
|
|
|
|
|
|
|
class LogoutSubscriber implements EventSubscriberInterface
|
|
|
|
{
|
2022-12-14 16:38:46 +01:00
|
|
|
private HydraService $hydra;
|
|
|
|
|
2022-12-09 17:31:07 +01:00
|
|
|
public function __construct(
|
2022-12-14 16:38:46 +01:00
|
|
|
HydraService $hydra
|
2022-12-09 17:31:07 +01:00
|
|
|
) {
|
2022-12-14 16:38:46 +01:00
|
|
|
$this->hydra = $hydra;
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubscribedEvents(): array
|
|
|
|
{
|
|
|
|
return [LogoutEvent::class => 'onLogout'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onLogout(LogoutEvent $event): void
|
|
|
|
{
|
|
|
|
// get the security token of the session that is about to be logged out
|
|
|
|
// get the current request
|
|
|
|
$request = $event->getRequest();
|
|
|
|
|
|
|
|
// get the current response, if it is already set by another listener
|
2022-12-13 15:46:24 +01:00
|
|
|
$request->getSession()->clear();
|
|
|
|
$request->getSession()->invalidate();
|
2022-12-09 17:31:07 +01:00
|
|
|
// configure a custom logout response to the homepage
|
|
|
|
$response = $this->hydra->handleLogoutRequest($request);
|
2022-12-13 15:46:24 +01:00
|
|
|
|
2022-12-09 17:31:07 +01:00
|
|
|
$event->setResponse($response);
|
|
|
|
}
|
|
|
|
}
|