PHP之运用CI用钩子实现URL权限控制————————【Badboy】
<span style="background-color: rgb(247, 252, 255); font-family: Verdana, Arial, Helvetica, sans-serif; "></span><p><span style="font-family: Verdana, Arial, Helvetica,
sans-serif;
font-size: 14px;
background-color: rgb(247, 252, 255);
">CI 的钩子功能使得您能够在不改动系统核心文件的基础上来改变或添加系统的核心执行功能。</span></p><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px; background-color: rgb(247, 252, 255); ">
比如,您能够在控制器刚刚加载前或刚刚加载后来执行特定的脚本,或者在其它时刻来触发您的脚本。
</span></p><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
background-color: rgb(247, 252, 255);
">看代码:</span></p><pre name="code" class="php"><span style="background-color: rgb(247, 252, 255); font-family: Verdana, Arial, Helvetica, sans-serif; ">
</span>
system/application/config/hooks.php中加入钩子声明:
[php]
$hook['post_controller_constructor'] = array(
'class' => 'Acl',
'function' => 'filter',
'filename' => 'acl.php',
'filepath' => 'hooks',
);
system/application/config/config.php中让钩子系统生效
$config['enable_hooks'] = TRUE;
然后在中新建acl.php权限系统配置文件,当然你也能够放在数据库中。
//游客权限映射
$config['acl']['visitor'] = array(
'' => array('index'),//首页 www.2cto.com
'music' => array('index', 'list'),
'user' => array('index', 'login', 'register')
);
//管理员
$config['acl']['admin'] = array(
);
//-------------配置权限不够的提示信息及跳转url------------------//
$config['acl_info']['visitor'] = array(
'info' => '须要登录以继续',
'return_url' => 'user/login'
);
$config['acl_info']['more_role'] = array(
'info' => '须要更高权限以继续',
'return_url' => 'user/up'
);
/* End of file acl.php */
/* Location: ./application/config/acl.php */
system/application/hooks文件夹下加入acl.php逻辑处理文件
class Acl
{
private $url_model;//所訪问的模块,如:music
private $url_method;//所訪问的方法,如:create
private $url_param;//url所带參数 可能是 1 也可能是 id=1&name=test
private $CI;
function Acl()
{
$this->CI = & get_instance();
$this->CI->load->library('session');
$url = $_SERVER['PHP_SELF'];
$arr = explode('/', $url);
$arr = array_slice($arr, array_search('index.php', $arr) + 1, count($arr));
$this->url_model = isset($arr[0]) ? $arr[0] : '';
$this->url_method = isset($arr[1]) ? $arr[1] : 'index';
$this->url_param = isset($arr[2]) ? $arr[2] : '';
}
function filter()
{
$user = $this->CI->session->userdata('user');
if (emptyempty($user)) {//游客visitor
$role_name = 'visitor';
} else {
$role_name = $user->role;
}
$this->CI->load->config('acl');
$acl = $this->CI->config->item('acl');
$role = $acl[$role_name];
$acl_info = $this->CI->config->item('acl_info');
if (array_key_exists($this->url_model, $role) && in_array($this->url_method, $role[$this->url_model])) {
;
} else {//无权限,给出提示,跳转url
$this->CI->session->set_flashdata('info', $acl_info[$role_name]['info']);
redirect($acl_info[$role_name]['return_url']);
}
}
}
PHP之运用CI用钩子实现URL权限控制————————【Badboy】的更多相关文章
- 第十九章 动态URL权限控制——《跟我学Shiro》
目录贴:跟我学Shiro目录贴 用过Spring Security的朋友应该比较熟悉对URL进行全局的权限控制,即访问URL时进行权限匹配:如果没有权限直接跳到相应的错误页面.Shiro也支持类似的机 ...
- Spring Security 动态url权限控制(三)
一.前言 本篇文章将讲述Spring Security 动态分配url权限,未登录权限控制,登录过后根据登录用户角色授予访问url权限 基本环境 spring-boot 2.1.8 mybatis-p ...
- Shiro学习(19)动态URL权限限制
用过spring Security的朋友应该比较熟悉对URL进行全局的权限控制,即访问URL时进行权限匹配:如果没有权限直接跳到相应的错误页面.Shiro也支持类似的机制,不过需要稍微改造下来满足实际 ...
- Spring Security-利用URL地址进行权限控制
目的是:系统内存在很多不同的用户,每个用户具有不同的资源访问权限,具体表现就是某个用户对于某个URL是无权限访问的.需要Spring Security忙我们过滤. 参考:http://www.cnbl ...
- CI框架 .htaccess 隐藏url在index.php解决方案
CodeIgniter(下面简称"CI")是一款国外优秀的PHP轻量级MVC框架,它支持PHP4和PHP5.是开发中小型可拓展性需求高的Web应用程序的利器.眼下你所见到的这个博客 ...
- 基于 URL 的权限控制
先不用框架,自己实现一下 数据库 /* SQLyog v10.2 MySQL - 5.1.72-community : Database - shiro *********************** ...
- 在ASP.NET MVC中实现基于URL的权限控制
本示例演示了在ASP.NET MVC中进行基于URL的权限控制,由于是基于URL进行控制的,所以只能精确到页.这种权限控制的优点是可以在已有的项目上改动极少的代码来增加权限控制功能,和项目本身的耦合度 ...
- springboot整合security实现基于url的权限控制
权限控制基本上是任何一个web项目都要有的,为此spring为我们提供security模块来实现权限控制,网上找了很多资料,但是提供的demo代码都不能完全满足我的需求,因此自己整理了一版. 在上代码 ...
- 基于shiro+jwt的真正rest url权限管理,前后端分离
代码地址如下:http://www.demodashi.com/demo/13277.html bootshiro & usthe bootshiro是基于springboot+shiro+j ...
随机推荐
- UVA 5875 DP
题意:给你一堆二维点,每个点有一些分数. 现在要从点(0 , 0 )出发,只能从标号小的点走到大的点,每个人有一个走的距离的限制,问最后能拿到的最高的分数,当然这个人从(0 , 0)出发还得回到( 0 ...
- Note for video Machine Learning and Data Mining——Linear Model
Here is the note for lecture three. the linear model Linear model is a basic and important model in ...
- Linux for周期运行命令注意事项
假定for有一些符号循环指令,需要使用()封闭. for i in {1..4}; do (python /data/UGCRobot/manage/Scheduler.py 1.log > / ...
- Hadoop云计算大数据书籍分享
1. 推荐书名 大数据云计算利器: Hadoop, The Definitive Guide, 1Ed.pdf(第1版) 大数据云计算利器: Hadoop, The Definitive ...
- SE 2014年4月2日
一 描述OSPF协议 LSA(Type 1~5)的名称,始发者以及特点 第一类LSA (router lsa)该类lSA为启动了ospf进程的所有路由器都可以产生,该类LSA主要含有本地路由器的接口状 ...
- c++野指针 之 实战篇
一:今天做poj上的3750那个题,用到了list的erase方法.提交之后总是报runtime error! 纠结了好长时间.曾有一度怀疑过vector的erase和list的erase处理方式不一 ...
- Get与Post的差别
Http定义了与server交互的不同方法,最主要的方法有4种,各自是GET,POST.PUT,DELETE. URL全称是资源描写叙述符.我们能够这样觉得:一个URL地址,它用于描写叙述一个网络上的 ...
- 《UNIX编程环境》的源代码的第二个版本Ubuntu下编
1.在http://www.apuebook.com下载源代码 2. 视图READ root@ubuntu:/home/wl/mywork/apue.2e# cat -n README 1 Read ...
- Memcached在.net中的应用
一.MemCached下载 服务端下载:http://memcachedproviders.codeplex.com/ client下载:path=/trunk">http://sou ...
- win7提示“ipconfig不是内部或外部命令”
进入windows环境变量设置->系统变量,找到path,添加C:\Windows\SysWOW64,或者c:\windows\system32