您的位置:首页 > 编程语言 > PHP开发

【学习笔记】thinkPHP的RBAC的基本用方法

2011-02-11 17:12 351 查看
1.在数据库中创建相应的表

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for think_access
-- ----------------------------
CREATE TABLE `think_access` (
`role_id` smallint(6) unsigned NOT NULL,
`node_id` smallint(6) unsigned NOT NULL,
`level` tinyint(1) NOT NULL,
`pid` smallint(6) NOT NULL,
`module` varchar(50) DEFAULT NULL,
KEY `groupId` (`role_id`),
KEY `nodeId` (`node_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for 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,
`type` tinyint(1) NOT NULL DEFAULT '0',
`group_id` tinyint(3) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `level` (`level`),
KEY `pid` (`pid`),
KEY `status` (`status`),
KEY `name` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=83 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for 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,
`ename` varchar(5) DEFAULT NULL,
`create_time` int(11) unsigned NOT NULL,
`update_time` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `parentId` (`pid`),
KEY `ename` (`ename`),
KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for 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;
-- ----------------------------
-- Table structure for think_user
-- ----------------------------
CREATE TABLE `think_user` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`account` varchar(64) NOT NULL,
`nickname` varchar(50) NOT NULL,
`password` char(32) NOT NULL,
`bind_account` varchar(50) NOT NULL,
`last_login_time` int(11) unsigned DEFAULT '0',
`last_login_ip` varchar(40) DEFAULT NULL,
`login_count` mediumint(8) unsigned DEFAULT '0',
`verify` varchar(32) DEFAULT NULL,
`email` varchar(50) NOT NULL,
`remark` varchar(255) NOT NULL,
`create_time` int(11) unsigned NOT NULL,
`update_time` int(11) unsigned NOT NULL,
`status` tinyint(1) DEFAULT '0',
`type_id` tinyint(2) unsigned DEFAULT '0',
`info` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `account` (`account`)
) ENGINE=MyISAM AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;


//(在RBAC.class.php文件里也可以找到上面的SQL语句)

2.config.php文件配置

return array(
//'配置项'=>'配置值'
//RBAC配置增加设置
'USER_AUTH_MODEL'	 =>'Users',
'USER_AUTH_ON'	 =>true,	 //是否需要认证
'USER_AUTH_TYPE'	 =>'2',	 //认证类型:1为登录模式,2为实时模式
'USER_AUTH_KEY'	 =>'uid',	//认证识别号(SEESION的用户ID名)
'ADMIN_AUTH_KEY'	 =>'admin',	//管理员SEESION
'REQUIRE_AUTH_MODULE'	=>'',	 //需要认证模块(模块名之间用短号分开)
'NOT_AUTH_MODULE'	 =>'',	 //无需认证模块(模块名之间用短号分开)
'REQUIRE_AUTH_ACTION'	=>'',	 //需要认证方法(方法名之间用短号分开)
'NOT_AUTH_ACTION'	 =>'login,checkLogin',	 //无需认证方法(方法名之间用短号分开)
'USER_AUTH_GATEWAY'	 =>'',	 //认证网关
'RBAC_USER_TABLE'	 =>'think_role_user',	//用户角色明细表
'RBAC_ROLE_TABLE'	 =>'think_role',	 //角色表
'RBAC_ACCESS_TABLE'	 =>'think_access',	 //权限表
'RBAC_NODE_TABLE'	 =>'think_node',	 //节点表
);


3.创建一个公共控制器类CommonAction

class CommonAction extends Action{
function _initialize(){
header("Content-Type:text/html; charset=utf8");
import("@.ORG.RBAC");
if(!RBAC::AccessDecision()){
echo "没有权限";
}
}
}


写到这里其实RBAC基本功能已经出来了,大家可以测试一下,下面我们来进行一些更加详细的设置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: