CI架構中通過hook的方式實現簡單的許可權控制,cihook_PHP教程

來源:互聯網
上載者:User

CI架構中通過hook的方式實現簡單的許可權控制,cihook


根據自己的實際情況,需要兩個檔案,一個是許可權控制類,Acl,另外一個是許可權配置的檔案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'
);

具體的參數說明可以參看文檔的連結地址,這裡尤其要注意post_controller_constructor這個值,可以根據情況選擇不同的。

3、編寫入權限設定檔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'),
),
);

這裡只是我根據自己的情況定義的,不是真實資料,根據自己的情況定。還有主要變數名字要交$config,這樣便於載入使用。

4、編寫具體的許可權控制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('您無權訪問該功能,該錯誤已經被記錄!點擊返回');
}
}else{
show_error('您無權訪問該模組,該錯誤已經被記錄!點擊返回');
}
}
else
show_error('錯誤的使用者類型,該錯誤已經被記錄!點擊返回');
}
}

整體上大體是這樣的形式,最後還是要根據自己的實際情況來確定。

需要注意的是:

複製代碼 代碼如下:
$this->CI =& get_instance();

以上只是實現了簡單的許可權控制,小夥伴們可以根據自己的需求,自由擴充下吧。

http://www.bkjia.com/PHPjc/939406.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/939406.htmlTechArticleCI架構中通過hook的方式實現簡單的許可權控制,cihook 根據自己的實際情況,需要兩個檔案,一個是許可權控制類,Acl,另外一個是許可權配置的文...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.