/**
  * Authenticate user
  *
  * @param \Magento\Framework\App\ActionInterface $subject
  * @param RequestInterface $request
  * @return void
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
 {
     $loginUrl = $this->customerUrl->getLoginUrl();
     if (!$this->customerSession->authenticate($loginUrl)) {
         $subject->getActionFlag()->set('', $subject::FLAG_NO_DISPATCH, true);
     }
 }
Example #2
0
 /**
  * Retrieve back url
  *
  * @return string
  */
 public function getBackUrl()
 {
     $url = $this->getData('back_url');
     if ($url === null) {
         $url = $this->_customerUrl->getLoginUrl();
     }
     return $url;
 }
Example #3
0
 /**
  * Retrieve create new account url
  *
  * @return string
  */
 public function getCreateAccountUrl()
 {
     $url = $this->getData('create_account_url');
     if ($url === null) {
         $url = $this->_customerUrl->getRegisterUrl();
     }
     if ($this->checkoutData->isContextCheckout()) {
         $url = $this->coreUrl->addRequestParam($url, ['context' => 'checkout']);
     }
     return $url;
 }
 /**
  * Check customer authentication
  *
  * @param RequestInterface $request
  * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\App\ResponseInterface
  */
 public function dispatch(RequestInterface $request)
 {
     $loginUrl = $this->customerUrl->getLoginUrl();
     if (!$this->customerSession->authenticate($loginUrl)) {
         $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
     }
     if (!$this->config->useVault()) {
         $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
         /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
         $resultRedirect = $this->resultRedirectFactory->create();
         $resultRedirect->setPath('noRoute');
         return $resultRedirect;
     }
     return parent::dispatch($request);
 }
 /**
  * @return void
  */
 public function testExecute()
 {
     $formId = 'user_login';
     $login = '******';
     $loginParams = ['username' => $login];
     $customerId = 7;
     $redirectUrl = 'http://magento.com/customer/account/login/';
     $captchaValue = 'some-value';
     $captcha = $this->getMock('Magento\\Captcha\\Model\\DefaultModel', [], [], '', false);
     $captcha->expects($this->once())->method('isRequired')->with($login)->willReturn(true);
     $captcha->expects($this->once())->method('isCorrect')->with($captchaValue)->willReturn(false);
     $captcha->expects($this->once())->method('logAttempt')->with($login);
     $this->helperMock->expects($this->once())->method('getCaptcha')->with($formId)->willReturn($captcha);
     $response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
     $response->expects($this->once())->method('setRedirect')->with($redirectUrl);
     $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $request->expects($this->any())->method('getPost')->with('login')->willReturn($loginParams);
     $controller = $this->getMock('Magento\\Framework\\App\\Action\\Action', [], [], '', false);
     $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
     $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
     $this->captchaStringResolverMock->expects($this->once())->method('resolve')->with($request, $formId)->willReturn($captchaValue);
     $customerDataMock = $this->getMock('\\Magento\\Customer\\Model\\Data\\Customer', ['getId'], [], '', false);
     $customerDataMock->expects($this->once())->method('getId')->willReturn($customerId);
     $this->customerRepositoryMock->expects($this->once())->method('get')->with($login)->willReturn($customerDataMock);
     $this->authenticationMock->expects($this->once())->method('processAuthenticationFailure')->with($customerId);
     $this->messageManagerMock->expects($this->once())->method('addError')->with(__('Incorrect CAPTCHA'));
     $this->actionFlagMock->expects($this->once())->method('set')->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
     $this->customerSessionMock->expects($this->once())->method('setUsername')->with($login);
     $this->customerSessionMock->expects($this->once())->method('getBeforeAuthUrl')->willReturn(false);
     $this->customerUrlMock->expects($this->once())->method('getLoginUrl')->willReturn($redirectUrl);
     $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
 }
 /**
  * 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;
 }
 /**
  * Check Captcha On User Login Page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @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 = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
     if ($captchaModel->isRequired($login)) {
         $word = $this->captchaStringResolver->resolve($controller->getRequest(), $formId);
         if (!$captchaModel->isCorrect($word)) {
             $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;
 }
Example #8
0
 /**
  * Involve new customer to system
  *
  * @return $this
  */
 protected function _involveNewCustomer()
 {
     $customer = $this->getQuote()->getCustomer();
     $confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId());
     if ($confirmationStatus === \Magento\Customer\Model\AccountManagement::ACCOUNT_CONFIRMATION_REQUIRED) {
         $url = $this->_customerUrl->getEmailConfirmationUrl($customer->getEmail());
         $this->messageManager->addSuccess(__('You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.', $url));
     } else {
         $this->getCustomerSession()->loginById($customer->getId());
     }
     return $this;
 }
 /**
  * Prepare redirect URL for logged in customer
  *
  * Redirect customer to the last page visited after logging in.
  *
  * @return void
  */
 protected function processLoggedCustomer()
 {
     // Set default redirect URL for logged in customer
     $this->applyRedirect($this->customerUrl->getAccountUrl());
     if (!$this->scopeConfig->isSetFlag(CustomerUrl::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD, ScopeInterface::SCOPE_STORE)) {
         $referer = $this->request->getParam(CustomerUrl::REFERER_QUERY_PARAM_NAME);
         if ($referer) {
             $referer = $this->urlDecoder->decode($referer);
             if ($this->url->isOwnOriginUrl()) {
                 $this->applyRedirect($referer);
             }
         }
     } elseif ($this->session->getAfterAuthUrl()) {
         $this->applyRedirect($this->session->getAfterAuthUrl(true));
     }
 }
