是否可以基于字段限制管理员用户?例如,我希望一些用户编辑产品的库存和简短描述,但我不希望他们能够更改产品名称和 sku 等字段

有帮助吗?

解决方案

我没有一个功能齐全的方法来做到这一点,但我可能有一个想法。
如果你看一下 Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Attributes 类,在产品添加/编辑页面上呈现属性选项卡的类,您将在 _prepareForm 方法

       if (Mage::registry('product')->hasLockedAttributes()) {
            foreach (Mage::registry('product')->getLockedAttributes() as $attribute) {
                $element = $form->getElement($attribute);
                if ($element) {
                    $element->setReadonly(true, true);
                }
            }
        }

这意味着如果该方法 getLockedAttributes 从产品模型中,返回一个带有属性代码的数组,它们在表单中将是只读的。这为您提供了客户端验证。

相同 getLockedAttributes 用于检查是否允许您为产品和类别的某些属性设置值 Mage_Catalog_Model_Abstract::setData. 。这为您提供了服务器端验证。

这意味着您可以在产品模型的加载方法上创建观察者 catalog_product_load_after 你在哪里检索(从某个地方......我还不知道从哪里)受限属性代码列表将它们放入一个名为的数组中 $locked 然后将它们设置在产品型号上。
像这样的东西:

public function catalogProductLoadAfter($observer) 
{
    $locked = Magic happens here and you get from somewhere the list of attributes that the current user is not allowed to modify.
    $product = $observer->getProduct();
    foreach ($locked as $code) {
        $product->lockAttribute($code);
    }
    return $this;
}

现在是“魔法”部分。
我的想法是有一个单独的管理部分,对于每个管理员,您可以指定某个属性是否可以由该特定管理员编辑。
然后为当前管理员检索允许的属性列表。

许可以下: CC-BY-SA归因
scroll top