Adding Menu Items to Role Resources in Magento
Hello developers! Today, we’re going to dive into a common challenge faced by Magento administrators: how to link custom admin menu items to specific roles or user groups. By the end of this guide, you’ll have a clear understanding of how to assign your newly created menu items to particular roles and manage access control effectively.
Understanding the Current Setup
You’ve already done a great job creating a new menu item in Magento by modifying the config.xml file located at app/etc/config.xml. This allows you to add a new menu titled “Inventory” with a sub-action called “Set It!”. The code snippet looks like this:
<adminhtml>
<menu>
<example translate="title" module="adminhtml">
<title>Inventory</title>
<sort_order>110</sort_order>
<children>
<set_time>
<title>Set It!</title>
<action>helloworld/index/goodbye</action>
</set_time>
</children>
</example>
</menu>
</adminhtml>
This addition successfully creates a new menu with the desired hierarchy and links it to a specific action. However, you’re currently unable to assign this menu to a particular user role or restrict its access.
The Challenge: Assigning Menu Items to Roles
The main hurdle is that Magento’s default setup doesn’t automatically link custom menus to roles. By default, all administrative users have access to every menu item in the backend. This poses security risks and isn’t an efficient way to manage user permissions.
To solve this issue, we need to create a new “resource” in Magento and then assign it to our custom menu and specific roles.
Step 1: Create a New Resource
Open your config.xml file again and add a new <resources> section under the <adminhtml> tag. Here’s an example of creating a resource called “inventory_management”:
<adminhtml>
<menu>
<!-- Other menu items -->
</menu>
<acl>
<resources>
<all>
<children>
<catalog translate="title" module="catalog">
<title>Catalog</title>
<sort_order>10</sort_order>
<children>
<products translate="title" module="catalog">
<title>Products</title>
<sort_order>10</sort_order>
<children>
<!-- Other product-related resources -->
</children>
</products>
</children>
</catalog>
</children>
</all>
<admin>
<children>
<inventory_management translate="title" module="inventory">
<title>Inventory Management</title>
<sort_order>20</sort_order>
<children>
<set_time translate="title">
<title>Set It!</title>
</set_time>
</children>
</inventory_management>
</children>
</admin>
</resources>
</acl>
</adminhtml>
In this example, we’ve created a new “translation” for our menu item called “Inventory Management”. We then mapped it to the action helloworld/index/goodbye inside the <set_time> element. The <sort_order> attribute determines the position of the menu item in the navigation.
Step 2: Assign the Resource to a Specific Role
After creating the resource, you need to assign it to one or more roles. Here’s how:
- Log into your Magento admin panel.
- Navigate to “System” > “Permissions” > “Roles”.
- Click on an existing role (or create a new one).
- Under the “Resources” tab, find and select the newly created “Inventory Management” resource.
- Save the changes.
This action will grant access to your custom menu only for users assigned to this specific role.
Step 3: Test the Setup
To verify that the assignment is working correctly:
- Log out of the admin panel.
- Create a new user and assign them the role you created in step 2.
- Log in as the newly created user.
You should now see your custom “Inventory” menu item available only for this particular user, demonstrating that the resource assignment was successful.
Conclusion
In this guide, we explored how to link Magento admin menu items to specific roles and manage access control more effectively. By following these steps:
- We modified the
config.xmlfile to create a new “translation” for our custom menu item. - We added a new resource in Magento’s ACL configuration.
- We assigned this resource to one or more user roles.
This approach allows you to restrict access to specific menu items based on user roles, enhancing security and simplifying permission management in your Magento backend.