此为Rbac在ThinkPHP中应用的实例,用到了ThinkPHP的Rbac扩展,样式比较难看,大家将就的看。此例子旨在学习交流使用,勿用于实际项目中。
Rbac简单说来就是基于“角色”的权限控制,所有用户都属于某一用户组,之后给用户组授权,则组内成员都有相应权限。而ThinkPHP的Rbac的核心,即为在每个操作前都进行权限验证操作,用_initialize方法实现。而权限位则在登录时写到session中,如果对权限的实时性要求非常高,也可以设置每一次验证都到数据库里查。
程序源码:
http://vdisk.weibo.com/s/fOMnN
相关配置项:
'USER_AUTH_ON' => true,//开启验证
'USER_AUTH_TYPE' => 1,//验证类型
'USER_AUTH_KEY' => 'uid',
// REQUIRE_AUTH_MODULE 需要认证模块,不设置即为除了NOT_AUTH_MODULE中的模块外全部验证
'NOT_AUTH_MODULE' => 'Public',
'USER_AUTH_GATEWAY' => '/Public/login', //认证网关
// RBAC_DB_DSN 数据库连接DSN,默认使用配置文件
'RBAC_ROLE_TABLE' => 'think_role', //角色表名称
'RBAC_USER_TABLE' => 'think_role_user', //用户表名称
'RBAC_ACCESS_TABLE' => 'think_access', //权限表名称
'RBAC_NODE_TABLE' => 'think_node', //节点表名称
'USER_AUTH_MODEL' => 'User',
'AUTH_PWD_ENCODER' => 'md5',
'GUEST_AUTH_ON' => false,
'ADMIN_AUTH_KEY' => 'administrator',//管理员标识
认证方法:
public function _initialize(){
// 用户权限检查
if (C('USER_AUTH_ON') && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
import('ORG.Util.RBAC');
if (!RBAC::AccessDecision()) {
//检查认证识别号
if (!$_SESSION [C('USER_AUTH_KEY')]) {
//跳转到认证网关
redirect(PHP_FILE . C('USER_AUTH_GATEWAY'));
}
// 没有权限 抛出错误
if (C('RBAC_ERROR_PAGE')) {
// 定义权限错误页面
redirect(C('RBAC_ERROR_PAGE'));
} else {
if (C('GUEST_AUTH_ON')) {
$this->assign('jumpUrl', PHP_FILE . C('USER_AUTH_GATEWAY'));
}
// 提示错误信息
$this->error(L('_VALID_ACCESS_'));
}
}
}
}
程序截图如下:
SQL:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `think_access`
-- ----------------------------
DROP TABLE IF EXISTS `think_access`;
CREATE TABLE `think_access` (
`role_id` smallint(6) unsigned NOT NULL,
`node_id` smallint(6) unsigned NOT NULL,
`pid` smallint(6) NOT NULL DEFAULT '0',
`level` tinyint(1) NOT NULL,
`module` varchar(50) DEFAULT NULL,
KEY `groupId` (`role_id`),
KEY `nodeId` (`node_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of think_access
-- ----------------------------
INSERT INTO `think_access` VALUES ('62', '112', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '109', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '108', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '107', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '106', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '105', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '104', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '103', '102', '2', null);
INSERT INTO `think_access` VALUES ('62', '102', '0', '1', null);
-- ----------------------------
-- Table structure for `think_node`
-- ----------------------------
DROP TABLE IF EXISTS `think_node`;
CREATE TABLE `think_node` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`title` varchar(50) DEFAULT NULL,
`status` tinyint(1) DEFAULT '0',
`remark` varchar(255) DEFAULT NULL,
`sort` smallint(6) unsigned DEFAULT NULL,
`pid` smallint(6) unsigned NOT NULL,
`level` tinyint(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `level` (`level`),
KEY `pid` (`pid`),
KEY `status` (`status`),
KEY `name` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=113 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of think_node
-- ----------------------------
INSERT INTO `think_node` VALUES ('102', 'Index', '首页', '1', '', '0', '0', '1');
INSERT INTO `think_node` VALUES ('103', 'Index', '首页模块', '1', '', '0', '102', '2');
INSERT INTO `think_node` VALUES ('104', 'index', '首页', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('105', 'left', '左侧模板', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('106', 'main', '右侧模板', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('107', 'header', '头部模板', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('108', 'add', '用户管理', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('109', 'addUser', '新增用户', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('112', 'ajaxuser', '获取用户组', '1', '', '0', '103', '3');
-- ----------------------------
-- Table structure for `think_role`
-- ----------------------------
DROP TABLE IF EXISTS `think_role`;
CREATE TABLE `think_role` (
`id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`pid` smallint(6) DEFAULT NULL,
`status` tinyint(1) unsigned DEFAULT NULL,
`remark` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=63 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of think_role
-- ----------------------------
INSERT INTO `think_role` VALUES ('62', '用户管理组', '0', '1', '这是用户管理组,只能新增用户');
-- ----------------------------
-- Table structure for `think_role_user`
-- ----------------------------
DROP TABLE IF EXISTS `think_role_user`;
CREATE TABLE `think_role_user` (
`role_id` mediumint(9) unsigned DEFAULT NULL,
`user_id` char(32) DEFAULT NULL,
KEY `group_id` (`role_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of think_role_user
-- ----------------------------
INSERT INTO `think_role_user` VALUES ('62', '112');
-- ----------------------------
-- Table structure for `think_user`
-- ----------------------------
DROP TABLE IF EXISTS `think_user`;
CREATE TABLE `think_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`password` char(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=122 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of think_user
-- ----------------------------
INSERT INTO `think_user` VALUES ('2', 'admin', '21232f297a57a5a743894a0e4a801fc3');
INSERT INTO `think_user` VALUES ('112', 'test', '098f6bcd4621d373cade4e832627b4f6');
欢迎交流讨论