表 user, rules(功能表),role(角色表),user_role(用户角色关系) ,role_rule(角色功能关系表)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `role`
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', '总经理');
INSERT INTO `role` VALUES ('2', '副总经理');
INSERT INTO `role` VALUES ('3', '秘书');
-- ----------------------------
-- Table structure for `role_rules`
-- ----------------------------
DROP TABLE IF EXISTS `role_rules`;
CREATE TABLE `role_rules` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`role_id` int(10) DEFAULT NULL,
`rules_id` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role_rules
-- ----------------------------
INSERT INTO `role_rules` VALUES ('6', '1', '1');
INSERT INTO `role_rules` VALUES ('11', '3', '3');
INSERT INTO `role_rules` VALUES ('10', '3', '1');
-- ----------------------------
-- Table structure for `rules`
-- ----------------------------
DROP TABLE IF EXISTS `rules`;
CREATE TABLE `rules` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of rules
-- ----------------------------
INSERT INTO `rules` VALUES ('1', '合作关系');
INSERT INTO `rules` VALUES ('2', '招聘人事');
INSERT INTO `rules` VALUES ('3', '市场分析');
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL COMMENT '名称',
`passwd` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123');
INSERT INTO `user` VALUES ('2', 'lisi', '123');
-- ----------------------------
-- Table structure for `user_role`
-- ----------------------------
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user_id` int(10) DEFAULT NULL,
`role_id` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_role
-- ----------------------------
INSERT INTO `user_role` VALUES ('6', '1', '2');
INSERT INTO `user_role` VALUES ('5', '1', '1');
INSERT INTO `user_role` VALUES ('7', '1', '3');
INSERT INTO `user_role` VALUES ('8', '2', '1');
INSERT INTO `user_role` VALUES ('9', '2', '2');
INSERT INTO `user_role` VALUES ('10', '2', '3');
代码
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>无标题文档</title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<?php
include 'db.class.php';
$db = new Db();
$sql = "select * from user";
$res = $db->getAll($sql);
$sql = "select * from role";
$role = $db->getAll($sql);
?>
<h1>用户与角色管理</h1>
<p>请选择用户
<select id="user">
<?php
foreach ($res as $v){
echo "<option value='{$v['id']}'>{$v['name']}</option>";
}
?>
</select>
</p>
请选择角色
<?php
foreach ($role as $v){
echo "<input type='checkbox' value='{$v['id']}' class='ck'/>{$v['name']} ";
}
?>
</p>
<input type="button" value="确定" id="btn"/>
<script>
$(function(){
//点击确定保存角色信息
$("#btn").click(function(){
var uid = $("#user").val();
//找到用户名
var juese ='';
var ck = $(".ck");
for(var i=0;i<ck.length;i++){
if(ck.eq(i).prop("checked")){
juese += ck.eq(i).val()+"|";
}
}
juese = juese.substr(0,juese.length-1);
$.ajax({
url:"chuli.php",
data:{uid:uid,juese:juese,type:1},
type:"POST",
dataType:"TEXT",
success:function(data){
alert("修改成功");
}
});
})
});
</script>
</p>
</body>
</html>
chuli.php
<?php
include 'db.class.php';
$uid = $_POST["uid"];
$juese = $_POST["juese"];
$type = $_POST['type'];
$db = new Db();
if($type==1){
$sdel = "delete from user_role WHERE user_id = '{$uid}'";
$db->query($sdel);
//拆分取到的字符串
$arr= explode("|",$juese);
foreach ($arr as $v){
$sql = "insert into user_role VALUES ('','{$uid}','{$v}')";
$db->query($sql);
}
echo "ok";
}else{
//角色规则
$sdel = "delete from role_rules WHERE role_id = '{$uid}'";
$db->query($sdel);
//拆分取到的字符串
$arr= explode("|",$juese);
foreach ($arr as $v){
$sql = "insert into role_rules VALUES ('','{$uid}','{$v}')";
$db->query($sql);
}
echo "ok";
}
rolerules.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>无标题文档</title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<?php
include 'db.class.php';
$db = new Db();
$sql = "select * from role";
$res = $db->getAll($sql);
$sql = "select * from rules";
$rules = $db->getAll($sql);
?>
<h1>角色规则关系</h1>
<p>请选择角色
<select id="role">
<?php
foreach ($res as $v){
echo "<option value='{$v['id']}'>{$v['name']}</option>";
}
?>
</select>
</p>
请选择角色
<?php
foreach ($rules as $v){
echo "<input type='checkbox' value='{$v['id']}' class='ck'/>{$v['name']} ";
}
?>
</p>
<input type="button" value="确定" id="btn"/>
<script>
$(function(){
//点击确定保存角色信息
$("#btn").click(function(){
var uid = $("#role").val();
//找到用户名
var juese ='';
var ck = $(".ck");
for(var i=0;i<ck.length;i++){
if(ck.eq(i).prop("checked")){
juese += ck.eq(i).val()+"|";
}
}
juese = juese.substr(0,juese.length-1);
$.ajax({
url:"chuli.php",
data:{uid:uid,juese:juese,type:2},
type:"POST",
dataType:"TEXT",
success:function(data){
alert("修改成功");
}
});
})
});
</script>
</p>
</body>
</html>
db.class.php
<?php
class Db{
public $link = "";
public function __construct(){
try {
$this->link = new PDO('mysql:dbname=rbac;host=localhost;port=3306','root','root');
} catch(PDOException $e) {
die('Could not connect to the database:<br/>' . $e);
}
}
public function getOne($sql){
$result = $this->link->query($sql)->fetch(PDO::FETCH_ASSOC);
return $result;
}
public function getAll($sql){
$result = $this->link->query($sql)->FetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function query($sql){
$result = $this->link->exec($sql);
return $result;
}
}