开发者

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

开发者 https://www.devze.com 2023-04-12 12:06 出处:网络
I want to remove 开发者_JAVA技巧a menu item from the adminmenu for a specific user role. I\'ve seen others do it by creating a dummy override but these are not based on roles. I want to do this withou

I want to remove 开发者_JAVA技巧a menu item from the adminmenu for a specific user role. I've seen others do it by creating a dummy override but these are not based on roles. I want to do this without using one of the .xml files. Is there a way to do this in, for example; the __construct() or prepareLayout?

EDIT: I must add to this that the part I want to disable is the Manage Hierarchy item in CMS. I know I can just disable the Hierarchy for the userrole but I need it to save CMS pages.


I extendend the Mage_Adminhtml_Block_Page_Menu with my own block. I copied the function "_buildMenuArray()" And just before I return the menu array I check if the current loggen in user is not the admin. If so; I remove the Hierarchy item from the menu and set the Page item with value last so the drop-shadow is displayed properly.

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


The correct way to do this is to edit ACL permissions for the role. This is a feature in the Magento admin and there's no need for a custom module.

You go to the System:Permissions:Roles. Then you select the role that you want to remove the menu item from. In the Role Resources tabs, you select the menu items you want to show in that role's admin. Click save and clear your cache and you should be good.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号