custom/plugins/theme/src/Subscriber/PasswordRecoverySubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DonCarneTheme\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Exception\CustomerNotFoundByHashException;
  4. use Shopware\Core\Checkout\Customer\Exception\CustomerRecoveryHashExpiredException;
  5. use Shopware\Core\PlatformRequest;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Twig\Environment;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class PasswordRecoverySubscriber implements EventSubscriberInterface
  16. {
  17.     private UrlGeneratorInterface $urlGenerator;
  18.     private Environment $twig;
  19.     public function __construct(
  20.         UrlGeneratorInterface $urlGenerator,
  21.         Environment $twig
  22.     ) {
  23.         $this->urlGenerator $urlGenerator;
  24.         $this->twig $twig;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::EXCEPTION => ['onKernelException'10],
  30.         ];
  31.     }
  32.     public function onKernelException(ExceptionEvent $event): void
  33.     {
  34.         $exception $event->getThrowable();
  35.         $request $event->getRequest();
  36.         // Prüfe ob es ein Passwort-Recovery-Request ist
  37.         if (!$this->isPasswordRecoveryRequest($request)) {
  38.             return;
  39.         }
  40.         // Prüfe ob es eine relevante Exception ist
  41.         if (!$this->isPasswordRecoveryException($exception)) {
  42.             return;
  43.         }
  44.         // Setze Flash-Message in Session
  45.         $session $request->getSession();
  46.         if ($session) {
  47.             $session->getFlashBag()->add(
  48.                 'danger',
  49.                 'Der Link zum Zurücksetzen des Passworts ist ungültig oder abgelaufen. ' .
  50.                 'Haben Sie eventuell eine weitere E-Mail zum Zurücksetzen des Passworts angefordert? ' .
  51.                 'Links sind aus Sicherheitsgründen nur 2 Stunden gültig.'
  52.             );
  53.         }
  54.         // Redirect zur Recovery-Seite
  55.         $url $this->urlGenerator->generate('frontend.account.recover.page');
  56.         $event->setResponse(new RedirectResponse($url));
  57.     }
  58.     private function isPasswordRecoveryRequest(Request $request): bool
  59.     {
  60.         $route $request->attributes->get('_route');
  61.         // Prüfe ob es die Password-Recovery-Route ist
  62.         if ($route === 'frontend.account.recover.password.page') {
  63.             return true;
  64.         }
  65.         // Prüfe auch die URL
  66.         $pathInfo $request->getPathInfo();
  67.         if (strpos($pathInfo'/account/recover/password') !== false) {
  68.             return true;
  69.         }
  70.         return false;
  71.     }
  72.     private function isPasswordRecoveryException(\Throwable $exception): bool
  73.     {
  74.         // Prüfe auf spezifische Passwort-Recovery-Exceptions
  75.         if ($exception instanceof CustomerNotFoundByHashException) {
  76.             return true;
  77.         }
  78.         if ($exception instanceof CustomerRecoveryHashExpiredException) {
  79.             return true;
  80.         }
  81.         // Prüfe auf 404 mit Hash-Parameter
  82.         if ($exception instanceof NotFoundHttpException) {
  83.             return true;
  84.         }
  85.         return false;
  86.     }
  87. }