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

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