网站首页技术博客
基于Thinkphp权限管理的实现
摘要基于Thinkphp权限管理的实现
基本思路,用户,角色,权限,三层管理结构
对所有的控制器和方法进行注册写入权限表,不同的角色分配不同权限,不同用户划分入不同角色
在登录时查询用户角色所拥有权限并进行缓存
基于Thinkphp权限管理的实现
基本思路,用户,角色,权限,三层管理结构
对所有的控制器和方法进行注册写入权限表,不同的角色分配不同权限,不同用户划分入不同角色
在登录时查询用户角色所拥有权限并进行缓存
数据表
核心代码
角色中分配权限部分
RoleController.class.php
/** * 权限分配弹框 */ public function juris(){ if (IS_POST){ $data=I('post.'); $data['id'] = $data['role_id']; unset($data['role_id']); $data['juris'] = implode(",",$data['juris']); $res = M('roles')->save($data); if ($res){ //删除权限缓存 S(session("uname")."_juris",null); $this->success("权限分配成功!"); }else{ $this->error("权限分配失败!"); } return ; } $id = I('get.id'); //1、获取所有权限 $juris = M('jurisdiction')->field("id,name,controller,action")->select(); //已获得的权限 $juris_done = M('roles')->field("juris")->find($id); foreach ($juris as &$temp){ $isset = array_search($temp['id'],explode(",", $juris_done['juris'])); if ($isset!==false){ $temp['checked']=true; }else{ $temp['checked']=false; } if($temp['controller'] == "Index"){ $temp['checked'] =true; } } $this->assign("role_id",$id); $this->assign("juris",$juris); $this->display(); }
base控制器权限验证部分,带缓存,可在用户注销或分配权限时清除缓存
//获取权限 $juris = S(session("uname")."_juris"); if (empty($juris)){ $juris_done = M('roles')->field("juris")->find(session("user")['roleid']); if (empty($juris_done['juris'])){ $this->error("你没有这个系统的任何权限,请联系管理员为你分配权限!",U('Login/logout')); } //权限缓存 $juris = M('jurisdiction')->field("controller,action")->where("id in (".$juris_done['juris'].")")->select(); S(session("uname")."_juris",$juris); } $array=['controller'=>CONTROLLER_NAME,'action'=>ACTION_NAME]; $array2 = ['controller'=>CONTROLLER_NAME,'action'=>""]; //判断是否具有权限 $res = array_search($array, $juris)!==false||array_search($array2, $juris)!==false; if ($res===false){ $this->error("你无权访问本页面!"); return ; }
判断是否拥有某权限
function.php
/** * 判断是否拥有权限 * @param 权限ID $id或者多id数组 传入数组时有一个满足则返回true * @return boolean 拥有该权限返回true 否则返回false */ function hasJuris($id){ $flag = false; $roleID = session('user')['roleid']; $juris = M('roles')->field("juris")->find($roleID); $juris = explode(",", $juris['juris']); $res = false; //传入为数组 if(is_array($id)){ foreach ($id as $vo){ $res = $res||array_search($vo, $juris)!==false; } } //传入为单个id else{ $res = array_search($id, $juris); } if ($res===false){ $flag = false; }else{ $flag = true; } return $flag; }
权限管理截图:
分配角色权限截图:
注:权限项中只填写了控制器名没有方法名,则代表拥有该控制器的所有权限
相关文章
- Access denied; you need (at least one of) the SUPER privilege(s) for this operatio
- rm: cannot remove `file': Operation not permitted解决方法 chattr命令详解
- easywechat报错:Failed to cache access token
- chrome模拟微信浏览器的user agent
- jQuery打印插件jqprint
- 实现页面图片的延时异步加载
- 微信小程序登录流程
- mysql General error: 1267 Illegal mix of collations错误
- 基于general_log的mysql日志追踪
- linux nvm 安装及使用
- Mysql5.7绿色版的安装
- yum安装软件时出现No package *** available的问题
- 3 个PHP 知识总结:Memcache、缓存和正则
- curl_errno错误码说明
- 一款bootstrap开关控件推荐,bootstrap-switch
- 微信企业号开发验证URL报-40004-AESKey 非法
- PHP面试宝典
- 【原创】Mysql的主从配置,实现读写分离
- 【服务器运维】清理服务器磁盘空间
- linux tail命令的使用方法详解