Example #10
0
 /**
  * Authenticate controller action by login customer
  *
  * @param   bool|null $loginUrl
  * @return  bool
  */
 public function authenticate($loginUrl = null)
 {
     if ($this->isLoggedIn()) {
         return true;
     }
     $this->setBeforeAuthUrl($this->_createUrl()->getUrl('*/*/*', ['_current' => true]));
     if (isset($loginUrl)) {
         $this->response->setRedirect($loginUrl);
     } else {
         $arguments = $this->_customerUrl->getLoginUrlParams();
         if ($this->_session->getCookieShouldBeReceived() && $this->_createUrl()->getUseSession()) {
             $arguments += ['_query' => [$this->sidResolver->getSessionIdQueryParam($this->_session) => $this->_session->getSessionId()]];
         }
         $this->response->setRedirect($this->_createUrl()->getUrl(\Magento\Customer\Model\Url::ROUTE_ACCOUNT_LOGIN, $arguments));
     }
     return false;
 }
 /**
  * @param $customerId
  * @param $customerEmail
  * @param $password
  * @param $confirmationStatus
  * @param $vatValidationEnabled
  * @param $addressType
  * @param $successMessage
  *
  * @dataProvider getSuccessMessageDataProvider
  */
 public function testSuccessMessage($customerId, $customerEmail, $password, $confirmationStatus, $vatValidationEnabled, $addressType, $successMessage)
 {
     $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
     $this->registration->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
     $this->customerUrl->expects($this->once())->method('getEmailConfirmationUrl')->will($this->returnValue($customerEmail));
     $this->customerSessionMock->expects($this->once())->method('regenerateId');
     $this->customerMock->expects($this->any())->method('getId')->will($this->returnValue($customerId));
     $this->customerMock->expects($this->any())->method('getEmail')->will($this->returnValue($customerEmail));
     $this->customerExtractorMock->expects($this->any())->method('extract')->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))->will($this->returnValue($this->customerMock));
     $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true));
     $this->requestMock->expects($this->any())->method('getPost')->will($this->returnValue(false));
     $this->requestMock->expects($this->any())->method('getParam')->willReturnMap([['password', null, $password], ['password_confirmation', null, $password], ['is_subscribed', false, true]]);
     $this->customerMock->expects($this->once())->method('setAddresses')->with($this->equalTo([]))->will($this->returnSelf());
     $this->accountManagement->expects($this->once())->method('createAccount')->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')->will($this->returnValue($this->customerMock));
     $this->accountManagement->expects($this->once())->method('getConfirmationStatus')->with($this->equalTo($customerId))->will($this->returnValue($confirmationStatus));
     $this->subscriberMock->expects($this->once())->method('subscribeCustomerById')->with($this->equalTo($customerId));
     $this->messageManagerMock->expects($this->any())->method('addSuccess')->with($this->stringContains($successMessage))->will($this->returnSelf());
     $this->addressHelperMock->expects($this->any())->method('isVatValidationEnabled')->will($this->returnValue($vatValidationEnabled));
     $this->addressHelperMock->expects($this->any())->method('getTaxCalculationAddressType')->will($this->returnValue($addressType));
     $this->model->execute();
 }
 /**
  * Redirect to login page
  *
  * @return void
  */
 public function redirectLogin()
 {
     $this->_actionFlag->set('', 'no-dispatch', true);
     $this->_customerSession->setBeforeAuthUrl($this->_redirect->getRefererUrl());
     $this->getResponse()->setRedirect($this->_urlHelper->addRequestParam($this->_customerUrl->getLoginUrl(), ['context' => 'checkout']));
 }
