:::: MENU ::::

Magento 2 get current admin user detail

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

Give A Rating

Average Rating / 5. Vote Count

Author: Qaisar Satti
Category: Magento 2
Last Modified: February 27, 2019


2 Comments

  • Reply Syed Muneeb Ul Hasan |

    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?

    • Reply qaisarsatti |

      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;
          }
      }

So, what do you think ?