CodeIgniter+Ion Auth:具有访问级别的递归菜单项


CodeIgniter + Ion Auth: Recursive menu item with access level

使用CodeIgniter创建CMS并使用Ion Auth进行身份验证。

在这里,我试图根据Ion AUth数据库组表中定义的访问级别(用户组)创建递归管理菜单。

我有三个组syste_adminManagersHuman 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的数组吗?这样我就可以在整个菜单中检查它,包括子级别,并根据定义的访问级别进行显示。

好吧,这里没有人帮忙,但我找到了答案。。

功能

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

非常感谢…:))