Menu Close
    Searching for an experienced Magento 2 Development Company ?

    How to Get Role and Allowed ACL Resources for Logged in Admin User?

    To get the Role of the Admin Logged in User and the Resources allowed to the logged in user we can have use the following functions. I have declared them in Helper file of my module.

    <?php
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_MpMassUpload
     * @author    Webkul
     * @copyright Copyright (c) 2010-2018 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    namespace Webkul\AdminRole\Helper;
    
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        protected $authSession;
    
        protected $aclRetriever;
    
        /**
         * @param \Magento\Framework\App\Helper\Context $context
         * @param \Magento\Backend\Model\Auth\Session $authSession
         * @param \Magento\Authorization\Model\Acl\AclRetriever $aclRetriever
         */
        public function __construct(
            \Magento\Framework\App\Helper\Context $context,
            \Magento\Backend\Model\Auth\Session $authSession,
            \Magento\Authorization\Model\Acl\AclRetriever $aclRetriever
        ) {
            $this->authSession = $authSession;
            $this->aclRetriever = $aclRetriever;
            parent::__construct($context);
        }
    
        /**
         * function to get logged in user role
         *
         * @return string
         */  
        public function getRole() {
            return $this->authSession->getUser()->getRole()->getRoleName();
        }
    
        /**
         * function to get the Allowed resources to logged in User
         *
         * @return array
         */
        public function getAllowedResource() {
            $roleId = $this->authSession->getUser()->getRole()->getRoleId();
            return $this->aclRetriever->getAllowedResourcesByRole($roleId);
        }
    }
    

    Thanks 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top