A simple access control _php technique by hook in CI frame

Source: Internet
Author: User

According to own actual situation, needs two files, one is the permission control class, the ACL, another is the permission configuration file acl.php placed in the config this directory.

This class of ACLs is placed in the application/hook/acl.php. Open the hook through the application/config/config.php file and configure the hook.php file in the directory of CONFIG.

1, open the hook function, config.php this file

Copy Code code as follows:

/*
|--------------------------------------------------------------------------
| Enable/disable System Hooks
|--------------------------------------------------------------------------
|
| If you are would like to use the ' hooks ' feature your must enable it by
|  Setting this variable to TRUE (Boolean). The user guide for details.
|
*/
$config [' enable_hooks '] = TRUE;

2, configure hook.php this file

Copy Code code as follows:

/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets your define "hooks" to extend CI without hacking the core
|  Files. Please, 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 '
);

The specific parameter description can refer to the link address of the document, especially pay attention to post_controller_constructor this value, can choose different according to the situation.

3, write the right configuration file acl.php placed in the Config directory.

Copy Code code as follows:

$config [' AUTH '] = Array (
Super_admin => Array (
' Admin ' => Array (' index ', ' logout '),
),
ADMIN => Array (
' Admin ' => Array (' index ', ' logout '),
),
GUEST => Array (
' Admin ' => Array (' index ', ' logout '),
),
);

Here is only what I have defined according to my own situation, not the real data, according to my own situation. There are also major variable names to be $config, so that it is easy to load and use.

4, write specific permissions to control ACL classes

Copy Code code 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 access to this feature, the error has been recorded! Click <a href= "'. Site_url (' Admin/logout '). " > Return </a> ');
}
}else{
Show_error (' You do not have access to the module, the error has been recorded! Click <a href= "'. Site_url (' Admin/logout '). " > Return </a> ');
}
}
Else
Show_error (' Wrong user type, this error has been logged! Click <a href= "'. Site_url (' Admin/logout '). " > Return </a> ');
}
}

Overall is such a form, and finally, according to their own actual situation to determine.

It is to be noted that:

Copy Code code as follows:

$this->ci =& get_instance ();

The above only realizes the simple permission control, the small partner may according to own demand, the free expansion under.

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.