preloder

How to create admin user in Magento 2 via Command Line

Prev Next

In case you have a problem with creating new admin user via Command Line in Magento 2 this post will help you.

I’ve faced with the problem on my development environment when trying to access Magento 2 admin panel. Obviously, with the number of systems and websites, we use on day-to-day basis and number of passwords we have to have it is easy to forget one :), or few of them.

Luckily, Magento 2 CLI has a special command for admin user creation. Very handy to have a command for such purpose, so you don’t need to search all over the internet on how to update the password in Magento 2, as we get used to doing it with Magento 1.

The Admin User Create Command

The admin:user:create command is used to create new admin user via command line. This command would work in case you have a vanilla Magento 2 installation without any production changes to the authorization_roletable.

Command arguments were as follow, as per recommendation from Alex Paliarush:

$ bin/magento admin:user:create --admin-user="admin" --admin-password="123123q" --admin-email="admin@example.com" --admin-firstname="Admin" --admin-lastname="Admin"

The error I’ve seen after execution:

No Administrators role was found, data fixture needs to be run

This error says nothing about why I don’t have new user created.

The Magento\Setup\Model\AdminAccount::save() method is responsible for new admin user creation. In order to make execution of command successfull, the save() method uses saveAdminUserRole() method and it triggers another method, called retrieveAdministratorsRoleId. As per method PHPDoc description, it says “Gets the “Administrators” role id, the special role created by data fixture in Authorization module.”

    private function retrieveAdministratorsRoleId()
    {
        // Get Administrators role id to use as parent_id
        $administratorsRoleData = [
            'parent_id'  => 0,
            'tree_level' => 1,
            'role_type' => Group::ROLE_TYPE,
            'user_id' => 0,
            'user_type' => UserContextInterface::USER_TYPE_ADMIN,
            'role_name' => 'Administrators',
        ];
        $result = $this->setup->getConnection()->fetchRow(
            'SELECT * FROM ' . $this->setup->getTable('authorization_role') . ' ' .
            'WHERE parent_id = :parent_id AND tree_level = :tree_level AND role_type = :role_type AND ' .
            'user_id = :user_id AND user_type = :user_type AND role_name = :role_name',
            $administratorsRoleData
        );

        if (empty($result)) {
            throw new \Exception('No Administrators role was found, data fixture needs to be run');
        } else {
            // Found at least one, use first
            return $result['role_id'];
        }
    }

The thing is, if you have changed Role Name for User Groups in the admin panel, you will see Exception message and no new admin user will be created. In my case the role_name is “Admin Users” instead of default “Administrators”. So I have to change whether Role Name to match the code or the code to match the expected behavior. I believe the second option is more preferable since we, as developers are expected to write code to help our Customers or Merchants to drive business fast and smoothly.

Hope this post will help you to overcome time investigating why the admin:user:create command might not work as expected.

 

Max Pronko is an international Magento Expert, development consultant, motivational speaker and Magento award-winning developer. He is known for the educational development programs for organizations of all sizes and has positively affected thousands of people and organizations worldwide.

Posted on Mar 13, 2017

The Devletter

Subscribe to my Magento 2 tips and tutorials content.
Every week I send an interesting email directly to your inbox

Related Posts