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 😄