How to Get Current Admin User Detail in Magento 2
The admin in Magento 2 knows the admin panel like no one else! There can be as many admin users as required with different level of access and roles. The various roles ensure security to the store.
There are 3 types of admin roles in Magento 2:
- Account owner role
- Project-level roles
- Environment-level roles
Each role has its own level of access and tasks. The super admin can assign these roles to the users. After assigning these roles, the admin may need to get the details of current logged in admin user for various purposes such as, sending a message with the details of the admin user, get an Email notification with the admin login details or keep a check on each admins’ activity log! To serve the purpose, the super admin needs to identify the current admin user logged and get the details such as admin username, email address, etc.and other details. Hence, here I’ll show you how to get current admin user detail in Magento 2.
Methods to Get Current Admin User Detail in Magento 2:
Implement the below codes, following any of the above methods,
-
Using Block:
1234567891011121314151617namespace Vendor\Extension\Block;use Magento\Backend\Model\Auth\Sessionuse Magento\Framework\View\Element\Template;class View extends Template{protected $authSession;public function __construct(Session $authSession){$this->authSession = $authSession;}public function getCurrentUser(){return $this->authSession->getUser();}}//get admin user name $this->getCurrentUser->getUsername();//get current admin email $this->getCurrentUser->getEmail(); -
Using Object Manager:
12$objectManager = \Magento\Framework\App\ObjectManager::getInstance();$extensionUser = $objectManager->get('Magento\Backend\Model\Auth\Session')->getUser()->getUsername();
Follow any of the above methods to get current admin user detail in Magento 2.
Please use the Comments section below to express your opinions or ask any doubts in the above post. I’d be happy to help 🙂
Thank you!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
10 Comments
i am trying to get current admin details using Block but it’s return NULL can you please let me know how i can fix this issue.
Hello Aayushi,
You can try to check with object manager or helper class.
Thank You
Hi, i am using this implementation in an extended class block in the Adminhtml folder in a custom module(Block/Adminhtml), but it doesn’t work, always return null. Please let me know what i should check.
Thank you very much
Hello Mario,
Try using object manager and check whether it works or not.
Thank You
Hi,
so can you please let me know how i can get current admin details in front side??
Thanks.
One cannot get the current admin details from the frontend in Magento 2.
Hi,
i am checking in the frontend file , i have also check with using object manager but displaying error in front side “Call to a member function getUsername() on null”
The above solution only works in the admin file.
Hi,
i am trying to get current admin details using Blog but it’s return NULL can you please let me know how i can fix this issue.
please reply me soon
Thanks.
Are you checking in the admin file or frontend file?