2011-10-10 100 views
7

我想從特定用戶角色的管理員菜單中刪除菜單項。我見過其他人通過創建虛擬覆蓋來實現,但這些並不是基於角色。我想在不使用任何.xml文件的情況下執行此操作。例如,有沒有辦法做到這一點? __construct()或prepareLayout?Magento Admin ::刪除特定角色/用戶的菜單項

編輯: 我必須補充說,我想禁用的部分是CMS中的管理層次結構項目。 我知道我只能禁用userrole的層次結構,但我需要它來保存CMS頁面。

+0

好的,所以我設法解決了這個問題。 我在本地Xxxxx_Xxxx_Block_Adminhtml_Page_Menu – Chris

+0

+1中擴展了Mage_Adminhtml_Block_Page_Menu,以便共享解決方案:) – JNDPNT

+0

請儘快提交您的解決方案作爲答案。 – cwallenpoole

回答

1

我用我自己的塊擴展Mage_Adminhtml_Block_Page_Menu。 我複製了函數「_buildMenuArray()」 而在我返回菜單數組之前,我檢查用戶當前的loggen是否不是admin。如果是這樣;我從菜單中刪除Hierarchy項目,並最後爲值設置頁面項目,以便正確顯示陰影。

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

正確的方法是編輯角色的ACL權限。這是Magento管理員的一項功能,不需要自定義模塊。

你去系統:權限:角色。然後,您選擇要從中刪除菜單項的角色。在「角色資源」選項卡中,選擇要在該角色的管理員中顯示的菜單項。點擊保存並清除緩存,你應該很好。

+0

正如我在描述中所說的。必須激活「層次」角色才能保存CMS頁面。如果我關閉它,CMS頁面會保存而沒有層次結構信息。並且在角色菜單中激活角色(層級)時。該角色(層級)的菜單項自動添加到菜單欄中。 – Chris