Hello Magento Folks,
Today We will discuss How to Get Admin User Data in Observer in Magento 2. Learn How to Add New Product Type in Magento 2 in our last published blog.
Magento allows for building events and observers. It is one type of Magento class where the created observer helps in identifying the performance, general behavior and to improve your business logic. Here, we will help you to get admin user data in the observer.
Steps to get admin user data in observer:
Step 1: Create events.xml file in your module at the below-given path:
Path: app\code\Webiators\AdminUserData\etc\events.xml
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="backend_auth_user_login_success"> <observer name="backend_auth_user_login_success_observer" instance="Webiators\AdminUserData\Observer\BackendAuthObserver" /> </event> </config> |
Step 2: Now create an observer file at the below path.
Path: app\code\Webiators\AdminUserData\Observer\Backenduserobserver.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace Webiators\AdminUserData\Observer; class Backenduserobserver implements \Magento\Framework\Event\ObserverInterface { public function execute(\Magento\Framework\Event\Observer $observer) { $user = $observer->getEvent()->getUser(); $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/backend_auth_user_login_success.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info($user->getData()); return $this; } } |
Now go to your Backend and try to login with your username and password, and navigate to your var/log directory you can see backend_auth_user_login_success.log
file there with your user content.
That’s it!
Any doubts in the above method can be mentioned in the Comments section below.
Happy Coding!