securityManager = $securityManager; $this->userCollectionFactory = $userCollectionFactory ?: ObjectManager::getInstance()->get(CollectionFactory::class); $this->backendDataHelper = $backendDataHelper ?: ObjectManager::getInstance()->get(Data::class); $this->notificator = $notificator ?? ObjectManager::getInstance()->get(NotificatorInterface::class); } /** * Forgot administrator password action * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { $email = (string)$this->getRequest()->getParam('email'); $params = $this->getRequest()->getParams(); /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if (!empty($email) && !empty($params)) { // Validate received data to be an email address if (\Zend_Validate::is($email, EmailAddress::class)) { try { $this->securityManager->performSecurityCheck( PasswordResetRequestEvent::ADMIN_PASSWORD_RESET_REQUEST, $email ); } catch (SecurityViolationException $exception) { $this->messageManager->addErrorMessage($exception->getMessage()); return $resultRedirect->setPath('admin'); } /** @var $collection \Magento\User\Model\ResourceModel\User\Collection */ $collection = $this->userCollectionFactory->create(); $collection->addFieldToFilter('email', $email); $collection->load(false); try { if ($collection->getSize() > 0) { foreach ($collection as $item) { /** @var \Magento\User\Model\User $user */ $user = $this->_userFactory->create()->load($item->getId()); if ($user->getId()) { $newPassResetToken = $this->backendDataHelper->generateResetPasswordLinkToken(); $user->changeResetPasswordLinkToken($newPassResetToken); $user->save(); $this->notificator->sendForgotPassword($user); } break; } } } catch (\Exception $exception) { $this->messageManager->addExceptionMessage( $exception, __('We\'re unable to send the password reset email.') ); return $resultRedirect->setPath('admin'); } // @codingStandardsIgnoreStart $this->messageManager->addSuccess(__('We\'ll email you a link to reset your password.')); // @codingStandardsIgnoreEnd $this->getResponse()->setRedirect( $this->backendDataHelper->getHomePageUrl() ); return; } else { $this->messageManager->addError(__('Please correct this email address:')); } } elseif (!empty($params)) { $this->messageManager->addError(__('Please enter an email address.')); } $this->_view->loadLayout(); $this->_view->renderLayout(); } }