vendor/shopware/storefront/Framework/Captcha/CaptchaRouteListener.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Captcha;
  3. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  4. use Shopware\Core\PlatformRequest;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Shopware\Storefront\Controller\ErrorController;
  8. use Shopware\Storefront\Framework\Captcha\Annotation\Captcha as CaptchaAnnotation;
  9. use Shopware\Storefront\Framework\Captcha\Exception\CaptchaInvalidException;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. /**
  14.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  15.  */
  16. class CaptchaRouteListener implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var iterable<AbstractCaptcha>
  20.      */
  21.     private iterable $captchas;
  22.     private ErrorController $errorController;
  23.     private SystemConfigService $systemConfigService;
  24.     /**
  25.      * @internal
  26.      *
  27.      * @param iterable<AbstractCaptcha> $captchas
  28.      */
  29.     public function __construct(
  30.         iterable $captchas,
  31.         ErrorController $errorController,
  32.         SystemConfigService $systemConfigService
  33.     ) {
  34.         $this->captchas $captchas;
  35.         $this->errorController $errorController;
  36.         $this->systemConfigService $systemConfigService;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             KernelEvents::CONTROLLER => [
  45.                 ['validateCaptcha'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  46.             ],
  47.         ];
  48.     }
  49.     public function validateCaptcha(ControllerEvent $event): void
  50.     {
  51.         /** @var CaptchaAnnotation|bool $captchaAnnotation */
  52.         $captchaAnnotation $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CAPTCHAfalse);
  53.         if ($captchaAnnotation === false) {
  54.             return;
  55.         }
  56.         /** @var SalesChannelContext|null $context */
  57.         $context $event->getRequest()->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  58.         $salesChannelId $context $context->getSalesChannelId() : null;
  59.         $activeCaptchas = (array) ($this->systemConfigService->get('core.basicInformation.activeCaptchasV2'$salesChannelId) ?? []);
  60.         foreach ($this->captchas as $captcha) {
  61.             $captchaConfig $activeCaptchas[$captcha->getName()] ?? [];
  62.             $request $event->getRequest();
  63.             if (
  64.                 $captcha->supports($request$captchaConfig) && !$captcha->isValid($request$captchaConfig)
  65.             ) {
  66.                 if ($captcha->shouldBreak()) {
  67.                     throw new CaptchaInvalidException($captcha);
  68.                 }
  69.                 $violations $captcha->getViolations();
  70.                 $event->setController(function () use (
  71.                     $violations,
  72.                     $request
  73.                 ) {
  74.                     return $this->errorController->onCaptchaFailure($violations$request);
  75.                 });
  76.                 // Return on first invalid captcha
  77.                 return;
  78.             }
  79.         }
  80.     }
  81. }