8

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'     => 'me@hackme.com',
        '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
    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 '17 at 17:02
8

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

|improve this answer|||||
  • The git url shared is 404 – Gagan Feb 21 '18 at 11:16
  • Modified the url – Priyank Feb 21 '18 at 11:23
7

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="admin@admin.com" --admin-password="admin@5252"
|improve this answer|||||
  • By this way, how can you set the role? – slayerbleast Jul 24 '17 at 14:53
  • Not sure have to check out – sandip Jul 24 '17 at 16:15
  • 1
    perfect made my day!!! +1 :) – SagarPPanchal Jan 18 '19 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'     => 'magento@helloworld.com',
        '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!"
?>
|improve this answer|||||
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','firstname.lastname@nikinpages.com','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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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