Exemple #1
0
 /**
  * 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');
 }