CheckUserEditObserver.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Observer;
  7. use Magento\Customer\Model\AuthenticationInterface;
  8. use Magento\Framework\Event\ObserverInterface;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class CheckUserEditObserver implements ObserverInterface
  15. {
  16. /**
  17. * Form ID
  18. */
  19. const FORM_ID = 'user_edit';
  20. /**
  21. * @var \Magento\Captcha\Helper\Data
  22. */
  23. protected $helper;
  24. /**
  25. * @var \Magento\Framework\App\ActionFlag
  26. */
  27. protected $actionFlag;
  28. /**
  29. * @var \Magento\Framework\Message\ManagerInterface
  30. */
  31. protected $messageManager;
  32. /**
  33. * @var \Magento\Framework\App\Response\RedirectInterface
  34. */
  35. protected $redirect;
  36. /**
  37. * @var CaptchaStringResolver
  38. */
  39. protected $captchaStringResolver;
  40. /**
  41. * Authentication
  42. *
  43. * @var AuthenticationInterface
  44. */
  45. protected $authentication;
  46. /**
  47. * @var Session
  48. */
  49. protected $customerSession;
  50. /**
  51. * @var ScopeConfigInterface
  52. */
  53. protected $scopeConfig;
  54. /**
  55. * @param \Magento\Captcha\Helper\Data $helper
  56. * @param \Magento\Framework\App\ActionFlag $actionFlag
  57. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  58. * @param \Magento\Framework\App\Response\RedirectInterface $redirect
  59. * @param CaptchaStringResolver $captchaStringResolver
  60. * @param AuthenticationInterface $authentication
  61. * @param Session $customerSession
  62. * @param ScopeConfigInterface $scopeConfig
  63. */
  64. public function __construct(
  65. \Magento\Captcha\Helper\Data $helper,
  66. \Magento\Framework\App\ActionFlag $actionFlag,
  67. \Magento\Framework\Message\ManagerInterface $messageManager,
  68. \Magento\Framework\App\Response\RedirectInterface $redirect,
  69. CaptchaStringResolver $captchaStringResolver,
  70. AuthenticationInterface $authentication,
  71. Session $customerSession,
  72. ScopeConfigInterface $scopeConfig
  73. ) {
  74. $this->helper = $helper;
  75. $this->actionFlag = $actionFlag;
  76. $this->messageManager = $messageManager;
  77. $this->redirect = $redirect;
  78. $this->captchaStringResolver = $captchaStringResolver;
  79. $this->authentication = $authentication;
  80. $this->customerSession = $customerSession;
  81. $this->scopeConfig = $scopeConfig;
  82. }
  83. /**
  84. * Check Captcha On Forgot Password Page
  85. *
  86. * @param \Magento\Framework\Event\Observer $observer
  87. * @return $this
  88. */
  89. public function execute(\Magento\Framework\Event\Observer $observer)
  90. {
  91. $captchaModel = $this->helper->getCaptcha(self::FORM_ID);
  92. if ($captchaModel->isRequired()) {
  93. /** @var \Magento\Framework\App\Action\Action $controller */
  94. $controller = $observer->getControllerAction();
  95. if (!$captchaModel->isCorrect(
  96. $this->captchaStringResolver->resolve(
  97. $controller->getRequest(),
  98. self::FORM_ID
  99. )
  100. )) {
  101. $customerId = $this->customerSession->getCustomerId();
  102. $this->authentication->processAuthenticationFailure($customerId);
  103. if ($this->authentication->isLocked($customerId)) {
  104. $this->customerSession->logout();
  105. $this->customerSession->start();
  106. $message = __(
  107. 'The account is locked. Please wait and try again or contact %1.',
  108. $this->scopeConfig->getValue('contact/email/recipient_email')
  109. );
  110. $this->messageManager->addError($message);
  111. }
  112. $this->messageManager->addError(__('Incorrect CAPTCHA'));
  113. $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
  114. $this->redirect->redirect($controller->getResponse(), '*/*/edit');
  115. }
  116. }
  117. $customer = $this->customerSession->getCustomer();
  118. $login = $customer->getEmail();
  119. $captchaModel->logAttempt($login);
  120. return $this;
  121. }
  122. }