Home > php教程 > php手册 > Auth权限控制类通用增强代码

Auth权限控制类通用增强代码

WBOY
Release: 2016-06-07 11:41:05
Original
2033 people have browsed it

增强了Auth功能与Auth结构化,具体详细功能代码TPer手动实现吧
// 角色表
CREATE TABLE `task_auth_group` (

`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,// 角色id

`title` char(100) NOT NULL DEFAULT '', // 角色名称

`status` tinyint(1) NOT NULL DEFAULT '1', // 角色状态 1:开启 2:关闭

`rules` char(80) NOT NULL DEFAULT '', // 角色拥有的权限规则id 多权限规则id用 , 连接

PRIMARY KEY (`id`) //设定为主键
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

// 用户权限表
CREATE TABLE `task_auth_group_access` (
`uid` mediumint(8) unsigned NOT NULL, // 对应用户id
`group_id` mediumint(8) unsigned NOT NULL, // 对应角色id
UNIQUE KEY `uid_group_id` (`uid`,`group_id`), // 唯一索引组
KEY `uid` (`uid`), // 普通索引
KEY `group_id` (`group_id`) // 普通索引
) ENGINE=MyISAM DEFAULT CHARSET=utf8

// 权限规则表 支持条件规则
CREATE TABLE `task_auth_rule` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, // 规则id 自增
`name` char(80) NOT NULL DEFAULT '', // 权限规则标识
`title` char(20) NOT NULL DEFAULT '', // 权限规则名称
`type` tinyint(1) NOT NULL DEFAULT '1', // 权限规则类型 1:url 2:菜单(不知道这个干啥用的)
`status` tinyint(1) NOT NULL DEFAULT '1', // 权限规则状态 1:开启 2:关闭
`condition` char(100) NOT NULL DEFAULT '', // 权限规则条件
`mid` int(10) unsigned NOT NULL DEFAULT '1', // 权限所属模块id
`level` tinyint(1) unsigned NOT NULL DEFAULT '0', // 菜单等级 0:不为菜单 1:左侧菜单 2:顶部菜单 3:底部菜单
PRIMARY KEY (`id`), // 设定主键为id
UNIQUE KEY `name` (`name`) // 唯一索引
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

// 模块表
CREATE TABLE `task_modules` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, // 模块id 自增
`name` varchar(20) NOT NULL, // 模块标识
`title` varchar(20) NOT NULL, // 模块名称
`level` tinyint(1) unsigned NOT NULL DEFAULT '1', // 模块等级 1:应用 2:控制器
`pid` tinyint(1) unsigned NOT NULL DEFAULT '0', // 模块上级id 0:应用 1:控制器
PRIMARY KEY (`id`) // 设定主键id
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

//用户表
CREATE TABLE `task_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id',
`username` varchar(20) NOT NULL COMMENT '用户登录名',
`password` varchar(32) NOT NULL COMMENT '用户登录密码',
`name` varchar(20) NOT NULL DEFAULT '0' COMMENT '用户昵称',
`qq` varchar(15) NOT NULL DEFAULT '0' COMMENT '用户QQ',
`tel` varchar(11) NOT NULL DEFAULT '0' COMMENT '用户电话',
`logindate` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '登录时间',
`createdate` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建用户时间',
`loginnums` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户登录次数',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

ThinkPHP公用控制器代码
namespace Common\Controller;
use Think\Controller;
use Think\Auth;

class CommonController extends Controller{
protected function _initialize(){
$sess_auth = session('auth');
//还未登陆后台
if(!$sess_auth){
$this->error('对不起,你还未登陆!',U('Login/index'));
}
//获取左侧菜单
$this->assign("menu",menu(1));
//超级管理员免验证
if($sess_auth['id'] == C("Administrator")){
return true;
}
$auth = new Auth();
//检查普通用户权限
if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,$sess_auth['id'])){
$this->error("没有权限");
}
}
}

ThinkPHP 公用函数文件
// 获取菜单数据
function menu($level){

//非超级管理员获取菜单
if(session('auth')['id'] != C("Administrator")){
$auth = new \Think\Auth;
$tmp_rules = $auth->getGroups(session("auth")['id']);
$rules = explode(",",$tmp_rules[0]['rules']);
foreach($rules as $k){
$rule = M("auth_rule")->find($rules[$k]);
if($rule['level'] == $level){
$menu[] = $rule;
}
}
} else {
$rules = M("auth_rule")->where(array('level'=>$level))->field("id,level")->select();
foreach($rules as $k => $v){
$rule = M("auth_rule")->find($v['id']);
if($rule['level'] == $level){
$menu[] = $rule;
}
}
}

return $menu;
}

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template