/** * @dataProvider getRedirectDataProvider * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function testGetRedirect($customerId, $lastCustomerId, $referer, $baseUrl, $beforeAuthUrl, $afterAuthUrl, $accountUrl, $loginUrl, $logoutUrl, $dashboardUrl, $customerLoggedIn, $redirectToDashboard) { // Preparations for method updateLastCustomerId() $this->customerSession->expects($this->once())->method('getLastCustomerId')->willReturn($customerId); $this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn($customerLoggedIn); $this->customerSession->expects($this->any())->method('getId')->willReturn($lastCustomerId); $this->customerSession->expects($this->any())->method('unsBeforeAuthUrl')->willReturnSelf(); $this->customerSession->expects($this->any())->method('setLastCustomerId')->with($lastCustomerId)->willReturnSelf(); // Preparations for method prepareRedirectUrl() $this->store->expects($this->once())->method('getBaseUrl')->willReturn($baseUrl); $this->customerSession->expects($this->any())->method('getBeforeAuthUrl')->willReturn($beforeAuthUrl); $this->customerSession->expects($this->any())->method('setBeforeAuthUrl')->willReturnSelf(); $this->customerSession->expects($this->any())->method('getAfterAuthUrl')->willReturn($afterAuthUrl); $this->customerSession->expects($this->any())->method('setAfterAuthUrl')->with($beforeAuthUrl)->willReturnSelf(); $this->customerUrl->expects($this->any())->method('getAccountUrl')->willReturn($accountUrl); $this->customerUrl->expects($this->any())->method('getLoginUrl')->willReturn($loginUrl); $this->customerUrl->expects($this->any())->method('getLogoutUrl')->willReturn($logoutUrl); $this->customerUrl->expects($this->any())->method('DashboardUrl')->willReturn($dashboardUrl); $this->scopeConfig->expects($this->any())->method('isSetFlag')->with(CustomerUrl::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD, ScopeInterface::SCOPE_STORE)->willReturn($redirectToDashboard); $this->request->expects($this->any())->method('getParam')->with(CustomerUrl::REFERER_QUERY_PARAM_NAME)->willReturn($referer); $this->urlDecoder->expects($this->any())->method('decode')->with($referer)->willReturn($referer); $this->url->expects($this->any())->method('isOwnOriginUrl')->willReturn(true); $this->resultRedirect->expects($this->once())->method('setUrl')->willReturnSelf(); $this->model->getRedirect(); }
/** * Login post action * * @return \Magento\Framework\Controller\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if ($this->_getSession()->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('*/*/'); return $resultRedirect; } if ($this->getRequest()->isPost()) { $login = $this->getRequest()->getPost('login'); if (!empty($login['username']) && !empty($login['password'])) { try { $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']); $this->_getSession()->setCustomerDataAsLoggedIn($customer); $this->_getSession()->regenerateId(); } catch (EmailNotConfirmedException $e) { $value = $this->customerUrl->getEmailConfirmationUrl($login['username']); $message = __('This account is not confirmed.' . ' <a href="%1">Click here</a> to resend confirmation email.', $value); $this->messageManager->addError($message); $this->_getSession()->setUsername($login['username']); } catch (AuthenticationException $e) { $message = __('Invalid login or password.'); $this->messageManager->addError($message); $this->_getSession()->setUsername($login['username']); } catch (\Exception $e) { $this->messageManager->addError(__('There was an error validating the login and password.')); } } else { $this->messageManager->addError(__('Login and password are required.')); } } return $this->accountRedirect->getRedirect(); }
/** * Login post action * * @return \Magento\Framework\Controller\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('*/*/'); return $resultRedirect; } if ($this->getRequest()->isPost()) { $login = $this->getRequest()->getPost('login'); if (!empty($login['username']) && !empty($login['password'])) { try { $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']); $this->session->setCustomerDataAsLoggedIn($customer); $this->session->regenerateId(); if ($this->getCookieManager()->getCookie('mage-cache-sessid')) { $metadata = $this->getCookieMetadataFactory()->createCookieMetadata(); $metadata->setPath('/'); $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata); } $redirectUrl = $this->accountRedirect->getRedirectCookie(); if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) { $this->accountRedirect->clearRedirectCookie(); $resultRedirect = $this->resultRedirectFactory->create(); // URL is checked to be internal in $this->_redirect->success() $resultRedirect->setUrl($this->_redirect->success($redirectUrl)); return $resultRedirect; } } catch (EmailNotConfirmedException $e) { $value = $this->customerUrl->getEmailConfirmationUrl($login['username']); $message = __('This account is not confirmed. <a href="%1">Click here</a> to resend confirmation email.', $value); $this->messageManager->addError($message); $this->session->setUsername($login['username']); } catch (UserLockedException $e) { $message = __('The account is locked. Please wait and try again or contact %1.', $this->getScopeConfig()->getValue('contact/email/recipient_email')); $this->messageManager->addError($message); $this->session->setUsername($login['username']); } catch (AuthenticationException $e) { $message = __('Invalid login or password.'); $this->messageManager->addError($message); $this->session->setUsername($login['username']); } catch (LocalizedException $e) { $message = $e->getMessage(); $this->messageManager->addError($message); $this->session->setUsername($login['username']); } catch (\Exception $e) { // PA DSS violation: throwing or logging an exception here can disclose customer password $this->messageManager->addError(__('An unspecified error occurred. Please contact us for assistance.')); } } else { $this->messageManager->addError(__('A login and a password are required.')); } } return $this->accountRedirect->getRedirect(); }
public function execute() { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); // redirect logged in user to the dashboard if ($this->getSession()->isLoggedIn() || !$this->registration->isAllowed()) { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('customer/account/index'); return $resultRedirect; } try { /* @var $requestData \BelVG\FacebookFree\Model\Request */ $requestData = $this->_objectManager->create('BelVG\\FacebookFree\\Model\\Request'); $fbData = $requestData->load(); $fbData['store_id'] = $this->storeManager->getStore()->getId(); $fbData['website_id'] = $this->storeManager->getStore()->getWebsiteId(); /* @var $customer \BelVG\FacebookFree\Model\Customer */ $customer = $this->_objectManager->create('BelVG\\FacebookFree\\Model\\Customer')->setFbData($fbData); /* @var $facebook \BelVG\FacebookFree\Model\FacebookFree */ $facebook = $this->_objectManager->create('BelVG\\FacebookFree\\Model\\FacebookFree'); if (!($customerId = $facebook->checkFbCustomer($customer->getFbData()))) { if (!($customerId = $customer->checkCustomer())) { // create customer $customerModel = $this->createCustomer($customer); $customerId = $customerModel->getId(); $this->messageManager->addSuccess($this->getSuccessMessage()); } if (!isset($customerModel)) { $customerModel = $customer->load($customerId); } $fbData['customer_id'] = $customerId; // create record about FB user $facebook->prepareData($fbData)->save(); $this->getSession()->setCustomerDataAsLoggedIn($customerModel); } else { $customerModel = $customer->load($customerId); $this->getSession()->setCustomerAsLoggedIn($customerModel); } $resultRedirect = $this->accountRedirect->getRedirect(); } catch (\Exception $e) { $this->logger->error($e->getMessage()); $this->messageManager->addError($e->getMessage()); $url = $this->urlModel->getUrl('customer/account/login', ['_secure' => true]); $resultRedirect->setUrl($url); return $resultRedirect; } return $resultRedirect; }
public function testBeforeRequestParams() { $requestParams = ['param1' => 'value1']; $module = 'module'; $controller = 'controller'; $action = 'action'; $this->customerSession->expects($this->exactly(2))->method('getBeforeRequestParams')->willReturn($requestParams); $this->customerSession->expects($this->once())->method('getBeforeModuleName')->willReturn($module); $this->customerSession->expects($this->once())->method('getBeforeControllerName')->willReturn($controller); $this->customerSession->expects($this->once())->method('getBeforeAction')->willReturn($action); $this->resultForward->expects($this->once())->method('setParams')->with($requestParams)->willReturnSelf(); $this->resultForward->expects($this->once())->method('setModule')->with($module)->willReturnSelf(); $this->resultForward->expects($this->once())->method('setController')->with($controller)->willReturnSelf(); $this->resultForward->expects($this->once())->method('forward')->with($action)->willReturnSelf(); $this->resultFactory->expects($this->once())->method('create')->with(ResultFactory::TYPE_FORWARD)->willReturn($this->resultForward); $result = $this->model->getRedirect(); $this->assertSame($this->resultForward, $result); }
/** * 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; }