Back to Top

Magento2 admin section : Get login user detail and it’s role

Updated 21 February 2024

Here we learn how we get user detail and it’s roles(login user in admin section) in our custom module.

First you need to pass object of  “\Magento\Backend\App\Action\Context” in your class constructor where you want to get login user detail .

suppose you want to use it in a data provider class for get collection according to role of admin user

 * Copyright © 2015 Webkul. All rights reserved.
 * See COPYING.txt for license details.
namespace Webkul\YourModule\Ui\DataProvider\Order;

use Webkul\YourModule\Model\ResourceModel\Order\CollectionFactory;
use Magento\Backend\App\Action; /* use it for get admin login user details  */

 * Class OrderDataProvider
class OrderDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
     * Construct
     * @param Action\Context $context // as a parameter we pass a object
     *                      of "Magento\Backend\App\Action" in our class construct
    public function __construct(
        Action\Context $context, /* object of "Magento\Backend\App\Action" */
        CollectionFactory $collectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory  $orderCollection,
        array $addFieldStrategies = [],
        array $addFilterStrategies = [],
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        /*for get detail raleted to login user authentication */
        $auth = $context->getAuth();/* $context is an object 
                             of class "Magento\Backend\App\Action" */

        $loginUser = $auth->getUser(); /*get Login user detail*/

        $loginUserRole = $loginUser->getRole();/*get Login user Role. You can get user data by calling getDtata() function on this*/
		/*here you can get collection on base of these details */
        $this->collection = /* collection as you requirement */

     * Get data
     * @return array
    public function getData()
        if (!$this->getCollection()->isLoaded()) {
        return $this->getCollection()->toArray();


You can use it in your block , model, controller class as per your requirement

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • Aneh Thakur
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home