How to Remove Magento Admin Menu Items for Specific User Roles
In Magento, controlling access to specific admin menu items based on user roles is a common requirement for maintaining data security and restricting functionality. While the XML files provide a way to manage permissions, they don’t offer dynamic role-based restrictions.
Let’s explore how you can achieve this using Magento 2’s event system and layout processing.
Using Event Observation for Role-Based Menu Restrictions
Magento dispatches an adminhtml_menu_render event when rendering the admin menu. We can observe this event and modify the menu array accordingly based on the current user role.
Here’s a basic example of how to achieve this:
namespace Magento\SampleModule\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\Request\Http;
class AdminMenuObserver implements ObserverInterface
{
private $request;
public function __construct(Http $request)
{
$this->request = $request;
}
public function execute(EventObserver $observer)
{
if ($this->request->getUserRole() === ' Guest_Editor') {
$menuArray = $observer->getMenuArray();
unset($menuArray['cms']);
$observer->setMenuArray($menuArray);
}
}
}
In this example, for the role of Guest_Editor, we remove the CMS menu item from the $menuArray.
Using Layout Processing for Menu Item Removal
Another approach is to use layout processing. The prepareLayout method in your controller can be used to manipulate the admin menu.
namespace Magento\SampleModule\Controller\Adminhtml;
class Index extends AbstractIndex
{
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Magento_Cms::save');
}
public function execute()
{
$resultPage = $this->getResultPage();
$resultPage->setActiveMenu('Magento_Cms::cms_page');
if ($this->_request->getParam('show_hierarchy')) {
$resultPage->addBreadcrumb(__('Manage Hierarchy'), __('Manage Hierarchy'));
$resultPage->getLayout()->getBlock('breadcrumbs')->addCrumb(
'manage_hierarchy',
[
'label' => __($this->escapeHtml(__('Manage Hierarchy'))),
'title' => $this->escapeHtml(__('Manage Hierarchy')),
]
);
} else {
if ($this->_request->getParam('id')) {
$resultPage->addBreadcrumb(
__('Edit Page'),
__('Edit Page')
);
} else {
$resultPage->addBreadcrumb(
__('New Page'),
__('New Page')
);
}
}
return $resultPage;
}
}
In this example, we hide the ‘Manage Hierarchy’ item by removing it from the breadcrumbs and setting the active menu to a different section (Magento_Cms::cms_page).
Conclusion
Removing admin menu items for specific user roles is a powerful way to control access and maintain data security in Magento 2. Both event observation and layout processing provide effective solutions, but they must be implemented carefully to avoid breaking core functionality.
Remember that these are advanced techniques, so it’s important to thoroughly test your changes before deploying them to production environments.
For more advanced permissions management strategies, consider exploring the Magento Academy’s guide on Magento 2 Access Control. It provides in-depth insights into Magento’s permission system and best practices for managing user roles and access control.