I want to create admin user from the root directory Is it possible? How can I create a new admin user without CLI (SSH) and database?
1 Answer
Please create one file named "user.php" in your root of magento and copy paste below code in your file
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$userFactory = $objectManager->get('\Magento\User\Model\UserFactory');
try{
$adminInfo = [
'username' => 'magento',
'firstname' => 'magento',
'lastname' => 'magento',
'email' => '[email protected]',
'password' =>'magento@123',
'interface_locale' => 'en_US',
'is_active' => 1
];
$userModel = $userFactory->create();
$userModel->setData($adminInfo);
$userModel->setRoleId(1);
$userModel->save();
echo "User is sucessfully created!";
} catch (\Exception $ex) {
echo $ex->getMessage();
exit;
}
Run this script in browser or you can also run this file in CLI. After creation of user please go to admin panel and try to login with the below credentials as per mentioned in script.
Username :- magento
Password :- magento@123
-
Could you please approve my answer too?ZealousWeb– ZealousWeb2021-02-10 09:15:14 +00:00Commented Feb 10, 2021 at 9:15