权限管理
根据登录用户名显示不同功能:
<?php session_start(); include("../DBDA.class.php"); $db = new DBDA(); if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } $uid = $_SESSION["uid"]; ?> <body> <div style="width:100%; height:35px;"> <?php //根据用户名查角色代号 $sjuese="select jueseid from userinjuese where userid='{$uid}'"; $ajuese = $db->Query($sjuese); //根据角色代号查功能 $attr = array(); foreach($ajuese as $v) { $sgn = "select ruleid from juesewithrules where jueseid='{$v[0]}'"; $agn = $db->Query($sgn); $attr = array_merge($attr,$agn); //array_mergy()函数将一个或多个数组合并为一个; } //对功能代号数组进行去重 $attr = array_unique($attr,SORT_REGULAR); foreach($attr as $v) { $sname = "select name from rules where code='{$v[0]}'"; $name = $db->StrQuery($sname); echo "<div class='list'>{$name}</div>"; } ?>
用户角色管理:
主(管理)页面:
<?php include("../DBDA.class.php"); $db = new DBDA(); ?> <br /> <div> 请选择用户: <select id="sel"> <?php $sql = "select * from users"; $attr = $db->Query($sql); foreach($attr as $v) { echo "<option value='{$v[0]}'>{$v[2]}</option>"; } //取到第一个人的用户名 $fuid = $attr[0][0]; ?> </select> </div> <br /> <div> 请选择角色:<br /><br /> <?php $sjuese="select * from juese"; $ajuese = $db->Query($sjuese); foreach($ajuese as $v) { $sxz = "select count(*) from userinjuese where userid='{$fuid}' and jueseid='{$v[0]}'"; $n = $db->StrQuery($sxz); if($n>0) { echo "<input type='checkbox' class='juese' checked='checked' value='{$v[0]}' /> {$v[1]} "; } else { echo "<input type='checkbox' class='juese' value='{$v[0]}' /> {$v[1]} "; } } ?> </div> <br /> <input type="button" value="保存" id="btn" /> </body> <script type="text/javascript"> $("#sel").change(function(){ var uid = $("#sel").val(); $.ajax({ url:"chuli.php", data:{uid:uid}, type:"POST", dataType:"TEXT", success: function(data){ var js = data.split("|"); //清除选中状态 var ck = $(".juese"); ck.prop("checked",false); //让等于返回角色代号的选中 for(var i=0;i<ck.length;i++) { if(js.indexOf(ck.eq(i).val())>-1) { ck.eq(i).prop("checked",true); } } } }); }) $("#btn").click(function(){ var uid = $("#sel").val(); //角色代号 var ck = $(".juese"); 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:"add.php", data:{uid:uid,juese:str}, type:"POST", dataType:"TEXT", success: function(){ alert("保存成功!"); } }); }) </script>
默认显示处理页面:
<?php include("../DBDA.class.php"); $db = new DBDA(); $uid = $_POST["uid"]; $sql = "select jueseid from userinjuese where userid='{$uid}'"; echo $db->StrQuery($sql);
修改处理页面:
<?php include("../DBDA.class.php"); $db = new DBDA(); $uid = $_POST["uid"]; $juese = $_POST["juese"]; $attr = explode("|",$juese); //删除所有该用户的角色信息 $sdel = "delete from userinjuese where userid='{$uid}'"; $db->Query($sdel,0); //将取到了角色信息添加进去 foreach($attr as $v) { $sql = "insert into userinjuese values('','{$uid}','{$v}')"; $db->Query($sql,0); }