How to get the current admin user detail in back-end ?
3 Answers
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()
.
-
1Notice: After adding the codes, we need run
bin/magento setup:di:compile
, otherwise it would appear "Session none given" error. Commented Oct 17, 2017 at 9:01 -
-
@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
-
-
is there any method to know current user duration ? Commented Sep 26, 2020 at 18:05
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();
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.