Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5488

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

$
0
0

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


Viewing all articles
Browse latest Browse all 5488

Trending Articles