【发布时间】:2014-01-01 02:44:31
【问题描述】:
使用 CodeIgniter 创建 CMS 并使用 Ion Auth 进行身份验证。
我正在尝试根据 Ion AUth db 组表中定义的访问级别(用户组)创建递归管理菜单。
我有 3 个组 syste_admin、Managers 和 Human 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 foreach、Undefined variable: display、Undefined variable: sub 等错误。
这可能是因为我没有正确调用access 或者不知道出了什么问题。我不是那么专家,第一次创建具有组权限的递归菜单。
谁能告诉我如何使用和设置access 的数组,以便我可以在整个菜单中检查它,包括子级别并根据定义的访问级别显示。
【问题讨论】:
标签: php codeigniter recursion