Home Backend Development PHP Tutorial CI框架中通过hook的方式实现简单的权限控制_PHP

CI框架中通过hook的方式实现简单的权限控制_PHP

May 31, 2016 pm 01:18 PM
ci framework Permission control

根据自己的实际情况,需要两个文件,一个是权限控制类,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();

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

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement permission control and user management in uniapp How to implement permission control and user management in uniapp Oct 20, 2023 am 11:15 AM

How to implement permission control and user management in uniapp With the development of mobile applications, permission control and user management have become an important part of application development. In uniapp, we can use some practical methods to implement these two functions and improve the security and user experience of the application. This article will introduce how to implement permission control and user management in uniapp, and provide some specific code examples for reference. 1. Permission Control Permission control refers to setting different operating permissions for different users or user groups in an application to protect the application.

Implementing user permissions and access control using PHP and SQLite Implementing user permissions and access control using PHP and SQLite Jul 29, 2023 pm 02:33 PM

Implementing user permissions and access control using PHP and SQLite In modern web applications, user permissions and access control are a very important part. With proper permissions management, you can ensure that only authorized users can access specific pages and functions. In this article, we will learn how to implement basic user permissions and access control using PHP and SQLite. First, we need to create a SQLite database to store information about users and their permissions. The following is the structure of a simple user table and permission table

How to use CI framework in php? How to use CI framework in php? Jun 01, 2023 am 08:48 AM

With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

User management and permission control in Laravel: implementing multiple users and role assignments User management and permission control in Laravel: implementing multiple users and role assignments Aug 12, 2023 pm 02:57 PM

User management and permission control in Laravel: Implementing multi-user and role assignment Introduction: In modern web applications, user management and permission control are one of the very important functions. Laravel, as a popular PHP framework, provides powerful and flexible tools to implement permission control for multiple users and role assignments. This article will introduce how to implement user management and permission control functions in Laravel, and provide relevant code examples. 1. Installation and configuration First, implement user management in Laravel

Best Practices for Laravel Permissions Features: How to Correctly Control User Permissions Best Practices for Laravel Permissions Features: How to Correctly Control User Permissions Nov 02, 2023 pm 12:32 PM

Best practices for Laravel permission functions: How to correctly control user permissions requires specific code examples Introduction: Laravel is a very powerful and popular PHP framework that provides many functions and tools to help us develop efficient and secure web applications. One important feature is permission control, which restricts user access to different parts of the application based on their roles and permissions. Proper permission control is a key component of any web application to protect sensitive data and functionality from unauthorized access

How to implement user login and permission control in PHP? How to implement user login and permission control in PHP? Jun 29, 2023 pm 02:28 PM

How to implement user login and permission control in PHP? When developing web applications, user login and permission control are one of the very important functions. Through user login, we can authenticate the user and perform a series of operational controls based on the user's permissions. This article will introduce how to use PHP to implement user login and permission control functions. 1. User login function Implementing the user login function is the first step in user verification. Only users who have passed the verification can perform further operations. The following is a basic user login implementation process: Create

How to use CI framework in PHP How to use CI framework in PHP Jun 27, 2023 pm 04:51 PM

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

See all articles