没错,这是一篇转过来的文章,因为需要一个简单的权限控制方法,Google查询后找到了通过hook方式控制权限的做法.
Acl这个类放在了application/hook/acl.php。通过application/config/config.php文件开启hook,并且配置config这个目录下的hook.php文件。
1、开启hook功能,config.php这个文件
1
2
3
4
5
6
7
8
9
10
|
/*
|--------------------------------------------------------------------------
| Enable/Disable System Hooks
|--------------------------------------------------------------------------
|
| If you would like to use the 'hooks' feature you must enable it by
| setting this variable to TRUE (boolean). See the user guide for details.
|
*/
$config
[
'enable_hooks'
] = TRUE;
|
2、配置hook.php这个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files. Please see the user guide for info:
|
| http://codeigniter.com/user_guide/general/hooks.html
|
*/
$hook
[
'post_controller_constructor'
] =
array
(
'class'
=>
'Acl'
,
//控制类
'function'
=>
'filter'
,
//控制函数
'filename'
=>
'acl.php'
,
//控制文件
'filepath'
=>
'hooks'
//存放路径
);
|
3、编写权限配置文件acl.php放在config目录下。
1
2
3
|
//游客权限映射
$config[
'acl'
][
'visitor'
] = array(
''
=> array(
'index'
),
//首页
|
1
|
//这里表示如一个visitor用户浏览/balance/create这样的额链接时是有权限的,但是浏览/balance/update就没有权限。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//如果需要则应该修改为'balance'=>array('create','update')
'balance'
=>array(
'create'
)
);
//管理员
$config[
'acl'
][
'admin'
] = array(
);
//-------------配置权限不够的提示信息及跳转url------------------//
$config[
'acl_info'
][
'visitor'
] = array(
'info'
=>
'需要登录以继续'
,
'return_url'
=>
'user/login'
);
$config[
'acl_info'
][
'more_role'
] = array(
'info'
=>
'需要更高权限以继续'
,
'return_url'
=>
'user/up'
);
|
4、编写具体的权限控制Acl类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
class
Acl
{
private
$url_model
;
//所访问的模块,如:music
private
$url_method
;
//所访问的方法,如:create
private
$url_param
;
//url所带参数 可能是 1 也可能是 id=1&name=test
private
$CI
;
function
Acl()
{
$this
->CI = & get_instance();
$this
->CI->load->library(
'session'
);
$url
=
$_SERVER
[
'PHP_SELF'
];
$arr
=
explode
(
'/'
,
$url
);
$arr
=
array_slice
(
$arr
,
array_search
(
'index.php'
,
$arr
) + 1,
count
(
$arr
));
$this
->url_model = isset(
$arr
[0]) ?
$arr
[0] :
''
;
$this
->url_method = isset(
$arr
[1]) ?
$arr
[1] :
'index'
;
$this
->url_param = isset(
$arr
[2]) ?
$arr
[2] :
''
;
}
function
filter()
{
$user
=
$this
->CI->session->userdata(
'user'
);
if
(
empty
(
$user
)) {
//游客visitor
$role_name
=
'visitor'
;
}
else
{
$role_name
=
$user
->role;
}
$this
->CI->load->config(
'acl'
);
$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'
]);
redirect(
$acl_info
[
$role_name
][
'return_url'
]);
}
}
}
|