Back to Top

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

Updated 15 March 2024

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

<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_AdminRole
 * @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 🙂

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

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

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

Back to Home