Magento 2 get current admin user detail

Today we talk about how in Magento 2 get current admin user detail?. in this tutorial we will cover how to get current admin username and current admin email and other information included as well. So let’s start with our code example.

protected $authSession;

public function __construct(
   
    \Magento\Backend\Model\Auth\Session $authSession,
   
) {
   
    $this->authSession = $authSession;
   
}

public function getCurrentUser()
{
    return $this->authSession->getUser();
}

Now you have access to current admin information. You can get information with the following code.

$this->getCurrentUser->getUsername(); //get admin user name

$this->getCurrentUser->getEmail();   //get current admin email

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

2 thoughts on “Magento 2 get current admin user detail

  1. Thanks for sharing this great tutorial.

    To edit core files it is highly recommended to create a custom module. So my question is how to implement your provided code within a custom module?

    1. i can give you example of using in your controller.

      <?php
      /**
          * Hello World
          *
          * @category    QaisarSatti
          * @package     QaisarSatti_HelloWorld
          * @author      Muhammad Qaisar Satti
          * @Email       qaisarssatti@gmail.com
          *
          */

      namespace QaisarSatti\HelloWorld\Controller\Adminhtml\Index;

      class Index extends \Magento\Backend\App\Action
      {
         
          protected $resultPageFactory;
          protected $authSession;


         
          public function __construct(
              \Magento\Backend\App\Action\Context $context,
              \Magento\Framework\Registry $coreRegistry,
              \Magento\Backend\Model\Auth\Session $authSession,
              \Magento\Framework\View\Result\PageFactory $resultPageFactory
          ) {
              $this->resultPageFactory = $resultPageFactory;
              parent::__construct($context, $coreRegistry);
              $this->authSession = $authSession;

          }

         
          public function execute()
          {
              $user = $this->authSession->getUser();
              echo $user->getUsername();
             

          }
         

       
          protected function _isAllowed()
          {
              return true;
          }
      }

Leave a Reply