Read More
Menu Close

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

    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

    <?php
    /**
     * 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" */
            $name,
            $primaryFieldName,
            $requestFieldName,
            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*/
    		
        
    		/*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()) {
                $this->getCollection()->load();  
            }
            return $this->getCollection()->toArray();
        }
    
    }
    

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

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    1 comments

  • Aneh Thakur
    hi, can you plz tell me how to get current user id if user is logged using rest api in android device i have token i didn’t find any relevant service thanks
  • Back to Top