2013-12-13 76 views
0

使用CodeIgniter創建CMS並進行身份驗證我正在使用Ion Auth。CodeIgniter + Ion Auth:具有訪問級別的遞歸菜單項

這裏我試圖根據Ion AUth db組表中定義的訪問級別(用戶組)創建遞歸管理菜單。

我有3組syste_admin,ManagersHuman Resource我想設置對菜單的訪問權限,因此只有有權訪問的用戶才能看到這樣的菜單項。

數組定義菜單項

$menu = array(
    'dashboard' => array(
     'display' => 'Dashboard', 
     'url' => 'admin/dashboard', 
     'iconclass' => 'fa fa-home', 
     'attributes' => 'class="hellow-atts"', 
     'access' => array('system_admin','managers', 'hr') 
    ), 
    'employee' => array(
     'display' => 'Employee', 
     'url' => 'admin/hr',   
     'iconclass' => false, 
     'access' => array('system_admin','hr'), 
     'sub' => array(
      'manage' => array('display' => 'Manage All'), 
      'add_new' => array('display' => 'Add New'), 
      'access' => array('system_admin','hr'), 
     ), 
    ), 
    'links' => array(
     'display' => 'Recommended Links', 
     'iconclass' => 'fa fa-th', 
     'access' => array('system_admin'), 
    ),  
    'contact' => array(
     'display' => 'Contact Us', 
     'access' => array('system_admin'), 
    ), 
    'part' => array('display' => '', 'divider' => true, 'url' => '#'), 
); 


echo '<nav id="page-leftbar" role="navigation">'; 
echo admin_side_menu($menu); 
echo '</nav>'; 

菜單助手功能

if (! function_exists('admin_side_menu')) 
{ 
    function admin_side_menu($menu_array, $is_sub=FALSE, $attributes=FALSE, $iconclass=FALSE, $divider=FALSE) 
    { 
     $CI =& get_instance(); 

     $attr = (!$is_sub) ? ' id="sidebar" class="acc-menu"' : ' class="acc-menu"'; 
     $menu = "<ul$attr>"; // Open the menu container 

     /* 
     * I want to check here if user is in defined 
     * access group than s/he will be elligable to 
     * access the menu else it won't display 
     */   

     // this is to test if getting $access 
     // however finally I want to wrap this 
     // condition to the list item to make 
     // it works with the access levele 

     if($CI->ion_auth->in_group($access)) { 
      echo 'You have access'; 
     } else { 
      echo 'You don\'t have access'; 
     } //end if/else 

     foreach($menu_array as $id => $properties) { 

      echo '<pre>', print_r($properties), '</pre>'; 

      foreach($properties as $key => $val) { 

       if(is_array($val)) 
       { 
        $sub = admin_side_menu($val, TRUE); 
       } 

       else 
       { 
        $sub = NULL; 
        $$key = $val; 
       } 
      } 

      if(!isset($url)) { 
       $url = $id; 
      } 

      $lclass = ($divider) ? ' class="divider"' : ''; 
      $icon = (isset($iconclass)) ? '<i class="'.$iconclass.'"></i>' : ''; 
      $item = (!$divider) ? anchor($url, $icon.'<span>'.$display.'</span>', $attributes).$sub : ''; 
      $menu .= '<li'.$lclass.'>'.$item.'</li>'; 

      unset($url, $display, $sub); 

     } // end foreach 

     return $menu . "</ul>"; 
    } 
} 

隨着對代碼我收到類似錯誤Invalid argument supplied for foreachUndefined variable: displayUndefined variable: sub等。

這可能是因爲我不打電話access或不知道什麼是錯的。我不是那麼專業,第一次創建組權限的遞歸菜單。

任何人都可以告訴我如何使用和設置access數組,因此我可以在整個菜單中檢查它,包括子級別和根據所定義的訪問級別顯示。

回答

1

好了沒人幫,但在這裏我找到了答案..

功能

if (! function_exists('admin_side_menu')) 
{ 

    function admin_side_menu($menu_array) { 

     $CI =& get_instance(); 

     echo '<ul>'; // Open the menu container 

     //go through each top level menu item 
     foreach($menu_array as $item) { 

      $admin_group = $CI->config->item('admin_group', 'ion_auth');    

      $item['access'] = array_key_exists('access', $item) ? $item['access'] : $admin_group; 

      if($CI->ion_auth->in_group($item['access'])){ 

       echo '<li><a href="'.$item['link'].'"">'.$item['label'].'</a>'; 
       //see if this menu has children 
       if(array_key_exists('children', $item)) { 
        echo '<ul>'; 
        //echo the child menu 
        admin_side_menu($item['children']); 
        echo '</ul>'; 
       } 
       echo '</li>'; 

      }// end if 
     } // end foreach 

     echo '</ul>'; 
    } 

} 

鑑於建立

$menu = array(

    array(

     'label' => 'Dashboard', 
     'link' => 'admin/dashboard', 
     'access' => array('system_admin', 'hr', 'guest'), 
     'children' => array(
      array(
       'label' => 'Admin Can See', 
       'link' => 'http://google.com', 
       'access' => array('system_admin', 'hr', 'guest'), 
      ), 
      array(
       'label' => 'Guest Can See', 
       'link' => 'http://google.com', 
       'access' => array('system_admin', 'hr', 'guest'), 
      ) 
     ) 
    ),   
); 

echo '<nav id="page-leftbar" role="navigation">'; 
echo admin_side_menu($menu); 
echo '</nav>'; 

非常感謝...: ))