/** * Change customer password action * * @return \Magento\Framework\Controller\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if (!$this->formKeyValidator->validate($this->getRequest())) { $resultRedirect->setPath('*/*/edit'); return $resultRedirect; } if ($this->getRequest()->isPost()) { $customerId = $this->_getSession()->getCustomerId(); $customer = $this->customerExtractor->extract('customer_account_edit', $this->_request); $customer->setId($customerId); if ($customer->getAddresses() == null) { $customer->setAddresses($this->customerRepository->getById($customerId)->getAddresses()); } if ($this->getRequest()->getParam('change_password')) { $currPass = $this->getRequest()->getPost('current_password'); $newPass = $this->getRequest()->getPost('password'); $confPass = $this->getRequest()->getPost('password_confirmation'); if (strlen($newPass)) { if ($newPass == $confPass) { try { $customerEmail = $this->customerRepository->getById($customerId)->getEmail(); $this->customerAccountManagement->changePassword($customerEmail, $currPass, $newPass); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong while changing the password.')); } } else { $this->messageManager->addError(__('Confirm your new password.')); } } else { $this->messageManager->addError(__('Please enter new password.')); } } try { $this->customerRepository->save($customer); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (InputException $e) { $this->messageManager->addException($e, __('Invalid input')); } catch (\Exception $e) { $this->messageManager->addException($e, __('We can\'t save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>'); } if ($this->messageManager->getMessages()->getCount() > 0) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPostValue()); $resultRedirect->setPath('*/*/edit'); return $resultRedirect; } $this->messageManager->addSuccess(__('You saved the account information.')); $resultRedirect->setPath('customer/account'); return $resultRedirect; } $resultRedirect->setPath('*/*/edit'); return $resultRedirect; }
/** * Change customer password action * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if (!$this->_formKeyValidator->validate($this->getRequest())) { $this->_redirect('*/*/edit'); return; } if ($this->getRequest()->isPost()) { $customerId = $this->_getSession()->getCustomerId(); $customer = $this->customerExtractor->extract('customer_account_edit', $this->_request); $this->_customerBuilder->populate($customer); $this->_customerBuilder->setId($customerId); $customer = $this->_customerBuilder->create(); if ($this->getRequest()->getParam('change_password')) { $currPass = $this->getRequest()->getPost('current_password'); $newPass = $this->getRequest()->getPost('password'); $confPass = $this->getRequest()->getPost('confirmation'); if (strlen($newPass)) { if ($newPass == $confPass) { try { $this->_customerAccountService->changePassword($customerId, $currPass, $newPass); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addException($e, __('A problem was encountered trying to change password.')); } } else { $this->messageManager->addError(__('Confirm your new password')); } } else { $this->messageManager->addError(__('New password field cannot be empty.')); } } try { $this->_customerDetailsBuilder->setCustomer($customer); $this->_customerAccountService->updateCustomer($this->_customerDetailsBuilder->create()); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (InputException $e) { $this->messageManager->addException($e, __('Invalid input')); } catch (\Exception $e) { $this->messageManager->addException($e, __('Cannot save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>'); } if ($this->messageManager->getMessages()->getCount() > 0) { $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()); $this->_redirect('*/*/edit'); return; } $this->messageManager->addSuccess(__('The account information has been saved.')); $this->_redirect('customer/account'); return; } $this->_redirect('*/*/edit'); }
public function testExtract() { $customerData = ['firstname' => 'firstname', 'lastname' => 'firstname', 'email' => 'email.example.com']; $this->formFactory->expects($this->once())->method('create')->with('customer', 'form-code')->willReturn($this->customerForm); $this->customerForm->expects($this->once())->method('extractData')->with($this->request)->willReturn($customerData); $this->customerForm->expects($this->once())->method('getAllowedAttributes')->willReturn(['group_id' => 'attribute object']); $this->customerFactory->expects($this->once())->method('create')->willReturn($this->customerData); $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with($this->customerData, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface')->willReturn($this->customerData); $this->storeManager->expects($this->once())->method('getStore')->willReturn($this->store); $this->store->expects($this->exactly(2))->method('getId')->willReturn(1); $this->customerGroupManagement->expects($this->once())->method('getDefaultGroup')->with(1)->willReturn($this->customerGroup); $this->customerGroup->expects($this->once())->method('getId')->willReturn(1); $this->customerData->expects($this->once())->method('setGroupId')->with(1); $this->store->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->customerData->expects($this->once())->method('setWebsiteId')->with(1); $this->customerData->expects($this->once())->method('setStoreId')->with(1); $this->assertSame($this->customerData, $this->customerExtractor->extract('form-code', $this->request)); }
/** * Create customer account action * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if ($this->_getSession()->isLoggedIn() || !$this->isRegistrationAllowed()) { $this->_redirect('*/*/'); return; } if (!$this->getRequest()->isPost()) { $url = $this->_createUrl()->getUrl('*/*/create', array('_secure' => true)); $this->getResponse()->setRedirect($this->_redirect->error($url)); return; } $this->_session->regenerateId(); try { $customer = $this->customerExtractor->extract('customer_account_create', $this->_request); $address = $this->_extractAddress(); $addresses = is_null($address) ? array() : array($address); $password = $this->getRequest()->getParam('password'); $redirectUrl = $this->_getSession()->getBeforeAuthUrl(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customer)->setAddresses($addresses)->create(); $customer = $this->_customerAccountService->createCustomer($customerDetails, $password, $redirectUrl); if ($this->getRequest()->getParam('is_subscribed', false)) { $this->_subscriberFactory->create()->subscribeCustomerById($customer->getId()); } $this->_eventManager->dispatch('customer_register_success', array('account_controller' => $this, 'customer' => $customer)); $confirmationStatus = $this->_customerAccountService->getConfirmationStatus($customer->getId()); if ($confirmationStatus === CustomerAccountServiceInterface::ACCOUNT_CONFIRMATION_REQUIRED) { $email = $this->_customerHelperData->getEmailConfirmationUrl($customer->getEmail()); // @codingStandardsIgnoreStart $this->messageManager->addSuccess(__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%1">click here</a>.', $email)); // @codingStandardsIgnoreEnd $url = $this->_createUrl()->getUrl('*/*/index', array('_secure' => true)); $this->getResponse()->setRedirect($this->_redirect->success($url)); } else { $this->_getSession()->setCustomerDataAsLoggedIn($customer); $url = $this->_welcomeCustomer($customer); $this->getResponse()->setRedirect($this->_redirect->success($url)); } return; } catch (StateException $e) { $url = $this->_createUrl()->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, __('Cannot save the customer.')); } $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()); $defaultUrl = $this->_createUrl()->getUrl('*/*/create', array('_secure' => true)); $this->getResponse()->setRedirect($this->_redirect->error($defaultUrl)); }
public function createCustomer(\BelVG\FacebookFree\Model\Customer $customerFb) { $this->_request->setParams($customerFb->prepareData()); $customer = $this->customerExtractor->extract('customer_account_create', $this->_request); $customer->setAddresses(null); $customer = $this->accountManagement->createAccount($customer, $customerFb->generatePassword(), ''); $this->_eventManager->dispatch('customer_register_success', ['account_controller' => $this, 'customer' => $customer]); $this->getSession()->setCustomerDataAsLoggedIn($customer); $this->messageManager->addSuccess($this->getSuccessMessage()); return $customer; }
/** * Create Data Transfer Object of customer candidate * * @param \Magento\Framework\App\RequestInterface $inputData * @param \Magento\Customer\Api\Data\CustomerInterface $currentCustomerData * @return \Magento\Customer\Api\Data\CustomerInterface */ private function populateNewCustomerDataObject(\Magento\Framework\App\RequestInterface $inputData, \Magento\Customer\Api\Data\CustomerInterface $currentCustomerData) { $customerDto = $this->customerExtractor->extract(self::FORM_DATA_EXTRACTOR_CODE, $inputData); $customerDto->setId($currentCustomerData->getId()); if (!$customerDto->getAddresses()) { $customerDto->setAddresses($currentCustomerData->getAddresses()); } if (!$inputData->getParam('change_email')) { $customerDto->setEmail($currentCustomerData->getEmail()); } return $customerDto; }
/** * Change customer password action * * @return \Magento\Framework\Controller\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function executeInternal() { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if (!$this->formKeyValidator->validate($this->getRequest())) { return $resultRedirect->setPath('*/*/edit'); } if ($this->getRequest()->isPost()) { $customerId = $this->session->getCustomerId(); $currentCustomer = $this->customerRepository->getById($customerId); // Prepare new customer data $customer = $this->customerExtractor->extract('customer_account_edit', $this->_request); $customer->setId($customerId); if ($customer->getAddresses() == null) { $customer->setAddresses($currentCustomer->getAddresses()); } // Change customer password if ($this->getRequest()->getParam('change_password')) { $this->changeCustomerPassword($currentCustomer->getEmail()); } try { $this->customerRepository->save($customer); } catch (AuthenticationException $e) { $this->messageManager->addError($e->getMessage()); } catch (InputException $e) { $this->messageManager->addException($e, __('Invalid input')); } catch (\Exception $e) { $message = __('We can\'t save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>'; $this->messageManager->addException($e, $message); } if ($this->messageManager->getMessages()->getCount() > 0) { $this->session->setCustomerFormData($this->getRequest()->getPostValue()); return $resultRedirect->setPath('*/*/edit'); } $this->messageManager->addSuccess(__('You saved the account information.')); return $resultRedirect->setPath('customer/account'); } return $resultRedirect->setPath('*/*/edit'); }
/** * 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; }