Example #13
0
 /**
  * Create customer account action
  *
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute()
 {
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
         $resultRedirect->setPath('*/*/');
         return $resultRedirect;
     }
     if (!$this->getRequest()->isPost()) {
         $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
         $resultRedirect->setUrl($this->_redirect->error($url));
         return $resultRedirect;
     }
     $this->session->regenerateId();
     try {
         $address = $this->extractAddress();
         $addresses = $address === null ? [] : [$address];
         $customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
         $customer->setAddresses($addresses);
         $password = $this->getRequest()->getParam('password');
         $confirmation = $this->getRequest()->getParam('password_confirmation');
         $redirectUrl = $this->session->getBeforeAuthUrl();
         $this->checkPasswordConfirmation($password, $confirmation);
         $customer = $this->accountManagement->createAccount($customer, $password, $redirectUrl);
         if ($this->getRequest()->getParam('is_subscribed', false)) {
             $this->subscriberFactory->create()->subscribeCustomerById($customer->getId());
         }
         $this->_eventManager->dispatch('customer_register_success', ['account_controller' => $this, 'customer' => $customer]);
         $confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId());
         if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
             $email = $this->customerUrl->getEmailConfirmationUrl($customer->getEmail());
             // @codingStandardsIgnoreStart
             $this->messageManager->addSuccess(__('You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.', $email));
             // @codingStandardsIgnoreEnd
             $url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
             $resultRedirect->setUrl($this->_redirect->success($url));
         } else {
             $this->session->setCustomerDataAsLoggedIn($customer);
             $this->messageManager->addSuccess($this->getSuccessMessage());
             $resultRedirect = $this->accountRedirect->getRedirect();
         }
         return $resultRedirect;
     } catch (StateException $e) {
         $url = $this->urlModel->getUrl('customer/account/forgotpassword');
         // @codingStandardsIgnoreStart
         $message = __('There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.', $url);
         // @codingStandardsIgnoreEnd
         $this->messageManager->addError($message);
     } catch (InputException $e) {
         $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
         foreach ($e->getErrors() as $error) {
             $this->messageManager->addError($this->escaper->escapeHtml($error->getMessage()));
         }
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('We can\'t save the customer.'));
     }
     $this->session->setCustomerFormData($this->getRequest()->getPostValue());
     $defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
     $resultRedirect->setUrl($this->_redirect->error($defaultUrl));
     return $resultRedirect;
 }
 public function testGetHrefLoggedOut()
 {
     $this->httpContext->expects($this->once())->method('getValue')->will($this->returnValue(false));
     $this->_customerUrl->expects($this->once())->method('getLoginUrl')->will($this->returnValue('login url'));
     $this->assertEquals('login url', $this->_block->getHref());
 }
 /**
  * @return string
  */
 public function getHref()
 {
     return $this->isLoggedIn() ? $this->_customerUrl->getLogoutUrl() : $this->_customerUrl->getLoginUrl();
 }
Example #16
0
 /**
  * @return string
  */
 public function getHref()
 {
     return $this->_customerUrl->getAccountUrl();
 }
 /**
  * Get login URL
  *
  * @return string
  */
 public function getLoginUrl()
 {
     return $this->customerUrl->getLoginUrl();
 }
Example #18
0
 /**
  * Retrieve password forgotten url
  *
  * @return string
  */
 public function getForgotPasswordUrl()
 {
     return $this->_customerUrl->getForgotPasswordUrl();
 }
 /**
  * Return forgot password URL
  *
  * @return string
  * @codeCoverageIgnore
  */
 private function getForgotPasswordUrl()
 {
     return $this->customerUrlManager->getForgotPasswordUrl();
 }
Example #20
0
 /**
  * Return register URL
  *
  * @return string
  */
 public function getRegisterUrl()
 {
     return $this->customerUrl->getRegisterUrl();
 }
Example #21
0
 /**
  * Involve new customer to system
  *
  * @return $this
  */
 protected function _involveNewCustomer()
 {
     $customer = $this->_quote->getCustomer();
     $confirmationStatus = $this->_accountManagement->getConfirmationStatus($customer->getId());
     if ($confirmationStatus === AccountManagement::ACCOUNT_CONFIRMATION_REQUIRED) {
         $url = $this->_customerUrl->getEmailConfirmationUrl($customer->getEmail());
         $this->_messageManager->addSuccess(__('Account confirmation is required. Please check your email for confirmation link. To resend confirmation email please <a href="%1">click here</a>.', $url));
     } else {
         $this->getCustomerSession()->regenerateId();
         $this->getCustomerSession()->loginById($customer->getId());
     }
     return $this;
 }