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

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. class CaptchaRouteListener implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var iterable<AbstractCaptcha>
  17.      */
  18.     private iterable $captchas;
  19.     private ErrorController $errorController;
  20.     private SystemConfigService $systemConfigService;
  21.     /**
  22.      * @internal
  23.      */
  24.     public function __construct(
  25.         iterable $captchas,
  26.         ErrorController $errorController,
  27.         SystemConfigService $systemConfigService
  28.     ) {
  29.         $this->captchas $captchas;
  30.         $this->errorController $errorController;
  31.         $this->systemConfigService $systemConfigService;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             KernelEvents::CONTROLLER => [
  40.                 ['validateCaptcha'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  41.             ],
  42.         ];
  43.     }
  44.     public function validateCaptcha(ControllerEvent $event): void
  45.     {
  46.         /** @var CaptchaAnnotation|bool $captchaAnnotation */
  47.         $captchaAnnotation $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CAPTCHAfalse);
  48.         if ($captchaAnnotation === false) {
  49.             return;
  50.         }
  51.         /** @var SalesChannelContext|null $context */
  52.         $context $event->getRequest()->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  53.         $salesChannelId $context $context->getSalesChannelId() : null;
  54.         $activeCaptchas = (array) ($this->systemConfigService->get('core.basicInformation.activeCaptchasV2'$salesChannelId) ?? []);
  55.         foreach ($this->captchas as $captcha) {
  56.             $captchaConfig $activeCaptchas[$captcha->getName()] ?? [];
  57.             $request $event->getRequest();
  58.             if (
  59.                 $captcha->supports($request$captchaConfig) && !$captcha->isValid($request$captchaConfig)
  60.             ) {
  61.                 if ($captcha->shouldBreak()) {
  62.                     throw new CaptchaInvalidException($captcha);
  63.                 }
  64.                 $violations $captcha->getViolations();
  65.                 $event->setController(function () use (
  66.                     $violations,
  67.                     $request
  68.                 ) {
  69.                     return $this->errorController->onCaptchaFailure($violations$request);
  70.                 });
  71.                 // Return on first invalid captcha
  72.                 return;
  73.             }
  74.         }
  75.     }
  76. }