CI框架中通過hook的方式實(shí)現(xiàn)簡單的權(quán)限控制
根據(jù)自己的實(shí)際情況,需要兩個文件,一個是權(quán)限控制類,Acl,另外一個是權(quán)限配置的文件acl.php放在了config這個目錄下。
Acl這個類放在了application/hook/acl.php。通過application/config/config.php文件開啟hook,并且配置config這個目錄下的hook.php文件。
1、開啟hook功能,config.php這個文件
/*
|--------------------------------------------------------------------------
| 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這個文件
/*
| -------------------------------------------------------------------------
| 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ù)說明可以參看文檔的鏈接地址,這里尤其要注意post_controller_constructor這個值,可以根據(jù)情況選擇不同的。
3、編寫權(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、編寫具體的權(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('您無權(quán)訪問該功能,該錯誤已經(jīng)被記錄!點(diǎn)擊<a href="'. site_url('admin/logout') .'">返回</a>');
}
}else{
show_error('您無權(quán)訪問該模塊,該錯誤已經(jīng)被記錄!點(diǎn)擊<a href="'. site_url('admin/logout') .'">返回</a>');
}
}
else
show_error('錯誤的用戶類型,該錯誤已經(jīng)被記錄!點(diǎn)擊<a href="'. site_url('admin/logout') .'">返回</a>');
}
}
整體上大體是這樣的形式,最后還是要根據(jù)自己的實(shí)際情況來確定。
需要注意的是:
$this->CI =& get_instance();
以上只是實(shí)現(xià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 const定義常量及global定義全局常量實(shí)例解析
這篇文章主要介紹了PHP const定義常量及global定義全局常量實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
php基于登陸時間判斷實(shí)現(xiàn)一天多次登錄只積分一次功能示例
這篇文章主要介紹了php基于登陸時間判斷實(shí)現(xiàn)一天多次登錄只積分一次功能,適合會員系統(tǒng)的積分功能,涉及php時間判斷與數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
WordPress中用于獲取及自定義頭像圖片的PHP腳本詳解
這篇文章主要介紹了WordPress中用于獲取及自定義頭像圖片的PHP腳本編寫方法,分別為get_avatar()和alt標(biāo)簽的使用,需要的朋友可以參考下2015-12-12

