Using ACL(Access Control List) in Magento2
ACL is used to add our resource under the resource tree so that we can allow or deny access to the particular users to that resource using ACL rules.
ACL resource tree is visible under System > Permission > User Roles area in the admin section.
To add our custom resource under this resource tree we have to create acl.xml inside etc/ folder of our module.
etc/acl.xml :-
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Custom_Module::menu" title="Custom Menu" sortOrder="10" >
</resource>
</resource>
</resources>
</acl>
</config>
Here resource id is a unique identifier which will be added to the menu's resource attribute and controller to restrict and allow access according to the ACL roles. And the format for the id attribute is:- vendor_module::resorcename
Title is the name of the resource.
Sortorder decides the position of the resource in the resource tree. The lower the number the position will be upper.
Using acl.xml we have added our resource now we can see our resource under user roles after clicking Add New Role inside the roles resources tab.
Now we have only one user and by default Magento has only one role as Administrator with all the permissions. In order to restrict the user we have to create a new admin user and a new role.
Role Name will be the name of the role.
Current user verification :- We need to mention the password of our current user in order to save the rule.
Then under the role resources tab we can select the resource for which i want to give access to my user as here i am giving the access of only custom menu to this role.
After creating the role i will assign this role to any user so that particular user will have access to the custom menu only.
This MagentoUser will have access to only custom menu.
As we can see step by step that we have to add our resource in the resource tree in order to allow or restrict, then we will create a role in which we can decide the resource and then we can assign that role to a particular user with the limited access using ACL.
In the next blog I will describe how to use ACL to restrict users, menu and controllers.
Comments
Post a Comment