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 🙂
Be the first to comment.