How to create admin user in Magento 2 via Command Line

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

In case you have 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 handly to have a command for such purpose, so you don't need to search all over the internet on how to update password in Magento 2, as we get use to do 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 authorization_role table.

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 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 behaviour. 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.

magento 2

Related Articles

Comments

Next Article Previous Article

LinkedIn Twitter Facebook