platform/src/Storefront/Framework/Captcha/CaptchaRouteListener.php line 49

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.     public function __construct(
  22.         iterable $captchas,
  23.         ErrorController $errorController,
  24.         SystemConfigService $systemConfigService
  25.     ) {
  26.         $this->captchas $captchas;
  27.         $this->errorController $errorController;
  28.         $this->systemConfigService $systemConfigService;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             KernelEvents::CONTROLLER => [
  37.                 ['validateCaptcha'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  38.             ],
  39.         ];
  40.     }
  41.     public function validateCaptcha(ControllerEvent $event): void
  42.     {
  43.         /** @var CaptchaAnnotation|bool $captchaAnnotation */
  44.         $captchaAnnotation $event->getRequest()->attributes->get('_captcha'false);
  45.         if ($captchaAnnotation === false) {
  46.             return;
  47.         }
  48.         if (!($captchaAnnotation instanceof CaptchaAnnotation)) {
  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. }