Home > Article > Backend Development > Detailed explanation of the implementation method of PHP personnel authority management (RBAC)
Before talking about Permission management, you should first know the permissions What functions are required for management:
(1) Users can only access the specified controller and the specified method
(2) Users can exist in multiple user groups
(3) User groups can be selected, specified controllers, specified methods
(4), you can add controllers and methods
RBAC (Role-Based Access Control, role-based access control), that is, the user can Roles are associated with permissions. Simply put, a user has several roles, and each role has several permissions. In this way, a "user-role-permission" authorization model is constructed. , There is generally a many-to-many relationship between roles and permissions.
#1. Database. Design
##Write five tables, first: user table, role table, function table:
The table of the connection table...Then there is the role function table and the user role table:
##2. Administrator’s management page,
(1). Display user name and role name respectively
(2). According to the change of the drop-down user name, change the role in the corresponding check box
(3). When modifying the user role, first make the corresponding role table of the user. This user Delete all the information, and then add the obtained user name and role code
##Use the drop-down list: embed the php query and traverse it, and display it in the form of a drop-down list
<select id="user">
0314a96823775a5678332a9797f46a39Query($sql); foreach ($arr as $v) { echo "8dfcd5164ba7c14d3a3a5df4cc194797{$v[2]}4afa15d3069109ac30911f04c56f3338"; } ?>
</select>
Select the character and use the multi-select box:
<p>
请选择角色
<?php
$sjs = "select * from qxzw";
$ajs = $db->Query($sjs);
foreach ($ajs as $v)
{
echo "<input type='checkbox' value='{$v[0]}' class='ck'/>{$v[1]} ";
}
?>
</p>
<input type="button" value="确定" id="btn"/>
Picture:
When the user changes, the corresponding role also changes accordingly, and the role information of the person is changed, added and saved. The basic idea of adding and saved is to first match the person in the database Delete all the character information, then get the selected part and add it to the database
##Let him select the default role first:
<script> //选中默认角色 function xuan() { var uid = $("#user").val(); $.ajax({ url:"chuli.php", data:{uid:uid,type:0}, type:"POST", dataType:"TEXT", success:function(data) { var juese = data.trim().split("|"); //拆分完全都变成代号 var ck = $(".ck"); ck.prop("checked",false); for(var i=0;i<ck.length;i++) { //便利所有的列表 if(juese.indexOf(ck.eq(i).val())>=0) { ck.eq(i).prop("checked",true); } } } }); } </script>to write his processing page:
<?php include ("../db.class.php"); $db = new db(); $type = $_POST["type"]; switch ($type) { case 0: $uid = $_POST["uid"]; $sql = "select jid from qxyhzw WHERE uid='{$uid}'"; echo $db->strQuery($sql); break; }
Let’s take a look at the final result, If you log in successfully, you will enter the homepage. If you fail to log in, you will be prompted with the error
# #Come again, save button:
<script> //当用户变化的时候去选中相应角色 $("#user").change(function(){ xuan(); }) //点击确定保存角色信息 $("#btn").click(function(){ var uid = $("#user").val(); //找到用户名 var juese = ""; // 找到角色代号 var ck = $(".ck"); //找到所有的checked 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>
Process page:
<?php include ("../db.class.php"); $db = new db(); $type = $_POST["type"]; switch ($type) { case 1: $uid = $_POST["uid"]; $juese = $_POST["juese"]; // 首先全部删掉里面的职位 $sdel = "delete from qxyhzw WHERE uid = '{$uid}'"; $db->Query($sdel,0); //拆分取到的字符串 $arr= explode("|",$juese); foreach ($arr as $v) { $sql = "insert into qxyhzw VALUES ('','{$uid}','{$v}')"; $db->query($sql,0); } echo "ok"; break; }
See the effect:
The role is selected by default;
Choose to save after making changes:
## Total code of management page: 请选择用户
<select id="user">
Query($sql);
foreach ($arr as $v)
{
echo "";
}
?>
</select>
用户与角色管理
Total code for processing page:
<?php
include ("../db.class.php");
$db = new db();
$type = $_POST["type"];
switch ($type)
{
case 0:
$uid = $_POST["zhang"];
$sql = "select jid from qxyhzw WHERE uid='{$uid}'";
echo $db->strQuery($sql);
break;
case 1:
$uid = $_POST["zhang"];
$juese = $_POST["juese"];
// 首先全部删掉里面的职位
$sdel = "delete from qxyhzw WHERE uid = '{$uid}'";
$db->Query($sdel,0);
//拆分取到的字符串
$arr= explode("|",$juese);
foreach ($arr as $v)
{
$sql = "insert into qxyhzw VALUES ('','{$uid}','{$v}')";
$db->query($sql,0);
}
echo "ok";
break;
}
##3. Login page:
The display is very simple: <form action="drcl.php" method="post">
<p>帐号:<input type="text" name="zhang"/></p>
<p>密码:<input type="text" name="mi"/></p>
<input type="submit" value="登入"/></form>
##Write login processing
<?php session_start(); include ("../db.class.php"); $db = new db(); $zhang = $_POST["zhang"]; $mi = $_POST["mi"]; $sql = "select mi from qxyh WHERE zhang = '{$zhang}'"; $mm = $db->strQuery($sql)>0; if($mm = $mi && !empty($mi)) { $_SESSION["zhang"] = $zhang; header("location:chaxun.php"); } //else //{ // echo "登入失败"; //}
跳转到主页面,主页面代码:
每个人的主页面都是不一样的
<body> <h1>主页面</h1> <?php session_start(); include ("../db.class.php"); $db = new db(); $zhang = ""; if(empty($_SESSION["zhang"])) { header("location:qx_dr.php"); exit; } //登入者用户名 $zhang = $_SESSION["zhang"]; //根据用户名查角色 $sql = "select jid from qxyhzw WHERE uid = '{$zhang}'"; $aql = $db->Query($sql); //根据角色代号查功能代号 $attr = array(); //定义一个存放功能代号的数组 foreach ($aql as $v) { $jsid = $v[0];// 角色代号 $ssql = "select rid from qxgnzw WHERE jid='{$jsid}'"; $aaql = $db->strQuery($ssql); //拆分 $adai = explode("|",$aaql); foreach ($adai as $h) { array_push($attr,$h); } } $attr = array_unique($attr); //去重 //显示 foreach ($attr as $k) { $ql = "select * from qxgn WHERE code = '{$k}'"; $arr = $db->Query($ql); $arr[0][0]; $arr[0][1]; echo "<p code='{$arr[0][0]}'>{$arr[0][1]}</p>"; } ?> </body>
用php的用户体验不好,最好还是得用ajax
The above is the detailed content of Detailed explanation of the implementation method of PHP personnel authority management (RBAC). For more information, please follow other related articles on the PHP Chinese website!