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 😂
27 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.
Just hopped onto hi888win. Pretty standard fare, to be honest. The layout’s easy to navigate, which is a plus, but nothing super groundbreaking. Have you tried it yet at hi888win?
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.info/register?ref=IHJUI7TF
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://accounts.binance.com/si-LK/register-person?ref=LBF8F65G
Với 888SLOT , bạn sẽ không bao giờ gặp phải tình trạng bị văng game khi đang đặt cược. Hệ thống máy chủ được đặt tại nước ngoài cực mạnh. TONY01-06S
Sup! So, I browsed dk999game the other day, and while it’s not gonna blow your mind, it’s got some interesting titles worth a look. Definitely a place to dig around if you’re in a ‘what to play’ crisis. Hop on dk999game and discover a gem
Win88apps, downloading now, hoping to win! Wish me luck, guys! Try those apps: win88apps
I relish, cause I found exactly what I was looking for.
You have ended my 4 day lengthy hunt! God Bless you man. Have a great day.
Bye
I’d like to find out more? I’d love to find out some additional information.
It’s hard to find well-informed people for this topic, however, you
seem like you know what you’re talking about! Thanks
Good day! This is kind of off topic but I need some guidance from an established blog.
Is it very hard to set up your own blog? I’m not very techincal but I can figure
things out pretty quick. I’m thinking about creating my own but I’m not
sure where to begin. Do you have any tips or suggestions?
Cheers
Hello! I’m at work browsing your blog from my new iphone 3gs!
Just wanted to say I love reading through your blog and look forward to all your posts!
Keep up the fantastic work!
I love reading through an article that can make men and women think.
Also, thank you for allowing for me to comment!
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? https://www.binance.com/register?ref=IXBIAFVY