Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EditPost.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
31 
37 {
41  const FORM_DATA_EXTRACTOR_CODE = 'customer_account_edit';
42 
47 
52 
56  protected $formKeyValidator;
57 
61  protected $customerExtractor;
62 
66  protected $session;
67 
71  private $emailNotification;
72 
76  private $authentication;
77 
81  private $customerMapper;
82 
86  private $escaper;
87 
97  public function __construct(
98  Context $context,
99  Session $customerSession,
104  ?Escaper $escaper = null
105  ) {
106  parent::__construct($context);
107  $this->session = $customerSession;
108  $this->customerAccountManagement = $customerAccountManagement;
109  $this->customerRepository = $customerRepository;
110  $this->formKeyValidator = $formKeyValidator;
111  $this->customerExtractor = $customerExtractor;
112  $this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
113  }
114 
120  private function getAuthentication()
121  {
122 
123  if (!($this->authentication instanceof AuthenticationInterface)) {
124  return ObjectManager::getInstance()->get(
125  \Magento\Customer\Model\AuthenticationInterface::class
126  );
127  } else {
128  return $this->authentication;
129  }
130  }
131 
138  private function getEmailNotification()
139  {
140  if (!($this->emailNotification instanceof EmailNotificationInterface)) {
141  return ObjectManager::getInstance()->get(
142  EmailNotificationInterface::class
143  );
144  } else {
145  return $this->emailNotification;
146  }
147  }
148 
152  public function createCsrfValidationException(
153  RequestInterface $request
154  ): ?InvalidRequestException {
156  $resultRedirect = $this->resultRedirectFactory->create();
157  $resultRedirect->setPath('*/*/edit');
158 
159  return new InvalidRequestException(
160  $resultRedirect,
161  [new Phrase('Invalid Form Key. Please refresh the page.')]
162  );
163  }
164 
168  public function validateForCsrf(RequestInterface $request): ?bool
169  {
170  return null;
171  }
172 
178  public function execute()
179  {
181  $resultRedirect = $this->resultRedirectFactory->create();
182  $validFormKey = $this->formKeyValidator->validate($this->getRequest());
183 
184  if ($validFormKey && $this->getRequest()->isPost()) {
185  $currentCustomerDataObject = $this->getCustomerDataObject($this->session->getCustomerId());
186  $customerCandidateDataObject = $this->populateNewCustomerDataObject(
187  $this->_request,
188  $currentCustomerDataObject
189  );
190 
191  try {
192  // whether a customer enabled change email option
193  $this->processChangeEmailRequest($currentCustomerDataObject);
194 
195  // whether a customer enabled change password option
196  $isPasswordChanged = $this->changeCustomerPassword($currentCustomerDataObject->getEmail());
197 
198  $this->customerRepository->save($customerCandidateDataObject);
199  $this->getEmailNotification()->credentialsChanged(
200  $customerCandidateDataObject,
201  $currentCustomerDataObject->getEmail(),
202  $isPasswordChanged
203  );
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) {
210  $message = __(
211  'The account sign-in was incorrect or your account is disabled temporarily. '
212  . 'Please wait and try again later.'
213  );
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()));
222  }
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.'));
227  }
228 
229  $this->session->setCustomerFormData($this->getRequest()->getPostValue());
230  }
231 
233  $resultRedirect = $this->resultRedirectFactory->create();
234  $resultRedirect->setPath('*/*/edit');
235  return $resultRedirect;
236  }
237 
244  private function dispatchSuccessEvent(\Magento\Customer\Api\Data\CustomerInterface $customerCandidateDataObject)
245  {
246  $this->_eventManager->dispatch(
247  'customer_account_edited',
248  ['email' => $customerCandidateDataObject->getEmail()]
249  );
250  }
251 
259  private function getCustomerDataObject($customerId)
260  {
261  return $this->customerRepository->getById($customerId);
262  }
263 
271  private function populateNewCustomerDataObject(
272  \Magento\Framework\App\RequestInterface $inputData,
273  \Magento\Customer\Api\Data\CustomerInterface $currentCustomerData
274  ) {
275  $attributeValues = $this->getCustomerMapper()->toFlatArray($currentCustomerData);
276  $customerDto = $this->customerExtractor->extract(
277  self::FORM_DATA_EXTRACTOR_CODE,
278  $inputData,
280  );
281  $customerDto->setId($currentCustomerData->getId());
282  if (!$customerDto->getAddresses()) {
283  $customerDto->setAddresses($currentCustomerData->getAddresses());
284  }
285  if (!$inputData->getParam('change_email')) {
286  $customerDto->setEmail($currentCustomerData->getEmail());
287  }
288 
289  return $customerDto;
290  }
291 
299  protected function changeCustomerPassword($email)
300  {
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.'));
308  }
309 
310  $isPasswordChanged = $this->customerAccountManagement->changePassword($email, $currPass, $newPass);
311  }
312 
313  return $isPasswordChanged;
314  }
315 
324  private function processChangeEmailRequest(\Magento\Customer\Api\Data\CustomerInterface $currentCustomerDataObject)
325  {
326  if ($this->getRequest()->getParam('change_email')) {
327  // authenticate user for changing email
328  try {
329  $this->getAuthentication()->authenticate(
330  $currentCustomerDataObject->getId(),
331  $this->getRequest()->getPost('current_password')
332  );
333  } catch (InvalidEmailOrPasswordException $e) {
334  throw new InvalidEmailOrPasswordException(
335  __("The password doesn't match this account. Verify the password and try again.")
336  );
337  }
338  }
339  }
340 
348  private function getCustomerMapper()
349  {
350  if ($this->customerMapper === null) {
351  $this->customerMapper = ObjectManager::getInstance()->get(\Magento\Customer\Model\Customer\Mapper::class);
352  }
353  return $this->customerMapper;
354  }
355 }
$formKeyValidator
Definition: EditPost.php:56
$email
Definition: details.phtml:13
$customerExtractor
Definition: EditPost.php:61
__()
Definition: __.php:13
$message
validateForCsrf(RequestInterface $request)
Definition: EditPost.php:168
$customerAccountManagement
Definition: EditPost.php:46
$customerRepository
Definition: EditPost.php:51
__construct(Context $context, Session $customerSession, AccountManagementInterface $customerAccountManagement, CustomerRepositoryInterface $customerRepository, Validator $formKeyValidator, CustomerExtractor $customerExtractor, ?Escaper $escaper=null)
Definition: EditPost.php:97
changeCustomerPassword($email)
Definition: EditPost.php:299
const FORM_DATA_EXTRACTOR_CODE
Definition: EditPost.php:41
createCsrfValidationException(RequestInterface $request)
$session
Definition: EditPost.php:66
Definition: EditPost.php:36