Forgotpassword.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\User\Controller\Adminhtml\Auth;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Framework\App\Action\HttpPostActionInterface;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Security\Model\SecurityManager;
  12. use Magento\Backend\App\Action\Context;
  13. use Magento\User\Model\UserFactory;
  14. use Magento\User\Model\ResourceModel\User\CollectionFactory;
  15. use Magento\Framework\Validator\EmailAddress;
  16. use Magento\Security\Model\PasswordResetRequestEvent;
  17. use Magento\Framework\Exception\SecurityViolationException;
  18. use Magento\User\Controller\Adminhtml\Auth;
  19. use Magento\Backend\Helper\Data;
  20. use Magento\User\Model\Spi\NotificatorInterface;
  21. /**
  22. * Initiate forgot-password process.
  23. *
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class Forgotpassword extends Auth implements HttpGetActionInterface, HttpPostActionInterface
  27. {
  28. /**
  29. * @var SecurityManager
  30. */
  31. protected $securityManager;
  32. /**
  33. * @var NotificatorInterface
  34. */
  35. private $notificator;
  36. /**
  37. * User model factory
  38. *
  39. * @var CollectionFactory
  40. */
  41. private $userCollectionFactory;
  42. /**
  43. * @var Data
  44. */
  45. private $backendDataHelper;
  46. /**
  47. * @param Context $context
  48. * @param UserFactory $userFactory
  49. * @param SecurityManager $securityManager
  50. * @param CollectionFactory $userCollectionFactory
  51. * @param Data $backendDataHelper
  52. * @param NotificatorInterface|null $notificator
  53. */
  54. public function __construct(
  55. Context $context,
  56. UserFactory $userFactory,
  57. SecurityManager $securityManager,
  58. CollectionFactory $userCollectionFactory = null,
  59. Data $backendDataHelper = null,
  60. ?NotificatorInterface $notificator = null
  61. ) {
  62. parent::__construct($context, $userFactory);
  63. $this->securityManager = $securityManager;
  64. $this->userCollectionFactory = $userCollectionFactory ?:
  65. ObjectManager::getInstance()->get(CollectionFactory::class);
  66. $this->backendDataHelper = $backendDataHelper ?:
  67. ObjectManager::getInstance()->get(Data::class);
  68. $this->notificator = $notificator
  69. ?? ObjectManager::getInstance()->get(NotificatorInterface::class);
  70. }
  71. /**
  72. * Forgot administrator password action
  73. *
  74. * @return void
  75. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  76. */
  77. public function execute()
  78. {
  79. $email = (string)$this->getRequest()->getParam('email');
  80. $params = $this->getRequest()->getParams();
  81. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  82. $resultRedirect = $this->resultRedirectFactory->create();
  83. if (!empty($email) && !empty($params)) {
  84. // Validate received data to be an email address
  85. if (\Zend_Validate::is($email, EmailAddress::class)) {
  86. try {
  87. $this->securityManager->performSecurityCheck(
  88. PasswordResetRequestEvent::ADMIN_PASSWORD_RESET_REQUEST,
  89. $email
  90. );
  91. } catch (SecurityViolationException $exception) {
  92. $this->messageManager->addErrorMessage($exception->getMessage());
  93. return $resultRedirect->setPath('admin');
  94. }
  95. /** @var $collection \Magento\User\Model\ResourceModel\User\Collection */
  96. $collection = $this->userCollectionFactory->create();
  97. $collection->addFieldToFilter('email', $email);
  98. $collection->load(false);
  99. try {
  100. if ($collection->getSize() > 0) {
  101. foreach ($collection as $item) {
  102. /** @var \Magento\User\Model\User $user */
  103. $user = $this->_userFactory->create()->load($item->getId());
  104. if ($user->getId()) {
  105. $newPassResetToken = $this->backendDataHelper->generateResetPasswordLinkToken();
  106. $user->changeResetPasswordLinkToken($newPassResetToken);
  107. $user->save();
  108. $this->notificator->sendForgotPassword($user);
  109. }
  110. break;
  111. }
  112. }
  113. } catch (\Exception $exception) {
  114. $this->messageManager->addExceptionMessage(
  115. $exception,
  116. __('We\'re unable to send the password reset email.')
  117. );
  118. return $resultRedirect->setPath('admin');
  119. }
  120. // @codingStandardsIgnoreStart
  121. $this->messageManager->addSuccess(__('We\'ll email you a link to reset your password.'));
  122. // @codingStandardsIgnoreEnd
  123. $this->getResponse()->setRedirect(
  124. $this->backendDataHelper->getHomePageUrl()
  125. );
  126. return;
  127. } else {
  128. $this->messageManager->addError(__('Please correct this email address:'));
  129. }
  130. } elseif (!empty($params)) {
  131. $this->messageManager->addError(__('Please enter an email address.'));
  132. }
  133. $this->_view->loadLayout();
  134. $this->_view->renderLayout();
  135. }
  136. }