CI框架中通過(guò)hook的方式實(shí)現(xiàn)簡(jiǎn)單的權(quán)限控制
根據(jù)自己的實(shí)際情況,需要兩個(gè)文件,一個(gè)是權(quán)限控制類,Acl,另外一個(gè)是權(quán)限配置的文件acl.php放在了config這個(gè)目錄下。
Acl這個(gè)類放在了application/hook/acl.php。通過(guò)application/config/config.php文件開(kāi)啟hook,并且配置config這個(gè)目錄下的hook.php文件。
1、開(kāi)啟hook功能,config.php這個(gè)文件
/*
|--------------------------------------------------------------------------
| 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這個(gè)文件
/*
| -------------------------------------------------------------------------
| 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' => 'auth',
'filename' => 'acl.php',
'filepath' => 'hooks'
);
具體的參數(shù)說(shuō)明可以參看文檔的鏈接地址,這里尤其要注意post_controller_constructor這個(gè)值,可以根據(jù)情況選擇不同的。
3、編寫(xiě)權(quán)限配置文件acl.php放在config目錄下。
$config['AUTH'] = array(
SUPER_ADMIN => array(
'admin' => array('index', 'logout'),
),
ADMIN => array(
'admin' => array('index', 'logout'),
),
GUEST => array(
'admin' => array('index', 'logout'),
),
);
這里只是我根據(jù)自己的情況定義的,不是真實(shí)數(shù)據(jù),根據(jù)自己的情況定。還有主要變量名字要交$config,這樣便于加載使用。
4、編寫(xiě)具體的權(quán)限控制Acl類
class Acl {
private $url_model;
private $url_method;
private $CI;
function Acl()
{
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->url_model = $this->CI->uri->segment(1);
$this->url_method = $this->CI->uri->segment(2);
}
function auth()
{
$user = $this->CI->session->userdata('USER');
if(empty($user))
$user->status = 0;
$this->CI->load->config('acl');
$AUTH = $this->CI->config->item('AUTH');
if(in_array($user->status, array_keys($AUTH))){
$controllers = $AUTH[$user->status];
if(in_array($this->url_model, array_keys($controllers))){
if(!in_array($this->url_method, $controllers[$this->url_model])){
show_error('您無(wú)權(quán)訪問(wèn)該功能,該錯(cuò)誤已經(jīng)被記錄!點(diǎn)擊<a href="'. site_url('admin/logout') .'">返回</a>');
}
}else{
show_error('您無(wú)權(quán)訪問(wèn)該模塊,該錯(cuò)誤已經(jīng)被記錄!點(diǎn)擊<a href="'. site_url('admin/logout') .'">返回</a>');
}
}
else
show_error('錯(cuò)誤的用戶類型,該錯(cuò)誤已經(jīng)被記錄!點(diǎn)擊<a href="'. site_url('admin/logout') .'">返回</a>');
}
}
整體上大體是這樣的形式,最后還是要根據(jù)自己的實(shí)際情況來(lái)確定。
需要注意的是:
$this->CI =& get_instance();
以上只是實(shí)現(xiàn)了簡(jiǎn)單的權(quán)限控制,小伙伴們可以根據(jù)自己的需求,自由擴(kuò)展下吧。
相關(guān)文章
PHP編程文件處理類SplFileObject和SplFileInfo用法實(shí)例分析
這篇文章主要介紹了PHP編程文件處理類SplFileObject和SplFileInfo用法,結(jié)合實(shí)例形式分析了文件處理類SplFileObject和SplFileInfo的功能、定義、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
PHP設(shè)計(jì)模式之調(diào)解者模式的深入解析
本篇文章是對(duì)PHP設(shè)計(jì)模式中的調(diào)解者模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP const定義常量及global定義全局常量實(shí)例解析
這篇文章主要介紹了PHP const定義常量及global定義全局常量實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
php基于登陸時(shí)間判斷實(shí)現(xiàn)一天多次登錄只積分一次功能示例
這篇文章主要介紹了php基于登陸時(shí)間判斷實(shí)現(xiàn)一天多次登錄只積分一次功能,適合會(huì)員系統(tǒng)的積分功能,涉及php時(shí)間判斷與數(shù)據(jù)庫(kù)相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
WordPress中用于獲取及自定義頭像圖片的PHP腳本詳解
這篇文章主要介紹了WordPress中用于獲取及自定義頭像圖片的PHP腳本編寫(xiě)方法,分別為get_avatar()和alt標(biāo)簽的使用,需要的朋友可以參考下2015-12-12

