Simple permission control through hook in CI framework

Source: Internet
Author: User
This article describes how to implement simple permission control through hook in the CI framework.

This article describes how to implement simple permission control through hook in the CI framework.

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 is placed in the config directory.

The Acl class is stored in application/hook/acl. php. Use the application/config. php file to enable the hook and configure the hook. php file under the config directory.

1. Enable the hook function. The config. php file

The Code is as follows:


/*
| --------------------------------------------------------------------------
| Enable/Disable System Hooks
| --------------------------------------------------------------------------
|
| If you wowould like to use the 'hooks' feature you must enable it
| Setting this variable to TRUE (boolean). See the user guide for details.
|
*/
$ Config ['Enable _ hooks'] = TRUE;

2. Configure the hook. php file.

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:
|
|
|
*/
$ Hook ['Post _ controller_constructor '] = array (
'Class' => 'acl ',
'Function' => 'auth ',
'Filename' => 'acl. php ',
'Filepath' => 'hooks'
);

For specific parameter descriptions, see the link address of the document. Pay special attention to the value of post_controller_constructor. You can select different parameters as needed.

3. Write the permission Configuration File acl. php In the config directory.

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 what I have defined based on my own situation. It is not real data. It depends on my own situation. There are also main variable names to be handed in $ config, so that it is easy to load and use.

4. Write a specific Acl class for permission Control

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 ('You do not have permission to access this function. This error has been recorded! Click return ');
}
} Else {
Show_error ('You do not have permission to access this module. This error has been recorded! Click return ');
}
}
Else
Show_error ('user type of the error, which has been recorded! Click return ');
}
}

This is the overall form, which should be determined based on your actual situation.

Note that:

The Code is as follows:


$ This-> CI = & get_instance ();

The above is just a simple permission control. You can expand it as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.