When we work in magento with out of box customization then we might need to create/update admin user information programmatically in magento2.

In this blog, We will see how we can create and update admin user information programmatically.

Admin User Create/Update

Here is code for create/update admin user using data patch. We can use similar code in Model or Helper classes.

PHP
<?php

declare(strict_types=1);

namespace MageDad\Module\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\User\Model\UserFactory;

class CreateUserUpdate implements DataPatchInterface
{
    public function __construct(
        private UserFactory $userFactory,
    ) {
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $adminInfo = [
            'username'  => 'johndoe',
            'firstname' => 'John',
            'lastname'    => 'Doe',
            'email'     => 'johndoe@johndoe.com',
            'password'  =>'johndoe@123',
            'interface_locale' => 'en_US',
            'is_active' => 1
        ];

        $userModel = $this->userFactory->create();
        $userModel->setData($adminInfo);
        $userModel->setRoleId(1);
        $userModel->save();
        
        # Admin user update code
        $userModel = $this->userFactory->create()->loadByUsername('johndoe');
        # $userModel = $this->userFactory->create()->load($userId);
        $userModel->setFirstname("Johnny");
        $userModel->setLastname("Dost");
        $userModel->save();
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

Our aims to write quality blog with tested code so we add this code to GitHub
You can see this GitHub commit of create/update admin user.

I hope this blog is useful to create/update admin user in magento2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.

Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😂

14 Comments

  1. **breathe**

    breathe is a plant-powered tincture crafted to promote lung performance and enhance your breathing quality.

  2. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  3. I have been exploring for a little bit for any high quality articles
    or blog posts in this kind of house . Exploring in Yahoo
    I finally stumbled upon this website. Reading this info So i’m glad to exhibit that I’ve a
    very excellent uncanny feeling I found out exactly what I needed.
    I most certainly will make sure to don?t disregard this website and give it a glance on a relentless basis.

  4. I pay a visit day-to-day some web pages and sites to read posts,
    except this blog presents feature based articles.

  5. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  6. Hi I am so grateful I found your blog page, I
    really found you by mistake, while I was browsing on Google for something
    else, Anyhow I am here now and would just like to
    say many thanks for a incredible post and a all round enjoyable blog (I also love the theme/design),
    I don’t have time to read it all at the moment but I have book-marked it and also included your
    RSS feeds, so when I have time I will be back to read
    a lot more, Please do keep up the superb work.

Write A Comment