Home >Backend Development >PHP Tutorial >Implementing simple permission control through hooks in the CI framework, cihook_PHP tutorial

Implementing simple permission control through hooks in the CI framework, cihook_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:05903browse

In the CI framework, simple permission control is implemented through hooks, cihook

According to your actual situation, you need two files, one is the permission control class, Acl, and the other is the permission configuration file acl.php placed in the config directory.

The Acl class is placed in application/hook/acl.php. Enable hooks through the application/config/config.php file, and configure the hook.php file in the config directory.

1. Turn on the hook function, config.php file

Copy code The code is as follows:

/*
|------------------------------------------------- --------------------------
| 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. Configure the hook.php file

Copy code The code is as follows:

/*
| -------------------------------------------------- --------------------------
| 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'
);

For specific parameter descriptions, please refer to the link address of the document. Pay special attention to the value of post_controller_constructor here. You can choose different ones according to the situation.

3. Write the permission configuration file acl.php and place it in the config directory.

Copy code The code is as follows:

$config['AUTH'] = array(
SUPER_ADMIN => array(
         'admin' => array('index', 'logout'),
),
ADMIN => array(
         'admin' => array('index', 'logout'),
),
GUEST => array(
         'admin' => array('index', 'logout'),
),
);

This is just my definition based on my own situation. It is not real data. It is based on my own situation. There are also main variable names that need to be submitted to $config, so that they are easy to load and use.

4. Write specific permission control Acl class

Copy code The code is as follows:

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();

以上只是实现了简单的权限控制,小伙伴们可以根据自己的需求,自由扩展下吧。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/939406.htmlTechArticleCI框架中通过hook的方式实现简单的权限控制,cihook 根据自己的实际情况,需要两个文件,一个是权限控制类,Acl,另外一个是权限配置的文...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn