CodeIgniter 中英文搜索引擎:
返回列表 发帖

[核心代码 Core] 在CI 中实现ACL

最近尝试了一下在CI中实现ACL ,只是说明思想,思维不一定太严谨,供参考
(1) system/application/hooks 中新增文件acl.php
PHP
<?php
//stone.pei added. acl hook.
function hook_acl()
{
        global $RTR;
        $controller = $RTR->class;
        $method = $RTR->method;
        //load acl config files
        $config = & load_class('Config');
        $config->load('acl',true,true);
        $acl_settings = $config->item('acl');
        $acl_tables = $acl_settings['acl'];
        //get current user level  eg : $_COOKIE['user_role'] = 'admin'
        $current_role = (isset($_COOKIE['user_role']))? $_COOKIE['user_role'] : 'guest' ;
        if(isset($acl_tables[$controller][$method])){
                //begin to check acl
                $allow_roles = $acl_tables[$controller][$method];
                if(!in_array($current_role,$allow_roles)){
                        show_error('No Right To Access',500);
                }
        }
       
       
}
?>
复制代码

(2) 在system/application/config/下添加文件 acl.php 用作配置文件
PHP
<?php
 /**
  * stone.pei added
  * used to config acl
  */

  //acl table
  //format
  /*$acl = array(
        'controll_1' =>array(
                'method_1' => array('super admin','admin','register'), //who can access this method.
                'method_2' => array('admin'),
    ),
    'controll_2' =>array(
                'method_1' => array('super admin','admin','register'), //who can access this method.
                'method_2' => array('admin','guest'), // no definition: no access limit.
    ),
  );*/

  $acl = array(
        'blog' => array(
                'add' => array('admin','register'),
                'html_all' => array('admin'),
                'remove_all' => array('admin'),
        ),
       
  );
 
  //set config
  $config['acl'] = $acl;
?>
复制代码

(3) system/application/config/hooks.php中添加
PHP
$hook['pre_controller'] = array(
                'function' => 'hook_acl',
                'filename'=> 'acl.php',
                'filepath' => 'hooks',
                'params' => array(),
);
复制代码


说明:此方法方便,低耦合,无需要修改controller 及method.
可以随时启用ACL 或变更ACL.
1

评分人数

  • Hex

加分!最近难得一见的好帖~
QQ: 49489680
MSN: zhaochang_tj AT hotmail DOT com
搜索: http://search.codeigniter.org.cn

TOP

确实是好贴,很不错

TOP

很好,我也想这样的。谢谢
另外可以把aclconfig存进数据库,这样以后再admin中可以动态的改变授权了。
CI 我喜欢

TOP

多谢分享
php爱好者 http://www.ihacklog.com

TOP

这个果然很强大。
传说的AOP

TOP

我的问题是,使用$_COOKIE能取到值吗?然后ci好象会加密cookis的内容。这个比较可以成功吗?
有知道的人说一下。
谢谢

TOP

返回列表