CheckUserLoginObserver.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Framework\Exception\NoSuchEntityException;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class CheckUserLoginObserver implements ObserverInterface
  15. {
  16. /**
  17. * @var \Magento\Captcha\Helper\Data
  18. */
  19. protected $_helper;
  20. /**
  21. * @var \Magento\Framework\App\ActionFlag
  22. */
  23. protected $_actionFlag;
  24. /**
  25. * @var \Magento\Framework\Message\ManagerInterface
  26. */
  27. protected $messageManager;
  28. /**
  29. * @var \Magento\Framework\Session\SessionManagerInterface
  30. */
  31. protected $_session;
  32. /**
  33. * @var CaptchaStringResolver
  34. */
  35. protected $captchaStringResolver;
  36. /**
  37. * Customer data
  38. *
  39. * @var \Magento\Customer\Model\Url
  40. */
  41. protected $_customerUrl;
  42. /**
  43. * @var CustomerRepositoryInterface
  44. */
  45. protected $customerRepository;
  46. /**
  47. * Authentication
  48. *
  49. * @var AuthenticationInterface
  50. */
  51. protected $authentication;
  52. /**
  53. * @param \Magento\Captcha\Helper\Data $helper
  54. * @param \Magento\Framework\App\ActionFlag $actionFlag
  55. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  56. * @param \Magento\Framework\Session\SessionManagerInterface $customerSession
  57. * @param CaptchaStringResolver $captchaStringResolver
  58. * @param \Magento\Customer\Model\Url $customerUrl
  59. */
  60. public function __construct(
  61. \Magento\Captcha\Helper\Data $helper,
  62. \Magento\Framework\App\ActionFlag $actionFlag,
  63. \Magento\Framework\Message\ManagerInterface $messageManager,
  64. \Magento\Framework\Session\SessionManagerInterface $customerSession,
  65. CaptchaStringResolver $captchaStringResolver,
  66. \Magento\Customer\Model\Url $customerUrl
  67. ) {
  68. $this->_helper = $helper;
  69. $this->_actionFlag = $actionFlag;
  70. $this->messageManager = $messageManager;
  71. $this->_session = $customerSession;
  72. $this->captchaStringResolver = $captchaStringResolver;
  73. $this->_customerUrl = $customerUrl;
  74. }
  75. /**
  76. * Get customer repository
  77. *
  78. * @return \Magento\Customer\Api\CustomerRepositoryInterface
  79. */
  80. private function getCustomerRepository()
  81. {
  82. if (!($this->customerRepository instanceof \Magento\Customer\Api\CustomerRepositoryInterface)) {
  83. return \Magento\Framework\App\ObjectManager::getInstance()->get(
  84. \Magento\Customer\Api\CustomerRepositoryInterface::class
  85. );
  86. } else {
  87. return $this->customerRepository;
  88. }
  89. }
  90. /**
  91. * Get authentication
  92. *
  93. * @return AuthenticationInterface
  94. */
  95. private function getAuthentication()
  96. {
  97. if (!($this->authentication instanceof AuthenticationInterface)) {
  98. return \Magento\Framework\App\ObjectManager::getInstance()->get(
  99. AuthenticationInterface::class
  100. );
  101. } else {
  102. return $this->authentication;
  103. }
  104. }
  105. /**
  106. * Check captcha on user login page
  107. *
  108. * @param \Magento\Framework\Event\Observer $observer
  109. * @throws NoSuchEntityException
  110. * @return $this
  111. */
  112. public function execute(\Magento\Framework\Event\Observer $observer)
  113. {
  114. $formId = 'user_login';
  115. $captchaModel = $this->_helper->getCaptcha($formId);
  116. $controller = $observer->getControllerAction();
  117. $loginParams = $controller->getRequest()->getPost('login');
  118. $login = (is_array($loginParams) && array_key_exists('username', $loginParams))
  119. ? $loginParams['username']
  120. : null;
  121. if ($captchaModel->isRequired($login)) {
  122. $word = $this->captchaStringResolver->resolve($controller->getRequest(), $formId);
  123. if (!$captchaModel->isCorrect($word)) {
  124. try {
  125. $customer = $this->getCustomerRepository()->get($login);
  126. $this->getAuthentication()->processAuthenticationFailure($customer->getId());
  127. } catch (NoSuchEntityException $e) {
  128. //do nothing as customer existance is validated later in authenticate method
  129. }
  130. $this->messageManager->addError(__('Incorrect CAPTCHA'));
  131. $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
  132. $this->_session->setUsername($login);
  133. $beforeUrl = $this->_session->getBeforeAuthUrl();
  134. $url = $beforeUrl ? $beforeUrl : $this->_customerUrl->getLoginUrl();
  135. $controller->getResponse()->setRedirect($url);
  136. }
  137. }
  138. $captchaModel->logAttempt($login);
  139. return $this;
  140. }
  141. }