How to create magento 2 admin user from the root folder?
-
12-01-2021 - |
Question
I want to create admin user from the root directory as it can be done in magento-1.9 ,
Is it possible? How can I create a new admin user without CLI (SSH) and database?
Solution
You can create user using userFactory
/**
* User model factory
*
* @var \Magento\User\Model\UserFactory
*/
protected $_userFactory;
public function __construct(
\Magento\User\Model\UserFactory $userFactory,
) {
$this->_userFactory = $userFactory;
}
public function execute(){
$adminInfo = [
'username' => 'killer',
'firstname' => 'admin',
'lastname' => 'admin',
'email' => 'me@helloworld.com',
'password' =>'hello@123',
'interface_locale' => 'en_US',
'is_active' => 1
];
$userModel = $this->_userFactory->create();
$userModel->setData($adminInfo);
$userModel->setRoleId(1);
try{
$userModel->save();
} catch (\Exception $ex) {
$ex->getMessage();
}
}
You can also refer how magento creates user git source
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange