【问题标题】:Magento Admin :: Remove Menu item for specific Roles/UsersMagento Admin :: 删除特定角色/用户的菜单项
【发布时间】:2015-07-15 09:09:57
【问题描述】:

我想从特定用户角色的管理菜单中删除一个菜单项。我见过其他人通过创建一个虚拟覆盖来做到这一点,但这些不是基于角色。我想在不使用 .xml 文件之一的情况下执行此操作。 例如,有没有办法做到这一点; __construct() 还是 prepareLayout?

编辑: 我必须补充一点,我要禁用的部分是 CMS 中的 Manage Hierarchy 项。 我知道我可以禁用用户角色的层次结构,但我需要它来保存 CMS 页面。

【问题讨论】:

  • 好的,所以我自己解决了这个问题。我在本地 Xxxxx_Xxxx_Block_Adminhtml_Page_Menu 中扩展了 Mage_Adminhtml_Block_Page_Menu
  • +1 分享解决方案 :)
  • 请尽快提交您的解决方案作为答案。
  • 我在提交 8 小时后才能做到这一点。所以我刚才做了。明天我可以将其标记为已解决。

标签: php magento


【解决方案1】:

我用自己的块扩展了 Mage_Adminhtml_Block_Page_Menu。 我复制了函数“_buildMenuArray()” 就在我返回菜单数组之前,我检查当前登录用户是否不是管理员。如果是这样;我从菜单中删除了 Hierarchy 项,并将 Page 项设置为最后一个值,以便正确显示阴影。

class Xxxxx_Xxxx_Block_Adminhtml_Page_Menu extends Mage_Adminhtml_Block_Page_Menu
{
    protected function _buildMenuArray(Varien_Simplexml_Element $parent=null, $path='', $level=0)
    {
        if (is_null($parent)) {
            $parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');
        }

        $parentArr = array();
        $sortOrder = 0;
        foreach ($parent->children() as $childName => $child) {
            if (1 == $child->disabled) {
                continue;
            }

            $aclResource = 'admin/' . ($child->resource ? (string)$child->resource : $path . $childName);
            if (!$this->_checkAcl($aclResource)) {
                continue;
            }

            if ($child->depends && !$this->_checkDepends($child->depends)) {
                continue;
            }

            $menuArr = array();

            $menuArr['label'] = $this->_getHelperValue($child);

            $menuArr['sort_order'] = $child->sort_order ? (int)$child->sort_order : $sortOrder;

            if ($child->action) {
                $menuArr['url'] = $this->_url->getUrl((string)$child->action, array('_cache_secret_key' => true));
            } else {
                $menuArr['url'] = '#';
                $menuArr['click'] = 'return false';
            }

            $menuArr['active'] = ($this->getActive()==$path.$childName)
                || (strpos($this->getActive(), $path.$childName.'/')===0);

            $menuArr['level'] = $level;

            if ($child->children) {
                $menuArr['children'] = $this->_buildMenuArray($child->children, $path.$childName.'/', $level+1);
            }
            $parentArr[$childName] = $menuArr;

            $sortOrder++;
        }

        uasort($parentArr, array($this, '_sortMenu'));

        while (list($key, $value) = each($parentArr)) {
            $last = $key;
        }
        if (isset($last)) {
            $parentArr[$last]['last'] = true;
        }

        $data = $this->_isAdmin($parentArr);

        return $data;
    }

    protected function _isAdmin($data){
        $userRole = Mage::getSingleton('admin/session')->getUser()->getRole();
        $roleName = $userRole->getRoleName();
        $roleId = $userRole->getRoleId();
        if ($roleName == 'Administrators' || $roleId == 1) {
            return $data;
        } else {
            if (isset($data['hierarchy'])){
                unset($data['hierarchy']);
                $data['page']['last'] = 1;  
            }
            if (isset($data['enterprise_page']['children']['hierarchy'])){
                unset($data['enterprise_page']['children']['hierarchy']);
                $data['enterprise_page']['children']['last'] = 1;
            }
            return $data;
        }
    }
}

【讨论】:

    【解决方案2】:

    执行此操作的正确方法是编辑角色的 ACL 权限。这是 Magento 管理员中的一项功能,不需要自定义模块。

    您转到系统:权限:角色。然后选择要从中删除菜单项的角色。在角色资源选项卡中,您选择要在该角色的管理员中显示的菜单项。单击保存并清除缓存,您应该会很好。

    【讨论】:

    • 正如我在描述中所说的。必须激活角色“层次结构”才能保存 CMS 页面。如果我将其关闭,则 CMS 页面会在没有层次结构信息的情况下保存。当您在角色菜单中激活角色(层次结构)时。该角色(层次结构)的菜单项会自动添加到菜单栏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2019-05-05
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    相关资源
    最近更新 更多