Magento Admin::删除特定角色/用户的菜单项


Magento Admin :: Remove Menu item for specific Roles/Users

我想从adminmenu中删除一个特定用户角色的菜单项。我见过其他人通过创建虚拟覆盖来实现,但这些都不是基于角色的。我希望在不使用任何.xml文件的情况下完成此操作。有没有办法做到这一点,例如;__construct()或prepareLayout?

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

我用自己的块扩展了Mage_Adminhtml_Block_Page_Menu。我复制了函数"_buildMenuArray()"在返回菜单数组之前,我检查当前登录的用户是否不是admin。如果是这样的话;我从菜单中删除了Hierarchy项目,并将Page项目设置为value最后,因此阴影显示正确。

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;
        }
    }
}

正确的方法是编辑角色的ACL权限。这是Magento管理中的一个功能,不需要自定义模块。

进入"System:Permissions:Roles"。然后选择要从中删除菜单项的角色。在Role Resources选项卡中,选择要在该角色的管理中显示的菜单项。点击保存并清除缓存,应该就可以了。