CaptchaValidator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\SendFriend\Model;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Captcha\Helper\Data;
  11. use Magento\Captcha\Model\DefaultModel;
  12. use Magento\Captcha\Observer\CaptchaStringResolver;
  13. use Magento\Authorization\Model\UserContextInterface;
  14. use Magento\Customer\Api\CustomerRepositoryInterface;
  15. /**
  16. * Class CaptchaValidator. Performs captcha validation
  17. */
  18. class CaptchaValidator
  19. {
  20. /**
  21. * @var Data
  22. */
  23. private $captchaHelper;
  24. /**
  25. * @var CaptchaStringResolver
  26. */
  27. private $captchaStringResolver;
  28. /**
  29. * @var UserContextInterface
  30. */
  31. private $currentUser;
  32. /**
  33. * @var CustomerRepositoryInterface
  34. */
  35. private $customerRepository;
  36. /**
  37. * CaptchaValidator constructor.
  38. *
  39. * @param Data $captchaHelper
  40. * @param CaptchaStringResolver $captchaStringResolver
  41. * @param UserContextInterface $currentUser
  42. * @param CustomerRepositoryInterface $customerRepository
  43. */
  44. public function __construct(
  45. Data $captchaHelper,
  46. CaptchaStringResolver $captchaStringResolver,
  47. UserContextInterface $currentUser,
  48. CustomerRepositoryInterface $customerRepository
  49. ) {
  50. $this->captchaHelper = $captchaHelper;
  51. $this->captchaStringResolver = $captchaStringResolver;
  52. $this->currentUser = $currentUser;
  53. $this->customerRepository = $customerRepository;
  54. }
  55. /**
  56. * Entry point for captcha validation
  57. *
  58. * @param RequestInterface $request
  59. * @throws LocalizedException
  60. * @throws \Magento\Framework\Exception\NoSuchEntityException
  61. */
  62. public function validateSending(RequestInterface $request): void
  63. {
  64. $this->validateCaptcha($request);
  65. }
  66. /**
  67. * Validates captcha and triggers log attempt
  68. *
  69. * @param RequestInterface $request
  70. * @throws LocalizedException
  71. * @throws \Magento\Framework\Exception\NoSuchEntityException
  72. */
  73. private function validateCaptcha(RequestInterface $request): void
  74. {
  75. $captchaTargetFormName = 'product_sendtofriend_form';
  76. /** @var DefaultModel $captchaModel */
  77. $captchaModel = $this->captchaHelper->getCaptcha($captchaTargetFormName);
  78. if ($captchaModel->isRequired()) {
  79. $word = $this->captchaStringResolver->resolve(
  80. $request,
  81. $captchaTargetFormName
  82. );
  83. $isCorrectCaptcha = $captchaModel->isCorrect($word);
  84. if (!$isCorrectCaptcha) {
  85. $this->logCaptchaAttempt($captchaModel);
  86. throw new LocalizedException(__('Incorrect CAPTCHA'));
  87. }
  88. }
  89. $this->logCaptchaAttempt($captchaModel);
  90. }
  91. /**
  92. * Log captcha attempts
  93. *
  94. * @param DefaultModel $captchaModel
  95. * @throws LocalizedException
  96. * @throws \Magento\Framework\Exception\NoSuchEntityException
  97. */
  98. private function logCaptchaAttempt(DefaultModel $captchaModel): void
  99. {
  100. $email = '';
  101. if ($this->currentUser->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
  102. $email = $this->customerRepository->getById($this->currentUser->getUserId())->getEmail();
  103. }
  104. $captchaModel->logAttempt($email);
  105. }
  106. }