0

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 1

2

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
1
  • Could you please approve my answer too? Commented Feb 10, 2021 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.