Skip to content

[Backport] Admin user auth controller refactor #17156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions app/code/Magento/User/Controller/Adminhtml/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@
namespace Magento\User\Controller\Adminhtml;

use Magento\Framework\Encryption\Helper\Security;
use Magento\Backend\App\AbstractAction;
use Magento\Backend\App\Action\Context;
use Magento\User\Model\UserFactory;
use Magento\Framework\Exception\LocalizedException;

/**
* \Magento\User Auth controller
*/
abstract class Auth extends \Magento\Backend\App\AbstractAction
abstract class Auth extends AbstractAction
{
/**
* User model factory
*
* @var \Magento\User\Model\UserFactory
* @var UserFactory
*/
protected $_userFactory;

/**
* Construct
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\User\Model\UserFactory $userFactory
* @param Context $context
* @param UserFactory $userFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\User\Model\UserFactory $userFactory
Context $context,
UserFactory $userFactory
) {
parent::__construct($context);
$this->_userFactory = $userFactory;
Expand All @@ -39,7 +43,7 @@ public function __construct(
* @param int $userId
* @param string $resetPasswordToken
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
*/
protected function _validateResetPasswordLinkToken($userId, $resetPasswordToken)
{
Expand All @@ -49,20 +53,20 @@ protected function _validateResetPasswordLinkToken($userId, $resetPasswordToken)
$resetPasswordToken
) || empty($resetPasswordToken) || empty($userId) || $userId < 0
) {
throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the password reset token.'));
throw new LocalizedException(__('Please correct the password reset token.'));
}

/** @var $user \Magento\User\Model\User */
$user = $this->_userFactory->create()->load($userId);
if (!$user->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__('Please specify the correct account and try again.')
);
}

$userToken = $user->getRpToken();
if (!Security::compareStrings($userToken, $resetPasswordToken) || $user->isResetPasswordLinkTokenExpired()) {
throw new \Magento\Framework\Exception\LocalizedException(__('Your password reset link has expired.'));
throw new LocalizedException(__('Your password reset link has expired.'));
}
}

Expand Down
58 changes: 43 additions & 15 deletions app/code/Magento/User/Controller/Adminhtml/Auth/Forgotpassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,56 @@
namespace Magento\User\Controller\Adminhtml\Auth;

use Magento\Security\Model\SecurityManager;
use Magento\Framework\App\ObjectManager;
use Magento\Backend\App\Action\Context;
use Magento\User\Model\UserFactory;
use Magento\User\Model\ResourceModel\User\CollectionFactory;
use Magento\Framework\Validator\EmailAddress;
use Magento\Security\Model\PasswordResetRequestEvent;
use Magento\Framework\Exception\SecurityViolationException;
use Magento\User\Controller\Adminhtml\Auth;
use Magento\Backend\Helper\Data;

class Forgotpassword extends \Magento\User\Controller\Adminhtml\Auth
class Forgotpassword extends Auth
{
/**
* @var SecurityManager
*/
protected $securityManager;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\User\Model\UserFactory $userFactory
* @param \Magento\Security\Model\SecurityManager $securityManager
* User model factory
*
* @var CollectionFactory
*/
private $userCollectionFactory;

/**
* @var Data
*/
private $backendDataHelper;

/**
* Forgotpassword constructor.
* @param Context $context
* @param UserFactory $userFactory
* @param SecurityManager $securityManager
* @param CollectionFactory|null $userCollectionFactory
* @param Data|null $backendDataHelper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\User\Model\UserFactory $userFactory,
\Magento\Security\Model\SecurityManager $securityManager
Context $context,
UserFactory $userFactory,
SecurityManager $securityManager,
CollectionFactory $userCollectionFactory = null,
Data $backendDataHelper = null
) {
parent::__construct($context, $userFactory);
$this->securityManager = $securityManager;
$this->userCollectionFactory = $userCollectionFactory ?:
ObjectManager::getInstance()->get(CollectionFactory::class);
$this->backendDataHelper = $backendDataHelper ?:
ObjectManager::getInstance()->get(Data::class);
}

/**
Expand All @@ -44,18 +74,18 @@ public function execute()
$resultRedirect = $this->resultRedirectFactory->create();
if (!empty($email) && !empty($params)) {
// Validate received data to be an email address
if (\Zend_Validate::is($email, 'EmailAddress')) {
if (\Zend_Validate::is($email, EmailAddress::class)) {
try {
$this->securityManager->performSecurityCheck(
\Magento\Security\Model\PasswordResetRequestEvent::ADMIN_PASSWORD_RESET_REQUEST,
PasswordResetRequestEvent::ADMIN_PASSWORD_RESET_REQUEST,
$email
);
} catch (\Magento\Framework\Exception\SecurityViolationException $exception) {
} catch (SecurityViolationException $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
return $resultRedirect->setPath('admin');
}
$collection = $this->_objectManager->get('Magento\User\Model\ResourceModel\User\Collection');
/** @var $collection \Magento\User\Model\ResourceModel\User\Collection */
$collection = $this->userCollectionFactory->create();
$collection->addFieldToFilter('email', $email);
$collection->load(false);

Expand All @@ -65,9 +95,7 @@ public function execute()
/** @var \Magento\User\Model\User $user */
$user = $this->_userFactory->create()->load($item->getId());
if ($user->getId()) {
$newPassResetToken = $this->_objectManager->get(
'Magento\User\Helper\Data'
)->generateResetPasswordLinkToken();
$newPassResetToken = $this->backendDataHelper->generateResetPasswordLinkToken();
$user->changeResetPasswordLinkToken($newPassResetToken);
$user->save();
$user->sendPasswordResetConfirmationEmail();
Expand All @@ -86,7 +114,7 @@ public function execute()
$this->messageManager->addSuccess(__('We\'ll email you a link to reset your password.'));
// @codingStandardsIgnoreEnd
$this->getResponse()->setRedirect(
$this->_objectManager->get('Magento\Backend\Helper\Data')->getHomePageUrl()
$this->backendDataHelper->getHomePageUrl()
);
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,32 @@
*/
namespace Magento\User\Controller\Adminhtml\Auth;

class ResetPasswordPost extends \Magento\User\Controller\Adminhtml\Auth
use Magento\User\Controller\Adminhtml\Auth;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Backend\Helper\Data;
use Magento\User\Model\UserFactory;

class ResetPasswordPost extends Auth
{
/**
* @var Data
*/
private $backendDataHelper;

/**
* @param Context $context
* @param UserFactory $userFactory
* @param Data $backendDataHelper
*/
public function __construct(
Context $context,
UserFactory $userFactory,
Data $backendDataHelper = null
) {
parent::__construct($context, $userFactory);
$this->backendDataHelper = $backendDataHelper ?: ObjectManager::getInstance()->get(Data::class);
}
/**
* Reset forgotten password
*
Expand All @@ -27,7 +51,7 @@ public function execute()
} catch (\Exception $exception) {
$this->messageManager->addError(__('Your password reset link has expired.'));
$this->getResponse()->setRedirect(
$this->_objectManager->get('Magento\Backend\Helper\Data')->getHomePageUrl()
$this->backendDataHelper->getHomePageUrl()
);
return;
}
Expand All @@ -53,7 +77,7 @@ public function execute()
$user->save();
$this->messageManager->addSuccess(__('You updated your password.'));
$this->getResponse()->setRedirect(
$this->_objectManager->get('Magento\Backend\Helper\Data')->getHomePageUrl()
$this->backendDataHelper->getHomePageUrl()
);
}
} catch (\Magento\Framework\Validator\Exception $exception) {
Expand Down