Magento 2 get current admin user detail
Today we talk about how in Magento 2 get current admin user detail?. in this tutorial we will cover how to get current admin username and current admin email and other information included as well. So let’s start with our code example.
protected $authSession;
public function __construct(
\Magento\Backend\Model\Auth\Session $authSession,
) {
$this->authSession = $authSession;
}
public function getCurrentUser()
{
return $this->authSession->getUser();
}
public function __construct(
\Magento\Backend\Model\Auth\Session $authSession,
) {
$this->authSession = $authSession;
}
public function getCurrentUser()
{
return $this->authSession->getUser();
}
Now you have access to current admin information. You can get information with the following code.
$this->getCurrentUser->getUsername(); //get admin user name
$this->getCurrentUser->getEmail(); //get current admin email
$this->getCurrentUser->getEmail(); //get current admin email
2 thoughts on “Magento 2 get current admin user detail”
Leave a Reply
You must be logged in to post a comment.
Thanks for sharing this great tutorial.
To edit core files it is highly recommended to create a custom module. So my question is how to implement your provided code within a custom module?
i can give you example of using in your controller.
/**
* Hello World
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/
namespace QaisarSatti\HelloWorld\Controller\Adminhtml\Index;
class Index extends \Magento\Backend\App\Action
{
protected $resultPageFactory;
protected $authSession;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Backend\Model\Auth\Session $authSession,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context, $coreRegistry);
$this->authSession = $authSession;
}
public function execute()
{
$user = $this->authSession->getUser();
echo $user->getUsername();
}
protected function _isAllowed()
{
return true;
}
}