【问题标题】:CodeIgniter + Ion Auth: Recursive menu item with access levelCodeIgniter + Ion Auth:具有访问级别的递归菜单项
【发布时间】:2014-01-01 02:44:31
【问题描述】:

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

我正在尝试根据 Ion AUth db 组表中定义的访问级别(用户组)创建递归管理菜单。

我有 3 个组 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 的数组,以便我可以在整个菜单中检查它,包括子级别并根据定义的访问级别显示。

【问题讨论】:

    标签: php codeigniter recursion


    【解决方案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>';
    

    非常感谢... :))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-23
      • 2013-02-11
      • 1970-01-01
      • 2018-08-28
      • 2014-03-08
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多