How to Implement ACL in Magento 2 Extensions

How to Implement ACL in Magento 2 Extensions

Magento is by far the most robust and feature rich platform in the Ecommerce world. After the recent release of Magento version 2, developers are in a race to upgrade the extension in this latest version. At this stage, you really need to implement ACL into the extension to grant limited access for the view and use of functionalities, features. This role can be used the most when you setup a demo store for the extension. Without creating user roles, the demo store will show up whole admin panel with all the tabs those are unnecessary for the particular view and use of the extension. This arises the need of implementing ACL for your extension.

Magento version upgrade

This step to step guide will walk you through implementing ACL and adding new user roles in your Magento extension and allowing them the necessary access to features and functionality.

Step 1: First, we need to create “system.xml” file inside extension the below directory.

app\code\Vendor\Extension\etc\adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
    <system>
        <tab id="magecomp" translate="label" sortOrder="100" class="magecomp-logo">
            <label><![CDATA[ MageComp ]]></label>
        </tab>
        <section id="customextension" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1"
showInStore="1">
            <class>separator-top</class>
            <label>General Settings</label>
            <tab>magecomp</tab>
            <resource>Vendor_Extension::generalsetting</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
                <label>Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
                    <label>Enable</label>
                 <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Step 2: After that, we need to create “acl.xml” file inside extension the below directory.

app\code\Vendor\Extension\etc

<?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="Magento_Backend::stores">
                    <resource id="Magento_Backend::stores_settings">
                        <resource id="Magento_Config::config">
                            <resource id="Vendor_Extension::generalsetting" title="General Settings" sortOrder="50" />
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

Magento 2 Speed & Performance Optimization Services

Step 3: To assign role to a particular user.

System -> Permissions -> User Roles -> Add/Edit Role -> Roles Resources -> Resource Access -> Set Custom to see Stores -> Settings

Feel free to post the comments if you have any suggestion or problem with it.

hyva theme development

Previous Article

How to Redirect Category with Single Product to Product Page in Magento 2

Next Article

How to Set Up Cron Jobs in Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