Magento 2: How to get current admin user detail?
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()
.
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.