How to restrict update form for few users using ACL ?
1 Answer
Create empty M2 module and then:
Your etc/admin/di.xml
:
<?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\CustomerBalance\Block\Adminhtml\Customer\Edit\Tab\Customerbalance\Form" type="NameSpace\YourModule\Block\Adminhtml\Customer\Edit\Tab\Customerbalance\Form" />
</config>
And now, your class: NameSpace\YourModule\Block\Adminhtml\Customer\Edit\Tab\Customerbalance\Form.php
<?php
namespace NameSpace\YourModule\Block\Adminhtml\Customer\Edit\Tab\Customerbalance;
use Magento\Customer\Model\Customer;
use Magento\Framework\AuthorizationInterface;
class Form extends \Magento\CustomerBalance\Block\Adminhtml\Customer\Edit\Tab\Customerbalance\Form
{
protected $authorization;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Store\Model\System\Store $systemStore,
AuthorizationInterface $authorization,
array $data = []
) {
$this->authorization = $authorization;
parent::__construct($context, $registry, $formFactory, $customerFactory, $systemStore, $data);
}
protected function _prepareForm()
{
if (!$this->_isAllowed()) {
return $this;
}
return parent::_prepareForm();
}
/**
* Determines whether current user is allowed to access Action
*
* @return bool
*/
protected function _isAllowed()
{
return $this->authorization->isAllowed('NameSpace_YourModule::your_acl');
}
}
And, now add acl
:
<?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="NameSpace_YourModule::your_acl" sortOrder="5" title="YourAcl" />
</resource>
</resources>
</acl>
</config>
Now if you go to system > user roles
and then add new role, you should see your added acl
in there. The form will appear only if you tick this.
Haven't tested it, this should work.