8

I have different user role for my website. For a particular user role, I want to remove "Delete" from mass action dropdown which displays on customer grid in admin section.

The Delete option is got rendered from

magento\vendor\magento\module-customer\view\adminhtml\ui_component\customer_listing.xml file.

I want to show this Delete option only for user role1 and want to hide for user role2.

How can we do this?

2 Answers 2

10

This can be done by creating a new class for MassActions:

<?php
namespace YourVendor\YourModule\Ui;

class MassAction extends \Magento\Ui\Component\MassAction
{
    private $authorization;

    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\AuthorizationInterface $authorization,
        $components,
        array $data
    ) {
        $this->authorization = $authorization;
        parent::__construct($context, $components, $data);
    }

    public function prepare()
    {
        parent::prepare();
        $config = $this->getConfiguration();
        if (!$this->authorization->isAllowed('YourVendor_YourModule::the_acl_youd_like_to_use')) {
            $allowedActions = [];
            foreach ($config['actions'] as $action) {
                if ('delete' != $action['type']) {
                    $allowedActions[] = $action;
                }
            }
            $config['actions'] = $allowedActions;
        }
        $this->setData('config', (array)$config);
    }
}

Add setting it to customer listing with help of app/code/YourVendor/YourModule/view/adminhtml/ui_component/customer_listing.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <listingToolbar name="listing_top">
        <massaction name="listing_massaction" class="YourVendor\YourModule\Ui\MassAction"/>
    </listingToolbar>
</listing>
2
  • It's worked for me Commented Sep 4, 2017 at 10:13
  • Works in CE 2.3.1
    – Garry
    Commented Jun 16, 2019 at 11:30
1

You can do using the plugin (Best Approach):

app/code/Devcrew/DeleteRestriction/etc/adminhtml/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Ui\Component\Product\MassAction">
        <plugin name="hide_delete_from_catalog_massaction" type="Devcrew\DeleteRestriction\Plugin\Catalog\Ui\Component\Product\MassAction" sortOrder="1"/>
    </type>
</config>

app/code/Devcrew/DeleteRestriction/Plugin/Catalog/Ui/Component/Product/MassAction.php

<?php
namespace Devcrew\DeleteRestriction\Plugin\Catalog\Ui\Component\Product;
class MassAction
{
    public function __construct(  \Magento\Backend\Model\Auth\Session $adminSession)
    {
        $this->_adminSession = $adminSession;
    }
    public function afterIsActionAllowed(

        \Magento\Catalog\Ui\Component\Product\MassAction $subject,
        $isAllowed,
        $actionType
    ) {
        $roleData = $this->_adminSession->getUser()->getRole()->getData();

         //Remove Delete Button for Admin users except Administrators
        if ($actionType == 'delete' && trim($roleData['role_name'])!=='Administrators') {
            return false;
        }

        return $isAllowed;
    }
}

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.