ResetPasswordPost.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Account;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Framework\App\Action\Context;
  11. use Magento\Framework\App\Action\HttpPostActionInterface;
  12. use Magento\Framework\Exception\InputException;
  13. use Magento\Customer\Model\Customer\CredentialsValidator;
  14. /**
  15. * Class ResetPasswordPost
  16. *
  17. * @package Magento\Customer\Controller\Account
  18. */
  19. class ResetPasswordPost extends \Magento\Customer\Controller\AbstractAccount implements HttpPostActionInterface
  20. {
  21. /**
  22. * @var \Magento\Customer\Api\AccountManagementInterface
  23. */
  24. protected $accountManagement;
  25. /**
  26. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  27. */
  28. protected $customerRepository;
  29. /**
  30. * @var Session
  31. */
  32. protected $session;
  33. /**
  34. * @param Context $context
  35. * @param Session $customerSession
  36. * @param AccountManagementInterface $accountManagement
  37. * @param CustomerRepositoryInterface $customerRepository
  38. * @param CredentialsValidator|null $credentialsValidator
  39. *
  40. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  41. */
  42. public function __construct(
  43. Context $context,
  44. Session $customerSession,
  45. AccountManagementInterface $accountManagement,
  46. CustomerRepositoryInterface $customerRepository,
  47. CredentialsValidator $credentialsValidator = null
  48. ) {
  49. $this->session = $customerSession;
  50. $this->accountManagement = $accountManagement;
  51. $this->customerRepository = $customerRepository;
  52. parent::__construct($context);
  53. }
  54. /**
  55. * Reset forgotten password
  56. *
  57. * Used to handle data received from reset forgotten password form
  58. *
  59. * @return \Magento\Framework\Controller\Result\Redirect
  60. */
  61. public function execute()
  62. {
  63. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  64. $resultRedirect = $this->resultRedirectFactory->create();
  65. $resetPasswordToken = (string)$this->getRequest()->getQuery('token');
  66. $password = (string)$this->getRequest()->getPost('password');
  67. $passwordConfirmation = (string)$this->getRequest()->getPost('password_confirmation');
  68. if ($password !== $passwordConfirmation) {
  69. $this->messageManager->addError(__("New Password and Confirm New Password values didn't match."));
  70. $resultRedirect->setPath('*/*/createPassword', ['token' => $resetPasswordToken]);
  71. return $resultRedirect;
  72. }
  73. if (iconv_strlen($password) <= 0) {
  74. $this->messageManager->addError(__('Please enter a new password.'));
  75. $resultRedirect->setPath('*/*/createPassword', ['token' => $resetPasswordToken]);
  76. return $resultRedirect;
  77. }
  78. try {
  79. $this->accountManagement->resetPassword(
  80. null,
  81. $resetPasswordToken,
  82. $password
  83. );
  84. $this->session->unsRpToken();
  85. $this->messageManager->addSuccess(__('You updated your password.'));
  86. $resultRedirect->setPath('*/*/login');
  87. return $resultRedirect;
  88. } catch (InputException $e) {
  89. $this->messageManager->addError($e->getMessage());
  90. foreach ($e->getErrors() as $error) {
  91. $this->messageManager->addError($error->getMessage());
  92. }
  93. } catch (\Exception $exception) {
  94. $this->messageManager->addError(__('Something went wrong while saving the new password.'));
  95. }
  96. $resultRedirect->setPath('*/*/createPassword', ['token' => $resetPasswordToken]);
  97. return $resultRedirect;
  98. }
  99. }