0

Here I am trying create a role who can only see the product grid but not authorized to Edit or Add New product. But in User roles there only option to select full access to view and edit the products.

enter image description here

How to achieve it.

Can I get help? Thank you in advance.

2
  • That's sadly impossible to do with core Magento :( Do you want me to prepare custom functionality for you?
    – dudzio
    Commented Jun 1, 2021 at 17:56
  • Nevermind, I've posted the answer ;)
    – dudzio
    Commented Jun 1, 2021 at 18:28

1 Answer 1

1

Basically - you can't do that with core Magento. There is only one ACL resource for products management. More than that - even if my solution below works, I don't recommend it, because in my opinion it's too little added functionality with a cost of 2 class rewrites.

You can do that with code customization, but it needs either plugin creation for each action authorization check which can add some time in code execution to your admin panel globally - I won't do that in that way then.

The other way to do this is by actually rewriting two classes. Those classes are only used for that functionality, so it's not that bad in a practical state, but I don't think it is worth it to do that just for that little functionality. Nevertheless, let's dive into code:

Create etc/acl.xml to declare your new ACL resource:

<?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_Catalog::catalog">
                    <resource id="Magento_Catalog::catalog_inventory">
                        <resource id="Magento_Catalog::products_grid" title="Products Grid View" translate="title" sortOrder="11" />
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

Change menu item resource for Product Grid (etc/adminhtml/menu.xml):

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <update id="Magento_Catalog::catalog_products" resource="Magento_Catalog::products_grid"/>
    </menu>
</config>

Create etc/adminhtml/di.xml to declare class preferences:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Controller\Adminhtml\Product\Grid"
                type="Vendor\ProductViewAclResource\Controller\Adminhtml\Product\Grid"/>
    <preference for="Magento\Catalog\Controller\Adminhtml\Product\Index"
                type="Vendor\ProductViewAclResource\Controller\Adminhtml\Product\Index"/>
</config>

Vendor\ProductViewAclResource\Controller\Adminhtml\Product\Grid:

<?php

namespace Vendor\ProductViewAclResource\Controller\Adminhtml\Product;

class Grid extends \Magento\Catalog\Controller\Adminhtml\Product\Grid
{
    /**
     * Replaced core resource
     *
     * @see _isAllowed()
     */
    const ADMIN_RESOURCE = 'Magento_Catalog::products_grid';
}

Vendor\ProductViewAclResource\Controller\Adminhtml\Product\Index:

<?php

namespace Vendor\ProductViewAclResource\Controller\Adminhtml\Product;

class Index extends \Magento\Catalog\Controller\Adminhtml\Product\Index
{
    /**
     * Replaced core resource
     *
     * @see _isAllowed()
     */
    const ADMIN_RESOURCE = 'Magento_Catalog::products_grid';
}

1
  • Thank you so much for answer. Commented Jun 2, 2021 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.