How to Create Admin User Programmatically in Magento 2?
In this blog, we will learn about how to create admin user in Magento 2?. Sometimes we need to create new admin users for different members to provide access to our backend.
There are three ways to create admin user in Magento 2:
1) Create Admin User Via Magento Admin
2) Create Admin User Via Command Line
3) Create Admin User Using PHP Script
Let’s see all three ways in details.
1. Create Admin User Via Magento Admin: Step-By-Step
For creating new user using admin panel, then follow below steps:
Step 1: Login Admin Panel
Step 2: Create User Role using admin panel(Optional)
If you want to use new user role then follow below steps:
-Go to the System→Permissions→User Roles→Add New Role.
Write Role information in the Role Name and Password. In the password, you can set current admin panel password.
In Role Resources Select “Custom” option if you want to provide custom access and select “All” if you want to provide access in all pages.
After fill all information, save role, and you can see your New Role is created. Here we created Sales Role.
Step 3: Go to the System→Permissions→All Users→Add New User.
Step 4: Fill all the account information for new user
Fill Account Information and current admin panel password and then Save User.
Step 5: Assign User Role
After following above steps, you can create Admin User Via Magento Admin Panel.
2. Create Admin User in Magento 2 Using Command Line
Another way to create Admin User via command line. In this way, we run follow commands in our Magento root folder:
bin/magento admin:user:create
After run above command it will ask Admin user, Admin password, Admin email, Admin first name and Admin last name.
After fill above fields, you can get ‘Created Magento administrator user named MageAnts’ message, so your admin user will be created.
3. Create Admin User Using PHP Script
For create admin user programmatically, you need to create PHP file at Magento root. For example, we create adminuser.php file at Magento root directory and write below code.
<?php use Magento\Framework\App\Bootstrap; require 'app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); try { $adminData = [ 'username' => 'mageants123', 'firstname' => 'admin', 'lastname' => 'admin123', 'email' => 'mageants123@mageants.com', 'password' =>'mageants123', 'interface_locale' => 'en_US', 'is_active' => 1 ]; $userFactory = $objectManager->get('\Magento\User\Model\UserFactory'); $userModel = $userFactory->create(); $userModel ->setData($adminData) ->setRoleId(1) // 1 is administrator unless something has been customized ->save(); } catch (\Exception $exception) { echo $exception->getMessage(); exit; } printf("User %s created", $adminData['username']); |
After create above file, then run command PHP adminuser.php in terminal and create new user in admin.
Here we use username ‘mageants123’ and its password is ‘mageants123’.
Conclusion:
Using above article you can easily understand How to Create Admin User in Magento 2 with admin panel, command line and PHP script. If you have any query regarding above code implementation then you can contact us or let us know in comment section.