Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ForgotPasswordPost.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
17 
23 {
28 
32  protected $escaper;
33 
37  protected $session;
38 
45  public function __construct(
46  Context $context,
47  Session $customerSession,
50  ) {
51  $this->session = $customerSession;
52  $this->customerAccountManagement = $customerAccountManagement;
53  $this->escaper = $escaper;
54  parent::__construct($context);
55  }
56 
62  public function execute()
63  {
65  $resultRedirect = $this->resultRedirectFactory->create();
66  $email = (string)$this->getRequest()->getPost('email');
67  if ($email) {
68  if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
69  $this->session->setForgottenEmail($email);
70  $this->messageManager->addErrorMessage(
71  __('The email address is incorrect. Verify the email address and try again.')
72  );
73  return $resultRedirect->setPath('*/*/forgotpassword');
74  }
75 
76  try {
77  $this->customerAccountManagement->initiatePasswordReset(
78  $email,
80  );
81  } catch (NoSuchEntityException $exception) {
82  // Do nothing, we don't want anyone to use this action to determine which email accounts are registered.
83  } catch (SecurityViolationException $exception) {
84  $this->messageManager->addErrorMessage($exception->getMessage());
85  return $resultRedirect->setPath('*/*/forgotpassword');
86  } catch (\Exception $exception) {
87  $this->messageManager->addExceptionMessage(
88  $exception,
89  __('We\'re unable to send the password reset email.')
90  );
91  return $resultRedirect->setPath('*/*/forgotpassword');
92  }
93  $this->messageManager->addSuccessMessage($this->getSuccessMessage($email));
94  return $resultRedirect->setPath('*/*/');
95  } else {
96  $this->messageManager->addErrorMessage(__('Please enter your email.'));
97  return $resultRedirect->setPath('*/*/forgotpassword');
98  }
99  }
100 
107  protected function getSuccessMessage($email)
108  {
109  return __(
110  'If there is an account associated with %1 you will receive an email with a link to reset your password.',
111  $this->escaper->escapeHtml($email)
112  );
113  }
114 }
$customerAccountManagement
$email
Definition: details.phtml:13
getSuccessMessage($email)
__()
Definition: __.php:13
$session
__construct(Context $context, Session $customerSession, AccountManagementInterface $customerAccountManagement, Escaper $escaper)
$escaper
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195