在Magento 2.0中以编程方式创建管理员用户


8

在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!";

?>

但是如何在Magento 2.0中添加管理员用户?


1
您已使用magento-2.1标记了此名称,但在文本中提及了Magento 2.0。哪一个?您可能希望使用一个通用的magento2标签。

Answers:


8

您可以使用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();
    }
}

您还可以参考magento如何创建用户git源


共享的git网址是404
Gagan

修改了网址
Priyank

7

您可以通过在根目录级别通过SSH运行以下命令来创建admin用户。

php bin/magento admin:user:create --admin-user="admin" --admin-firstname="Admin" --admin-lastname="A" --admin-email="admin@admin.com" --admin-password="admin@5252"

这样,如何设置角色?
slayerbleast

不确定是否要退房
Sandip

1
完美的一天!+1 :)
SagarPPanchal

2

在根目录中创建一个文件。对于前。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!"
?>

1

如果是一次性使用,或者在开发过程中需要使用,则可以在phpMyAdmin或mysql命令行上执行以下mysql命令来创建管理员用户。

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;

在此处查找说明和更多详细信息

希望这对正在寻找类似问题/解决方案的人们有所帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.