11

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

28

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().

  • 1
    Notice: After adding the codes, we need run bin/magento setup:di:compile, otherwise it would appear "Session none given" error. – Key Shang Oct 17 '17 at 9:01
  • perfect answer with explanation!!! +1 :) – SagarPPanchal Jul 2 '18 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. – Masud Shaikh Oct 22 '18 at 11:39
  • you cannot access admin session from frontend. – Marius Oct 22 '18 at 12:09
5

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();
  • Not necessary to inject in your controller though. – MagePsycho May 19 '16 at 10:47
3

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, privacy policy and cookie policy

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