9

It is simple to add new admin user in Magento 1.9

<?php
require_once('app/Mage.php');
umask(0);
Mage::app();

$user = Mage::getModel('admin/user')
    ->setData(array(
        'username'  => 'admin',
        'firstname' => 'admin',
        'lastname'  => 'admin',
        'email'     => '[email protected]',
        'password'  => 'hacker@123',
        'is_active' => 1
    ))
    ->save();

$user->setRoleIds(array(1))
    ->setRoleUserId($user->getUserId())
    ->saveRelations();

echo "User has been created successfully!";

?>

But how can I add admin user in Magento 2.0?

1
  • 1
    You have tagged this with magento-2.1 but mention Magento 2.0 in the text. Which one is it? There is a generic magento2 tag that you may wish to use.
    – user21075
    May 24, 2017 at 17:02

4 Answers 4

9

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'     => '[email protected]',
        '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

2
  • The git url shared is 404
    – Gagan
    Feb 21, 2018 at 11:16
  • 1
    Modified the url
    – Priyank
    Feb 21, 2018 at 11:23
8

You can create the admin user by running the following command via SSH on the root directory level.

php bin/magento admin:user:create --admin-user="admin" --admin-firstname="Admin" --admin-lastname="A" --admin-email="[email protected]" --admin-password="admin@5252"
3
  • By this way, how can you set the role?
    – Marc Pont
    Jul 24, 2017 at 14:53
  • Not sure have to check out
    – sandip
    Jul 24, 2017 at 16:15
  • 1
    perfect made my day!!! +1 :) Jan 18, 2019 at 8:59
2

Create a file in root directory. for ex. admin.php

<?php 
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$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(); 

    } catch (\Exception $ex) {
        echo $ex->getMessage();
         exit;
    }
    echo "User is sucessfully created!"
?>
1

If it is a one time use or if you need it during development, then alternatively you may execute the below mysql commands on phpMyAdmin or mysql command line to create an admin user.

LOCK TABLES `admin_role` WRITE , `admin_user` WRITE;
SET @SALT = "rp";
SET @PASS = CONCAT(MD5(CONCAT( @SALT , "password") ), CONCAT(":", @SALT ));
SELECT @EXTRA := MAX(extra) FROM admin_user WHERE extra IS NOT NULL;
INSERT INTO `admin_user` (firstname,lastname,email,username,password,created,lognum,reload_acl_flag,is_active,extra,rp_token_created_at) 
VALUES ('Firstname','Lastname','[email protected]','adminuser',@PASS,NOW(),0,0,1,@EXTRA,NOW());
INSERT INTO `admin_role` (parent_id,tree_level,sort_order,role_type,user_id,role_name) 
VALUES (1,2,0,'U',(SELECT user_id FROM admin_user WHERE username = 'adminuser'),'Firstname');
UNLOCK TABLES;

Find explanation and more details here

Hope this helps people who are looking for similar issue/solution.

1
  • This isn't working for me. On a fresh Magento 2.4 install there is no 'admin_role' table.
    – Caleb
    Sep 16, 2020 at 17:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.