/** * @param \Magento\Customer\Controller\Ajax\Login $subject * @param \Closure $proceed * @return $this * @throws \Zend_Json_Exception * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundExecute(\Magento\Customer\Controller\Ajax\Login $subject, \Closure $proceed) { $captchaFormIdField = 'captcha_form_id'; $captchaInputName = 'captcha_string'; /** @var \Magento\Framework\App\RequestInterface $request */ $request = $subject->getRequest(); $loginParams = []; $content = $request->getContent(); if ($content) { $loginParams = \Zend_Json::decode($content); } $username = isset($loginParams['username']) ? $loginParams['username'] : null; $captchaString = isset($loginParams[$captchaInputName]) ? $loginParams[$captchaInputName] : null; $loginFormId = isset($loginParams[$captchaFormIdField]) ? $loginParams[$captchaFormIdField] : null; foreach ($this->formIds as $formId) { $captchaModel = $this->helper->getCaptcha($formId); if ($captchaModel->isRequired($username) && !in_array($loginFormId, $this->formIds)) { $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData(['errors' => true, 'message' => __('Provided form does not exist')]); } if ($formId == $loginFormId) { $captchaModel->logAttempt($username); if (!$captchaModel->isCorrect($captchaString)) { $this->sessionManager->setUsername($username); /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData(['errors' => true, 'message' => __('Incorrect CAPTCHA')]); } } } return $proceed(); }
/** * {@inheritdoc} */ public function dispatch(\Magento\Framework\App\RequestInterface $request) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'dispatch'); if (!$pluginInfo) { return parent::dispatch($request); } else { return $this->___callPlugins('dispatch', func_get_args(), $pluginInfo); } }
/** * @param \Magento\Customer\Controller\Ajax\Login $subject * @param callable $proceed * @return \Magento\Framework\Controller\ResultInterface * @throws \Zend_Json_Exception */ public function aroundExecute(\Magento\Customer\Controller\Ajax\Login $subject, \Closure $proceed) { $loginFormId = 'user_login'; $captchaInputName = 'captcha_string'; /** @var \Magento\Framework\App\RequestInterface $request */ $request = $subject->getRequest(); /** @var \Magento\Captcha\Model\ModelInterface $captchaModel */ $captchaModel = $this->helper->getCaptcha($loginFormId); $loginParams = \Zend_Json::decode($request->getContent()); $username = isset($loginParams['username']) ? $loginParams['username'] : null; $captchaString = isset($loginParams[$captchaInputName]) ? $loginParams[$captchaInputName] : null; if ($captchaModel->isRequired($username)) { $captchaModel->logAttempt($username); if (!$captchaModel->isCorrect($captchaString)) { $this->sessionManager->setUsername($username); /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData(['errors' => true, 'message' => __('Incorrect CAPTCHA')]); } } return $proceed(); }
public function testLoginFailure() { $jsonRequest = '{"username":"******", "password":"******"}'; $loginFailureResponse = '{"message":"Invalid login or password."}'; $this->request->expects($this->any())->method('getContent')->willReturn($jsonRequest); $this->request->expects($this->any())->method('getMethod')->willReturn('POST'); $this->request->expects($this->any())->method('isXmlHttpRequest')->willReturn(true); $this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson); $this->jsonHelperMock->expects($this->any())->method('jsonDecode')->with($jsonRequest)->willReturn(['username' => '*****@*****.**', 'password' => 'invalid']); $customerMock = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->customerAccountManagementMock->expects($this->any())->method('authenticate')->with('*****@*****.**', 'invalid')->willThrowException(new InvalidEmailOrPasswordException(__('Invalid login or password.'))); $this->customerSession->expects($this->never())->method('setCustomerDataAsLoggedIn')->with($customerMock); $this->customerSession->expects($this->never())->method('regenerateId'); $result = ['errors' => true, 'message' => __('Invalid login or password.')]; $this->resultJson->expects($this->once())->method('setData')->with($result)->willReturn($loginFailureResponse); $this->assertEquals($loginFailureResponse, $this->object->execute()); }