@@ -70,42 +70,23 @@ public function execute()
7070 /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
7171 $ resultRedirect = $ this ->resultRedirectFactory ->create ();
7272 if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
73- $ resultRedirect ->setPath ('*/*/edit ' );
74- return $ resultRedirect ;
73+ return $ resultRedirect ->setPath ('*/*/edit ' );
7574 }
7675
7776 if ($ this ->getRequest ()->isPost ()) {
7877 $ customerId = $ this ->_getSession ()->getCustomerId ();
78+ $ currentCustomer = $ this ->customerRepository ->getById ($ customerId );
79+
80+ // Prepare new customer data
7981 $ customer = $ this ->customerExtractor ->extract ('customer_account_edit ' , $ this ->_request );
8082 $ customer ->setId ($ customerId );
8183 if ($ customer ->getAddresses () == null ) {
82- $ customer ->setAddresses ($ this -> customerRepository -> getById ( $ customerId ) ->getAddresses ());
84+ $ customer ->setAddresses ($ currentCustomer ->getAddresses ());
8385 }
8486
87+ // Change customer password
8588 if ($ this ->getRequest ()->getParam ('change_password ' )) {
86- $ currPass = $ this ->getRequest ()->getPost ('current_password ' );
87- $ newPass = $ this ->getRequest ()->getPost ('password ' );
88- $ confPass = $ this ->getRequest ()->getPost ('password_confirmation ' );
89-
90- if (strlen ($ newPass )) {
91- if ($ newPass == $ confPass ) {
92- try {
93- $ customerEmail = $ this ->customerRepository ->getById ($ customerId )->getEmail ();
94- $ this ->customerAccountManagement ->changePassword ($ customerEmail , $ currPass , $ newPass );
95- } catch (AuthenticationException $ e ) {
96- $ this ->messageManager ->addError ($ e ->getMessage ());
97- } catch (\Exception $ e ) {
98- $ this ->messageManager ->addException (
99- $ e ,
100- __ ('Something went wrong while changing the password. ' )
101- );
102- }
103- } else {
104- $ this ->messageManager ->addError (__ ('Confirm your new password. ' ));
105- }
106- } else {
107- $ this ->messageManager ->addError (__ ('Please enter new password. ' ));
108- }
89+ $ this ->changeCustomerPassword ($ currentCustomer ->getEmail ());
10990 }
11091
11192 try {
@@ -115,24 +96,54 @@ public function execute()
11596 } catch (InputException $ e ) {
11697 $ this ->messageManager ->addException ($ e , __ ('Invalid input ' ));
11798 } catch (\Exception $ e ) {
118- $ this -> messageManager -> addException (
119- $ e ,
120- __ ( ' We can \' t save the customer. ' ) . $ e -> getMessage () . '<pre> ' . $ e ->getTraceAsString () . '</pre> '
121- );
99+ $ message = __ ( ' We can \' t save the customer. ' )
100+ . $ e -> getMessage ()
101+ . '<pre> ' . $ e ->getTraceAsString () . '</pre> ' ;
102+ $ this -> messageManager -> addException ( $ e , $ message );
122103 }
123104
124105 if ($ this ->messageManager ->getMessages ()->getCount () > 0 ) {
125106 $ this ->_getSession ()->setCustomerFormData ($ this ->getRequest ()->getPostValue ());
126- $ resultRedirect ->setPath ('*/*/edit ' );
127- return $ resultRedirect ;
107+ return $ resultRedirect ->setPath ('*/*/edit ' );
128108 }
129109
130110 $ this ->messageManager ->addSuccess (__ ('You saved the account information. ' ));
131- $ resultRedirect ->setPath ('customer/account ' );
132- return $ resultRedirect ;
111+ return $ resultRedirect ->setPath ('customer/account ' );
112+ }
113+
114+ return $ resultRedirect ->setPath ('*/*/edit ' );
115+ }
116+
117+ /**
118+ * Change customer password
119+ *
120+ * @param string $email
121+ * @return $this
122+ */
123+ protected function changeCustomerPassword ($ email )
124+ {
125+ $ currPass = $ this ->getRequest ()->getPost ('current_password ' );
126+ $ newPass = $ this ->getRequest ()->getPost ('password ' );
127+ $ confPass = $ this ->getRequest ()->getPost ('password_confirmation ' );
128+
129+ if (!strlen ($ newPass )) {
130+ $ this ->messageManager ->addError (__ ('Please enter new password. ' ));
131+ return $ this ;
132+ }
133+
134+ if ($ newPass !== $ confPass ) {
135+ $ this ->messageManager ->addError (__ ('Confirm your new password. ' ));
136+ return $ this ;
137+ }
138+
139+ try {
140+ $ this ->customerAccountManagement ->changePassword ($ email , $ currPass , $ newPass );
141+ } catch (AuthenticationException $ e ) {
142+ $ this ->messageManager ->addError ($ e ->getMessage ());
143+ } catch (\Exception $ e ) {
144+ $ this ->messageManager ->addException ($ e , __ ('Something went wrong while changing the password. ' ));
133145 }
134146
135- $ resultRedirect ->setPath ('*/*/edit ' );
136- return $ resultRedirect ;
147+ return $ this ;
137148 }
138149}
0 commit comments