In this blog in explain about how we can get current admin user details from session.
We need to use session class Magento\Backend\Model\Auth\Sessiont
to get current loggedin admin data.
Here is simple code is to get admin details. Code is writter with PHP8 and supported to magento 2.4.6 😍 Created one simple class and loaded Session
in construct method
<?php
declare(strict_types = 1);
namespace Vendor\Module\Model;
use Magento\Backend\Model\Auth\Session;
class ClassName
{
public function __construct(
private Session $authSession
) {}
public function getCurrentUser()
{
return $this->authSession->getUser();
//get admin user name $this->authSession->getUser()->getUsername();
//get current admin email $this->authSession->getUser()->getEmail();
}
}
You can get more details about user with different methods like for get admin user name $this->authSession->getUser()->getUsername();
Here is code with Object Manager. We do not recommend to use Object Manager.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\Backend\Model\Auth\Session')->getUser()->getUsername();
I hope this blog is useful to get current admin user details 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 😄
10 Comments
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.
Your article helped me a lot, is there any more related content? Thanks!
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
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?
Your article helped me a lot, is there any more related content? Thanks!
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
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?
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?
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?