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,
1. Using Block:
namespace Vendor\Extension\Block; use Magento\Backend\Model\Auth\Session use 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();
2. Using Object Manager:
$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.
Thank you!