71 private $emailNotification;
76 private $authentication;
81 private $customerMapper;
106 parent::__construct($context);
107 $this->session = $customerSession;
120 private function getAuthentication()
125 \
Magento\Customer\Model\AuthenticationInterface::class
128 return $this->authentication;
138 private function getEmailNotification()
140 if (!($this->emailNotification instanceof EmailNotificationInterface)) {
142 EmailNotificationInterface::class
145 return $this->emailNotification;
154 ): ?InvalidRequestException {
156 $resultRedirect = $this->resultRedirectFactory->create();
157 $resultRedirect->setPath(
'*/*/edit');
159 return new InvalidRequestException(
161 [
new Phrase(
'Invalid Form Key. Please refresh the page.')]
181 $resultRedirect = $this->resultRedirectFactory->create();
182 $validFormKey = $this->formKeyValidator->validate($this->
getRequest());
184 if ($validFormKey && $this->
getRequest()->isPost()) {
185 $currentCustomerDataObject = $this->getCustomerDataObject($this->session->getCustomerId());
186 $customerCandidateDataObject = $this->populateNewCustomerDataObject(
188 $currentCustomerDataObject
193 $this->processChangeEmailRequest($currentCustomerDataObject);
198 $this->customerRepository->save($customerCandidateDataObject);
199 $this->getEmailNotification()->credentialsChanged(
200 $customerCandidateDataObject,
201 $currentCustomerDataObject->getEmail(),
204 $this->dispatchSuccessEvent($customerCandidateDataObject);
205 $this->messageManager->addSuccess(
__(
'You saved the account information.'));
206 return $resultRedirect->setPath(
'customer/account');
207 }
catch (InvalidEmailOrPasswordException $e) {
208 $this->messageManager->addErrorMessage($this->escaper->escapeHtml($e->getMessage()));
209 }
catch (UserLockedException $e) {
211 'The account sign-in was incorrect or your account is disabled temporarily. ' 212 .
'Please wait and try again later.' 214 $this->session->logout();
215 $this->session->start();
216 $this->messageManager->addError(
$message);
217 return $resultRedirect->setPath(
'customer/account/login');
218 }
catch (InputException $e) {
219 $this->messageManager->addErrorMessage($this->escaper->escapeHtml($e->getMessage()));
220 foreach ($e->getErrors() as $error) {
221 $this->messageManager->addErrorMessage($this->escaper->escapeHtml($error->getMessage()));
223 }
catch (\
Magento\Framework\Exception\LocalizedException $e) {
224 $this->messageManager->addError($e->getMessage());
225 }
catch (\Exception $e) {
226 $this->messageManager->addException($e,
__(
'We can\'t save the customer.'));
229 $this->session->setCustomerFormData($this->
getRequest()->getPostValue());
233 $resultRedirect = $this->resultRedirectFactory->create();
234 $resultRedirect->setPath(
'*/*/edit');
235 return $resultRedirect;
244 private function dispatchSuccessEvent(\
Magento\Customer\Api\Data\CustomerInterface $customerCandidateDataObject)
246 $this->_eventManager->dispatch(
247 'customer_account_edited',
248 [
'email' => $customerCandidateDataObject->getEmail()]
259 private function getCustomerDataObject(
$customerId)
261 return $this->customerRepository->getById(
$customerId);
271 private function populateNewCustomerDataObject(
272 \
Magento\Framework\
App\RequestInterface $inputData,
273 \
Magento\Customer\Api\Data\CustomerInterface $currentCustomerData
275 $attributeValues = $this->getCustomerMapper()->toFlatArray($currentCustomerData);
276 $customerDto = $this->customerExtractor->extract(
277 self::FORM_DATA_EXTRACTOR_CODE,
281 $customerDto->setId($currentCustomerData->getId());
282 if (!$customerDto->getAddresses()) {
283 $customerDto->setAddresses($currentCustomerData->getAddresses());
285 if (!$inputData->getParam(
'change_email')) {
286 $customerDto->setEmail($currentCustomerData->getEmail());
301 $isPasswordChanged =
false;
302 if ($this->
getRequest()->getParam(
'change_password')) {
303 $currPass = $this->
getRequest()->getPost(
'current_password');
304 $newPass = $this->
getRequest()->getPost(
'password');
305 $confPass = $this->
getRequest()->getPost(
'password_confirmation');
306 if ($newPass != $confPass) {
307 throw new InputException(
__(
'Password confirmation doesn\'t match entered password.'));
310 $isPasswordChanged = $this->customerAccountManagement->changePassword(
$email, $currPass, $newPass);
313 return $isPasswordChanged;
326 if ($this->
getRequest()->getParam(
'change_email')) {
329 $this->getAuthentication()->authenticate(
330 $currentCustomerDataObject->getId(),
331 $this->
getRequest()->getPost(
'current_password')
333 }
catch (InvalidEmailOrPasswordException $e) {
334 throw new InvalidEmailOrPasswordException(
335 __(
"The password doesn't match this account. Verify the password and try again.")
348 private function getCustomerMapper()
350 if ($this->customerMapper ===
null) {
353 return $this->customerMapper;
validateForCsrf(RequestInterface $request)
$customerAccountManagement
__construct(Context $context, Session $customerSession, AccountManagementInterface $customerAccountManagement, CustomerRepositoryInterface $customerRepository, Validator $formKeyValidator, CustomerExtractor $customerExtractor, ?Escaper $escaper=null)
changeCustomerPassword($email)
const FORM_DATA_EXTRACTOR_CODE
createCsrfValidationException(RequestInterface $request)