-1

I want to create admin user from the root directory as it can be done in magento-1.9 ,

Is it possible? How can I create a new admin user without CLI (SSH) and database?

0

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

  • When addthis script & run in front then we get error 'Parse error: syntax error, unexpected 'protected' (T_PROTECTED) ' in magento 2. – Anil Jun 17 at 6:43

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.