|
最近尝试了一下在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. |
评分
-
查看全部评分
|