Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DOC

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 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 🙂

. . .

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