Magento 2: How to get current admin user detail?

The question:

How to get the current admin user detail in back-end ?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

you need to add this to the constructor of your class

protected $authSession;
public function __construct(
    ....
    MagentoBackendModelAuthSession $authSession, 
    ....
) {
    ....
    $this->authSession = $authSession;
    ....
}

Then create this method

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

this will give you the current logged in admin.
You can later get the details like $user->getUsername() or $user->getEmail().

Method 2

How to get current admin user detail?

inject backend session in your controller

public function __construct(
....
MagentoBackendModelAuthSession $authSession, 
  ....
 ) {
  ....
   $this->authSession = $authSession;
   ....

}

and use this to get user name or email

 $this->authSession->getUser()->getUsername();
$this->authSession->getUser()->getEmail();

Method 3

Your Controller already extends MagentoBackendAppAction so it already has the authorization object. No additional injections are needed.
To get the user simply use this function:

    /** @var MagentoUserModelUser $user*/
    $user = $this->_auth->getUser();

Other answers are suggesting duplicate injections, which are not needed.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment