ForgotPasswordPost.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Account;
  8. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  9. use Magento\Customer\Api\AccountManagementInterface;
  10. use Magento\Customer\Model\AccountManagement;
  11. use Magento\Customer\Model\Session;
  12. use Magento\Framework\App\Action\Context;
  13. use Magento\Framework\Escaper;
  14. use Magento\Framework\Exception\NoSuchEntityException;
  15. use Magento\Framework\Exception\SecurityViolationException;
  16. /**
  17. * ForgotPasswordPost controller
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class ForgotPasswordPost extends \Magento\Customer\Controller\AbstractAccount implements HttpPostActionInterface
  21. {
  22. /**
  23. * @var \Magento\Customer\Api\AccountManagementInterface
  24. */
  25. protected $customerAccountManagement;
  26. /**
  27. * @var \Magento\Framework\Escaper
  28. */
  29. protected $escaper;
  30. /**
  31. * @var Session
  32. */
  33. protected $session;
  34. /**
  35. * @param Context $context
  36. * @param Session $customerSession
  37. * @param AccountManagementInterface $customerAccountManagement
  38. * @param Escaper $escaper
  39. */
  40. public function __construct(
  41. Context $context,
  42. Session $customerSession,
  43. AccountManagementInterface $customerAccountManagement,
  44. Escaper $escaper
  45. ) {
  46. $this->session = $customerSession;
  47. $this->customerAccountManagement = $customerAccountManagement;
  48. $this->escaper = $escaper;
  49. parent::__construct($context);
  50. }
  51. /**
  52. * Forgot customer password action
  53. *
  54. * @return \Magento\Framework\Controller\Result\Redirect
  55. */
  56. public function execute()
  57. {
  58. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  59. $resultRedirect = $this->resultRedirectFactory->create();
  60. $email = (string)$this->getRequest()->getPost('email');
  61. if ($email) {
  62. if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
  63. $this->session->setForgottenEmail($email);
  64. $this->messageManager->addErrorMessage(
  65. __('The email address is incorrect. Verify the email address and try again.')
  66. );
  67. return $resultRedirect->setPath('*/*/forgotpassword');
  68. }
  69. try {
  70. $this->customerAccountManagement->initiatePasswordReset(
  71. $email,
  72. AccountManagement::EMAIL_RESET
  73. );
  74. } catch (NoSuchEntityException $exception) {
  75. // Do nothing, we don't want anyone to use this action to determine which email accounts are registered.
  76. } catch (SecurityViolationException $exception) {
  77. $this->messageManager->addErrorMessage($exception->getMessage());
  78. return $resultRedirect->setPath('*/*/forgotpassword');
  79. } catch (\Exception $exception) {
  80. $this->messageManager->addExceptionMessage(
  81. $exception,
  82. __('We\'re unable to send the password reset email.')
  83. );
  84. return $resultRedirect->setPath('*/*/forgotpassword');
  85. }
  86. $this->messageManager->addSuccessMessage($this->getSuccessMessage($email));
  87. return $resultRedirect->setPath('*/*/');
  88. } else {
  89. $this->messageManager->addErrorMessage(__('Please enter your email.'));
  90. return $resultRedirect->setPath('*/*/forgotpassword');
  91. }
  92. }
  93. /**
  94. * Retrieve success message
  95. *
  96. * @param string $email
  97. * @return \Magento\Framework\Phrase
  98. */
  99. protected function getSuccessMessage($email)
  100. {
  101. return __(
  102. 'If there is an account associated with %1 you will receive an email with a link to reset your password.',
  103. $this->escaper->escapeHtml($email)
  104. );
  105. }
  106. }