5月27 权限设置及功能
建表要求:
create table Users ( UserName varchar(50) primary key, Password varchar(50), Name varchar(50) ) ; create table JueSe ( Code varchar(50) primary key, Name varchar(50) ) ; create table Rules ( Code varchar(50) primary key, Name varchar(50) ) ; create table UserInJueSe ( Ids int auto_increment primary key, UserId varchar(50) references Users(UserName), JueSeId varchar(50) references JueSe(Code) ) ; create table JueSeWithRules ( Ids int auto_increment primary key, JueSeId varchar(50) references JueSe(Code), RuleId varchar(50) references Rules(Code) ) ; insert into JueSe values('j001','管理员'); insert into JueSe values('j002','前台'); insert into JueSe values('j003','市场'); insert into JueSe values('j004','财务'); insert into JueSe values('j005','行政'); insert into Rules values('r001','咨询登记'); insert into Rules values('r002','招聘面试'); insert into Rules values('r003','业务洽谈'); insert into Rules values('r004','市场分析'); insert into Rules values('r005','财务统计'); insert into Rules values('r006','报表分析'); insert into Rules values('r007','考勤管理'); insert into JueSeWithRules values('','j001','r001'); insert into JueSeWithRules values('','j001','r002'); insert into JueSeWithRules values('','j001','r003'); insert into JueSeWithRules values('','j001','r004'); insert into JueSeWithRules values('','j001','r005'); insert into JueSeWithRules values('','j001','r006'); insert into JueSeWithRules values('','j001','r007'); insert into JueSeWithRules values('','j002','r001'); insert into JueSeWithRules values('','j002','r002'); insert into JueSeWithRules values('','j003','r003'); insert into JueSeWithRules values('','j003','r004'); insert into JueSeWithRules values('','j004','r005'); insert into JueSeWithRules values('','j005','r006'); insert into JueSeWithRules values('','j005','r007');
权限管理数据显示页面:
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>权限管理</title> <script src="../jquery-1.11.2.min.js"></script> </head> <body> <br /> <br /> <h1>权限管理</h1> <br /> <br /> <div>请选择用户: <select id="user"> <?php include("../DBDA.php"); $db = new DBDA(); $sql = "select * from Users"; $attr = $db->Query($sql); var_dump($attr); foreach($attr as $v) { echo "<option value='{$v[0]}'>{$v[2]}</option>"; } ?> </select> </div> <br /> <div> <div>请选择角色:</div><br /> <div> <?php $sqljs = "select * from JueSe"; $attrjs = $db->Query($sqljs); //var_dump($attrjs); foreach($attrjs as $vjs) { echo "<input type='checkbox' class='js' value='{$vjs[0]}'>{$vjs[1]} "; } ?> </div> </div> <br /> <input type="button" value="确定" id="btn" /> </body> </html> <script type="text/javascript"> $(document).ready(function(e) { ShowJueSe();//避免首次打开什么都没有 $("#user").change(function(){ ShowJueSe(); }) function ShowJueSe() { var uid = $("#user").val(); $.ajax({ url:"seljuese2.php", data:{uid:uid}, type:"POST", dataType:"TEXT", success: function(data){ //alert(data); var shuzu = data.split("|"); var ck = $(".js"); ck.prop("checked",false);//清除之前选择 for(var i=0;i<ck.length;i++) { var v = ck.eq(i).val(); //alert($.inArray(v,shuzu)); if($.inArray(v,shuzu)>=0)//-1是没有选中,选中的找索引 { ck.eq(i).prop("checked",true); } } } }); } //添加角色的处理 $("#btn").click(function(){ var uid = $("#user").val(); var ck = $(".js"); var str = ""; for(var i=0;i<ck.length;i++) { if(ck.eq(i).prop("checked")) { str =str+ck.eq(i).val()+"|"; } } str = str.substr(0,str.length-1); $.ajax({ url:"addchuli3.php", data:{uid:uid,juese:str}, type:"POST", dataType:"TEXT", success: function(data){ if(data.trim()=="OK") { alert("操作成功"); } else { alert("操作失败"); } } }); }) }); </script>
权限管理数据处理:查询用户的角色:seljuese2.php
<?php $uid = $_POST["uid"]; include("../DBDA.php"); $db = new DBDA(); $sql = "select JueSeId from UserInJueSe where UserId = '{$uid}' "; echo $db->StrQuery($sql);
权限管理数据处理:更改用户的角色:addchuli3.php
<?php include("../DBDA.php"); $db = new DBDA(); $uid = $_POST["uid"]; $juese = $_POST["juese"]; $shuzu = explode("|",$juese); $bs = true;//定义bool型 //清除所有之前选中重新选 $sqln = "delete from UserInJueSe where UserId = '{$uid}'"; if(!$db->Query($sqln,1)) { $bs = $bs && false; } foreach($shuzu as $v) { $sqladd = "insert into UserInJueSe values('','{$uid}','{$v}')"; if(!$db->Query($sqladd,1)) { $bs = $bs && false; } } if($bs) { echo "OK"; } else { echo "NO"; }
权限管理的显示效果
登录数据显示页面:login4.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录</title>
</head>
<body>
<br />
<br />
<h1>登录</h1>
<form action="loginchuli4.php" method="post">
<div>用户名:<input type="text" name="uid" /></div><br />
<div>密 码 :<input type="text" name="pwd" /></div><br />
<div><input type="submit" value="登录" /></div>
</form>
</body>
</html>
登录数据处理页面:login4chuli.php
<?php session_start(); include("../DBDA.php"); $db = new DBDA(); $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; $sql = "select count(*) from Users where Uid = '{$uid}' and Pwd = '{$pwd}'"; $attr = $db->StrQuery($sql); if($attr==1) { $_SESSION["uid"] = $uid; header("location:main5.php"); } else { header("location:login4.php"); }
主页面:查看用户对应的所有的功能:main5.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> <?php session_start(); include("../DBDA.php"); $db = new DBDA(); if(empty($_SESSION["uid"])) { header("location:login4.php"); exit; } $uid = $_SESSION["uid"]; ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>主页面</title> <style type="text/css"> *{ margin:0 auto; padding:0 } .menu { width:100%; height:40px;} .qx { width:100px; height:40px; background-color:#639; color:white; font-size:18px; text-align:center; line-height:40px; vertical-align:middle; float:left; } .qx:hover { background-color:#0F9; cursor:pointer;} </style> </head> <body> <br /> <br /> <center><h1>主页面</h1></center><br /> <a href="login4.php">退出</a><br /><br /> <div class="menu"> <div class="qx">权限管理</div> <?php //通过uid查询UserInJueSe表中的JueSeId $sjs = "select JueSeId from UserInJueSe where UserId = '{$uid}'"; $ajs = $db->Query($sjs); //var_dump($ajs);//找到角色代号 //根据JueSeId找JueSeWithRules表中的RuleId $all = array();//存储用户的RuleId foreach($ajs as $vjs) { $sgn = "select RuleId from JueSeWithRules where JueSeId = '{$vjs[0]}'"; //找到所有对应的功能代号 $agn = $db->Query($sgn); //var_dump($agn);//找到所有的功能代号 foreach($agn as $vgn) { array_push($all,$vgn[0]); } } $all = array_unique($all);//找到所有的功能代号 //var_dump($all);//一维数组 //显示对应的功能 foreach($all as $v) { //自己的 /*$sql = "select * from Rules where Code = '{$v}'"; $attr = $db->Query($sql); //var_dump($attr);//查询出所有的功能名称 echo "<div class='qx'>{$attr[0][1]}</div>";*/ //老师的 $sql = "select Name from Rules where Code = '{$v}'"; $name = $db->StrQuery($sql); //echo $name; echo "<div class='qx'>{$name}</div>"; } ?> </div> </body> </html>