17

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

3 Answers 3

46

you need to add this to the constructor of your class

protected $authSession;
public function __construct(
    ....
    \Magento\Backend\Model\Auth\Session $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().

5
  • 1
    Notice: After adding the codes, we need run bin/magento setup:di:compile, otherwise it would appear "Session none given" error.
    – Key Shang
    Commented Oct 17, 2017 at 9:01
  • perfect answer with explanation!!! +1 :) Commented Jul 2, 2018 at 6:32
  • @Marius I have added login as customer form backend. But I want admin details on product view page which admin login to the customer. Commented Oct 22, 2018 at 11:39
  • you cannot access admin session from frontend.
    – Marius
    Commented Oct 22, 2018 at 12:09
  • is there any method to know current user duration ? Commented Sep 26, 2020 at 18:05
9

How to get current admin user detail?

inject backend session in your controller

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

}

and use this to get user name or email

 $this->authSession->getUser()->getUsername();
$this->authSession->getUser()->getEmail();
1
  • Not necessary to inject in your controller though.
    – MagePsycho
    Commented May 19, 2016 at 10:47
5

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

    /** @var \Magento\User\Model\User $user*/
    $user = $this->_auth->getUser();

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.