Skip to content

Admin user auth controller refactor #16560

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

Conversation

AnshuMishra17
Copy link
Contributor

Description

Refactor the code by removing the direct use of ObjectManager and includes the dependencies using Constructor Dependency Injection

Manual testing scenarios

  1. Created a new admin user
  2. Login and logout using new admin user
  3. Clicked on forgot password and provided new admin user email id.
  4. Clicked on reset password link in email.
  5. Created new password from reset password page.
  6. Login using new password.

@magento-engcom-team
Copy link
Contributor

Hi @AnshuMishra17. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me {$VERSION} instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

/**
* @var \Magento\Backend\Helper\Data
*/
protected $_backendHelper;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually a backendDataHelper. The Backend\Helper namespace has more Helper classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\User\Model\UserFactory $userFactory
\Magento\User\Model\UserFactory $userFactory,
\Magento\Backend\Helper\Data $backendHelper = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could do the PR on 2.3-develop instead for the refactoring. Than you can remove the = null part and you don't need the if statement on line 42 and 43.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do a forwardport for 2.3-develop.

@@ -6,6 +6,7 @@
namespace Magento\User\Controller\Adminhtml;

use Magento\Framework\Encryption\Helper\Security;
use Magento\Framework\App\ObjectManager;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are refactoring, please add import statement for all used classes in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please also do the ones on line 24 and 29.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -7,26 +7,38 @@
namespace Magento\User\Controller\Adminhtml\Auth;

use Magento\Security\Model\SecurityManager;
use Magento\Framework\App\ObjectManager;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please import all used classes in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\User\Model\UserFactory $userFactory,
\Magento\Security\Model\SecurityManager $securityManager
\Magento\Security\Model\SecurityManager $securityManager,
\Magento\User\Model\ResourceModel\User\CollectionFactory $userCollectionFactory = null
Copy link
Contributor

@arnoudhgz arnoudhgz Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could do the PR on 2.3-develop instead for the refactoring. Than you can remove the = null part and you don't need the if statement on line 40 and 41.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do a forwardport for 2.3-develop.

@@ -54,7 +66,7 @@ public function execute()
$this->messageManager->addErrorMessage($exception->getMessage());
return $resultRedirect->setPath('admin');
}
$collection = $this->_objectManager->get(\Magento\User\Model\ResourceModel\User\Collection::class);
$collection = $this->userCollectionFactory->create();
/** @var $collection \Magento\User\Model\ResourceModel\User\Collection */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch line 69 and 70

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

use Magento\Framework\App\ObjectManager;
use Magento\Backend\App\Action\Context;
use Magento\User\Model\UserFactory;
use Magento\Security\Model\SecurityManager;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This specific one is now imported twice. Please fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@arnoudhgz
Copy link
Contributor

@AnshuMishra17 could you make the PR to 2.3-develop, than you could make it backwards-incompatible and remove the ObjectManager

\Magento\User\Model\UserFactory $userFactory
Context $context,
UserFactory $userFactory,
Data $backendDataHelper = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do PR on 2.3-develop and remove the =null

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

) {
parent::__construct($context);
$this->_userFactory = $userFactory;
$this->_backendDataHelper = $backendDataHelper ?: ObjectManager::getInstance()->get(Data::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do PR on 2.3-develop and remove the ?: ObjectManager::getInstance()->get(Data::class); part

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

Context $context,
UserFactory $userFactory,
SecurityManager $securityManager,
CollectionFactory $userCollectionFactory = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do PR on 2.3-develop and remove the =null

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

) {
parent::__construct($context, $userFactory);
$this->securityManager = $securityManager;
$this->userCollectionFactory = $userCollectionFactory ?:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do PR on 2.3-develop and remove the ?: ObjectManager::getInstance()->get(Data::class); part

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

@AnshuMishra17
Copy link
Contributor Author

How can I make backward incompatible?
Do I need to close this one and create a new PR for 2.3-develop?
I am not sure what are the correct steps for this.
Kindly share the steps.

@arnoudhgz
Copy link
Contributor

arnoudhgz commented Jul 6, 2018

@AnshuMishra17

First option (probably not working)
You can try to just update the target:
image

Second option
If that is not possible start a new branch from 2.3-develop, do a PR for the new one and close this indeed. Please add a comment to me (with @arnoudhgz mention) the new PR referring this old one.

@AnshuMishra17
Copy link
Contributor Author

@arnoudhgz Second option seems to be better. But I am not aware of how to fetch/pull 2.3-develop. I would be nice and helpful if you can share correct steps.

@arnoudhgz
Copy link
Contributor

@AnshuMishra17 You created a local branch from 2.2-develop, right?

  1. You should first update your fork: https://help.github.com/articles/syncing-a-fork/
  2. Checkout the 2.3-develop branch
  3. Create a new branch from that branch and name it, similar to you current branch (but keep it specific for 2.3 so you don't mixup)
  4. Apply the changes manually in the same files
  5. Do a PR again

\Magento\User\Model\UserFactory $userFactory
Context $context,
UserFactory $userFactory,
Data $backendDataHelper = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

) {
parent::__construct($context);
$this->_userFactory = $userFactory;
$this->_backendDataHelper = $backendDataHelper ?: ObjectManager::getInstance()->get(Data::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

Context $context,
UserFactory $userFactory,
SecurityManager $securityManager,
CollectionFactory $userCollectionFactory = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

) {
parent::__construct($context, $userFactory);
$this->securityManager = $securityManager;
$this->userCollectionFactory = $userCollectionFactory ?:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was done correctly, no need to introduce backward incompatible changes if possible

/**
* @var Data
*/
protected $_backendDataHelper;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make this variable private and remove underscore

@arnoudhgz
Copy link
Contributor

@AnshuMishra17 my apologies, I thought you shouldn't use the ObjectManager anymore like this. Please let @ihor-sviziev help you out with this PR.

Thanks @ihor-sviziev I learned something today too.

@magento-engcom-team
Copy link
Contributor

Hi @ihor-sviziev, thank you for the review.
ENGCOM-2282 has been created to process this Pull Request

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Intregration tests failed and DI compile also failed. Please try to fix them

@magento-engcom-team
Copy link
Contributor

Hi @ihor-sviziev, thank you for the review.
ENGCOM-2282 has been created to process this Pull Request

@magento-engcom-team
Copy link
Contributor

Hi @AnshuMishra17. Thank you for your contribution.
We will aim to release these changes as part of 2.2.6.
Please check the release notes for final confirmation.

Please, consider to port this solution to 2.3 release line.
You may use Porting tool to port commits automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants