zoukankan      html  css  js  c++  java
  • CI中利用hook实现用户权限访问

    CI使用hook方法:

    1、启用hook,在config.php中设置:

    $config['enable_hooks'] = true;

    2、定义hook,在application/config/hooks.php中定义钩子

    $hook['pre_controller'] = array(
                                    'class'    => 'Acl',//挂钩类名
                                    'function' => 'filter',//过滤函数名
                                    'filename' => 'Acl.php',//挂钩类文件名
                                    'filepath' => 'hooks',//类文件所在文件夹application/hooks
                                    );

    3、接下来定义挂钩类,根据上面设置,在application/hooks文件夹中新建一个名为acl.php的文件,然后进行如下设计

    class Acl 
    {
        private $url_model;
        private $url_method;
        private $url_param;
        
        private $CI;
        
        function __construct(){
            $this->CI=&get_instance();
            $this->CI->load->library('session');
            
            $url=$_SERVER['PHP_SELF'];//得到网址index.php/......
            $arr=explode('/',$url);//CI是以'/'隔开网址各个部分
            
            //var_dump($arr);
            $arr = array_slice($arr, array_search('index.php', $arr) + 1, count($arr));
            //var_dump($arr);
            
            $this->url_model=empty($arr[0])?'user':$arr[0];//如果控制器没有 就设置为空
            $this->url_method=empty($arr[1])?'index':$arr[1];//如果没有方法 则为index方法
            $this->url_param=empty($arr[2])?'':$arr[2];//如果没定义 则表示没有参数        
        }
        function filter(){
            
            $user=$this->CI->session->userdata('username');
            
            if(!empty($user)){
                $role_name='user';//此处应该根据权限读取角色
            }else{
                $role_name='visitor';
            }
    
            $this->CI->config->load('acl');//此处是config用法 注意
            $acl=$this->CI->config->item('acl');
            
            $role=$acl[$role_name];
            
            $acl_info = $this->CI->config->item('acl_info');
            
            if(array_key_exists($this->url_model,$role)&&in_array($this->url_method, $role[$this->url_model])){
                ;
            }else{
                  //无权限,给出提示,跳转url
                $this->CI->session->set_flashdata('info', $acl_info[$role_name]['info']);
                $this->CI->session->set_flashdata('return_url',$acl_info[$role_name]['return_url']);
                redirect("user/error/");
            }
            
    
        }
    }

    4、由于第三步里面用到了配置文件,所以要在application/config中新建一个acl.php用来存放一些配置信息,代码如下

    <?php
    /*
        ACL相关配置
    */
    $config['acl']['visitor']=array(
                        
                        ''=>'index',
                        'user'=>array('index','error','check_login','logout','ceshi'),
                        'upload'=>array('do_upload'),
                        'easyui'=>array('panel','table'),
    );
    $config['acl']['user']=array(
                        ''=>'index',
                        'user'=>array('index','main','error','excel_to_mysql','get_progress','destory_progress','kaoshi'),
                        'upload'=>array('index','do_upload'),
                        
    );
    
    $config['acl_info']['visitor']=array(
                        'info'=>'登录之后才能使用',
                        'return_url'=>'user/index'
    );
    
    $config['acl_info']['user']=array(
                                        'info'=>'没有这个操作啦!!',
                                        'return_url'=>'user/main'
                                        
                                        );
  • 相关阅读:
    hdu2988:Dark roads(最小生成树)
    hdu1596:find the safest road(最短路)
    hdu1596:find the safest road(最短路)
    CultureInfo中重要的InvariantCulture
    c#通过反射获取类上的自定义特性
    分享我们项目中基于EF事务机制的架构 【转载】
    ASP.NET MVC3中的路由系统(Routes) .
    为ASP.NET MVC应用添加自定义路由
    Mvc生成页面之t4模板相关
    LINQ to SQL语句对应SQL的实现
  • 原文地址:https://www.cnblogs.com/huilange/p/2815485.html
Copyright © 2011-2022 走看看