CreatePassword.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Model\Session;
  9. use Magento\Framework\App\Action\HttpGetActionInterface;
  10. use Magento\Framework\View\Result\PageFactory;
  11. use Magento\Framework\App\Action\Context;
  12. /**
  13. * Class CreatePassword
  14. *
  15. * @package Magento\Customer\Controller\Account
  16. */
  17. class CreatePassword extends \Magento\Customer\Controller\AbstractAccount implements HttpGetActionInterface
  18. {
  19. /**
  20. * @var \Magento\Customer\Api\AccountManagementInterface
  21. */
  22. protected $accountManagement;
  23. /**
  24. * @var Session
  25. */
  26. protected $session;
  27. /**
  28. * @var PageFactory
  29. */
  30. protected $resultPageFactory;
  31. /**
  32. * @param Context $context
  33. * @param Session $customerSession
  34. * @param PageFactory $resultPageFactory
  35. * @param AccountManagementInterface $accountManagement
  36. */
  37. public function __construct(
  38. Context $context,
  39. Session $customerSession,
  40. PageFactory $resultPageFactory,
  41. AccountManagementInterface $accountManagement
  42. ) {
  43. $this->session = $customerSession;
  44. $this->resultPageFactory = $resultPageFactory;
  45. $this->accountManagement = $accountManagement;
  46. parent::__construct($context);
  47. }
  48. /**
  49. * Resetting password handler
  50. *
  51. * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
  52. */
  53. public function execute()
  54. {
  55. $resetPasswordToken = (string)$this->getRequest()->getParam('token');
  56. $isDirectLink = $resetPasswordToken != '';
  57. if (!$isDirectLink) {
  58. $resetPasswordToken = (string)$this->session->getRpToken();
  59. }
  60. try {
  61. $this->accountManagement->validateResetPasswordLinkToken(null, $resetPasswordToken);
  62. if ($isDirectLink) {
  63. $this->session->setRpToken($resetPasswordToken);
  64. $resultRedirect = $this->resultRedirectFactory->create();
  65. $resultRedirect->setPath('*/*/createpassword');
  66. return $resultRedirect;
  67. } else {
  68. /** @var \Magento\Framework\View\Result\Page $resultPage */
  69. $resultPage = $this->resultPageFactory->create();
  70. $resultPage->getLayout()
  71. ->getBlock('resetPassword')
  72. ->setResetPasswordLinkToken($resetPasswordToken);
  73. return $resultPage;
  74. }
  75. } catch (\Exception $exception) {
  76. $this->messageManager->addError(__('Your password reset link has expired.'));
  77. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  78. $resultRedirect = $this->resultRedirectFactory->create();
  79. $resultRedirect->setPath('*/*/forgotpassword');
  80. return $resultRedirect;
  81. }
  82. }
  83. }