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
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
**breathe**
breathe is a plant-powered tincture crafted to promote lung performance and enhance your breathing quality.
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?
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.
Right away I am going to do my breakfast, once having my breakfast coming again to read other news.
Very descriptive article, I enjoyed that a lot. Will there be a part 2?
I pay a visit day-to-day some web pages and sites to read posts,
except this blog presents feature based articles.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
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.
https://t.me/Top_BestCasino/160
At this moment I am going to do my breakfast, once
having my breakfast coming again to read further news.
Your article helped me a lot, is there any more related content? Thanks!
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.info/bg/register?ref=V2H9AFPY
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.