<?php declare(strict_types=1);namespace DonCarneTheme\Subscriber;use Shopware\Core\Checkout\Customer\Exception\CustomerNotFoundByHashException;use Shopware\Core\Checkout\Customer\Exception\CustomerRecoveryHashExpiredException;use Shopware\Core\PlatformRequest;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ExceptionEvent;use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Twig\Environment;use Symfony\Component\HttpFoundation\Response;class PasswordRecoverySubscriber implements EventSubscriberInterface{ private UrlGeneratorInterface $urlGenerator; private Environment $twig; public function __construct( UrlGeneratorInterface $urlGenerator, Environment $twig ) { $this->urlGenerator = $urlGenerator; $this->twig = $twig; } public static function getSubscribedEvents(): array { return [ KernelEvents::EXCEPTION => ['onKernelException', 10], ]; } public function onKernelException(ExceptionEvent $event): void { $exception = $event->getThrowable(); $request = $event->getRequest(); // Prüfe ob es ein Passwort-Recovery-Request ist if (!$this->isPasswordRecoveryRequest($request)) { return; } // Prüfe ob es eine relevante Exception ist if (!$this->isPasswordRecoveryException($exception)) { return; } // Setze Flash-Message in Session $session = $request->getSession(); if ($session) { $session->getFlashBag()->add( 'danger', 'Der Link zum Zurücksetzen des Passworts ist ungültig oder abgelaufen. ' . 'Haben Sie eventuell eine weitere E-Mail zum Zurücksetzen des Passworts angefordert? ' . 'Links sind aus Sicherheitsgründen nur 2 Stunden gültig.' ); } // Redirect zur Recovery-Seite $url = $this->urlGenerator->generate('frontend.account.recover.page'); $event->setResponse(new RedirectResponse($url)); } private function isPasswordRecoveryRequest(Request $request): bool { $route = $request->attributes->get('_route'); // Prüfe ob es die Password-Recovery-Route ist if ($route === 'frontend.account.recover.password.page') { return true; } // Prüfe auch die URL $pathInfo = $request->getPathInfo(); if (strpos($pathInfo, '/account/recover/password') !== false) { return true; } return false; } private function isPasswordRecoveryException(\Throwable $exception): bool { // Prüfe auf spezifische Passwort-Recovery-Exceptions if ($exception instanceof CustomerNotFoundByHashException) { return true; } if ($exception instanceof CustomerRecoveryHashExpiredException) { return true; } // Prüfe auf 404 mit Hash-Parameter if ($exception instanceof NotFoundHttpException) { return true; } return false; }}