_helper = $helper; $this->_actionFlag = $actionFlag; $this->messageManager = $messageManager; $this->_session = $customerSession; $this->captchaStringResolver = $captchaStringResolver; $this->_customerUrl = $customerUrl; } /** * Get customer repository * * @return \Magento\Customer\Api\CustomerRepositoryInterface */ private function getCustomerRepository() { if (!($this->customerRepository instanceof \Magento\Customer\Api\CustomerRepositoryInterface)) { return \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Customer\Api\CustomerRepositoryInterface::class ); } else { return $this->customerRepository; } } /** * Get authentication * * @return AuthenticationInterface */ private function getAuthentication() { if (!($this->authentication instanceof AuthenticationInterface)) { return \Magento\Framework\App\ObjectManager::getInstance()->get( AuthenticationInterface::class ); } else { return $this->authentication; } } /** * Check captcha on user login page * * @param \Magento\Framework\Event\Observer $observer * @throws NoSuchEntityException * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $formId = 'user_login'; $captchaModel = $this->_helper->getCaptcha($formId); $controller = $observer->getControllerAction(); $loginParams = $controller->getRequest()->getPost('login'); $login = (is_array($loginParams) && array_key_exists('username', $loginParams)) ? $loginParams['username'] : null; if ($captchaModel->isRequired($login)) { $word = $this->captchaStringResolver->resolve($controller->getRequest(), $formId); if (!$captchaModel->isCorrect($word)) { try { $customer = $this->getCustomerRepository()->get($login); $this->getAuthentication()->processAuthenticationFailure($customer->getId()); } catch (NoSuchEntityException $e) { //do nothing as customer existance is validated later in authenticate method } $this->messageManager->addError(__('Incorrect CAPTCHA')); $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true); $this->_session->setUsername($login); $beforeUrl = $this->_session->getBeforeAuthUrl(); $url = $beforeUrl ? $beforeUrl : $this->_customerUrl->getLoginUrl(); $controller->getResponse()->setRedirect($url); } } $captchaModel->logAttempt($login); return $this; } }