Magento2 admin login user detail and role module
how to get Magento2 admin login user detail and role, in this post we will show you how to get admin user detail and it’s roles(for login user in admin section) in this custom module.
This Magento2 admin login user detail and role module can use in over block , model, controller class as your requirement requirement.
First hea we need to pass object of "\Magento\Backend\App\Action\Context"
in over class constructor where we want to get login user detail.
suppose we want to use it in a data provider class for get collection according to role of admin user. and chnage your namespace
and Model name
in this code.
<?php /* * how to get Magento2 admin login user detail * namespace = onlinecode * Model name = OnlinecodeModule */ namespace onlinecode\OnlinecodeModule\Ui\DataProvider\Order; use onlinecode\OnlinecodeModule\Model\ResourceModel\Order\CollectionFactory; /* use this get details for admin login user */ use Magento\Backend\App\Action; /* * we use Class OrderDataProvider */ // extends AbstractDataProvider class OrderDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider { /** * Construct * @param Action\Context $context * parameter we pass a object * of "Magento\Backend\App\Action" in this construct class * */ public function __construct( Action\Context $context, /* object of "Magento\Backend\App\Action" */ $user_name, $primary_Field_Name, $request_Field_Name, CollectionFactory $collectionFactory, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollection, array $addFieldStrategies = [], array $addFilterStrategies = [], array $meta_data = [], array $user_data = [] ) { // pass data in to construct function parent::__construct($user_name, $primary_Field_Name, $request_Field_Name, $meta_data, $user_data); /* * get login user detail raleted to authentication */ $get_authentication = $context->getAuth(); /* * class "Magento\Backend\App\Action" object is $context */ $login_user = $get_authentication->getUser(); /* get detail of Login user */ $login_User_Role = $login_user->getRole(); /* get Role of Login user */ /* * here you can get collection on base of these details */ $this->collection(); /* you requirement for collection */ } /** * Get data for user * * return data as array */ public function getData() { if (!$this->getCollection()->isLoaded()) { // get data $this->getCollection()->load(); } // data as array return $this->getCollection()->toArray(); } }
You also like Get Order Details using Order/a> and customer registration programmatically