Home Backend Development PHP Tutorial php personnel authority management (RBAC)

php personnel authority management (RBAC)

Mar 26, 2017 am 09:29 AM

Before talking about permission management, you should first know what functions permission management has:

 (1) Users can only access, specify Controller, specified method

 (2) Users can exist in multiple user groups

 (3) User groups You can choose the specified controller and the specified method

(4), you can add the controller and method

RBAC (Role-Based Access Control (role-based access control) is where users are associated with permissions through roles. Simply put, a user has several roles, and each role has several permissions. In this way, a "user-role-permission" authorization model is constructed. In this model, there is generally a many-to-many relationship between users and roles, and between roles and permissions.

1. Database design

Write five tables, first: user table, role table, function table:

Connection Table of tables.. Next is the role function table and user role table:

2. Administrator's Management page,

<?php
    include ("../db.class.php");
    $db = new db();
    $sql = "select * from qxyh";
    $arr = $db->Query($sql);
    foreach ($arr as $v)
    {
        echo "<option value=&#39;{$v[0]}&#39;>{$v[2]}</option>";
    }

    ?>
Copy after login

(1). Display user name and role name respectively

(2). According to the drop-down user name Change, change the role in the corresponding check box

(3). When modifying the user role, first delete the user's corresponding role table and all the information of this user, and then retrieve The username and role code are newly added.

Use the drop-down list: embed the php query and traverse it, display it in the form of a drop-down list

Select the role, use multiple Marquee:


<p style="margin-bottom: 7px;"><p>请选择角色<br/><?php<br/>$sjs = "select * from qxzw";<br/>$ajs = $db->Query($sjs);<br/>foreach ($ajs as $v){    <br/>    echo "<input type=&#39;checkbox&#39; value=&#39;{$v[0]}&#39; class=&#39;ck&#39;/>{$v[1]} ";<br/>}?><br/></p><br/><input type="button" value="确定" id="btn"/><br/></p>
Copy after login

Picture:

##When the user changes, the corresponding role also changes accordingly, and the person's role information is changed and added and saved. The basic idea of ​​adding and saving is to first delete all the role information corresponding to the person in the database, and then retrieve it. The selected part is added 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>
Copy after login

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=&#39;{$uid}&#39;";
        echo $db->strQuery($sql);
break;
}
Copy after login

Let’s take a look at the final result. If you log in successfully, you will enter the homepage. If you log in failed, you will get an 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>
Copy after login

Processing 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 = &#39;{$uid}&#39;";
        $db->Query($sdel,0);
        //拆分取到的字符串
        $arr= explode("|",$juese);
        foreach ($arr as $v)
        {
            $sql = "insert into qxyhzw VALUES (&#39;&#39;,&#39;{$uid}&#39;,&#39;{$v}&#39;)";
            $db->query($sql,0);
        }
        echo "ok";
        break;
}
Copy after login

See the effect:

The role is selected by default;

Choose to save after making changes:

#Management page summary Code:




    无标题文档
    


用户与角色管理

请选择用户
请选择角色 Query($sjs); foreach ($ajs as $v) { echo "{$v[1]} "; } ?>
</body>
Copy after login

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=&#39;{$uid}&#39;";
        echo $db->strQuery($sql);
break;
    case 1:
        $uid = $_POST["zhang"];
        $juese = $_POST["juese"];
        //        首先全部删掉里面的职位
        $sdel = "delete from qxyhzw WHERE uid = &#39;{$uid}&#39;";
        $db->Query($sdel,0);
        //拆分取到的字符串
        $arr= explode("|",$juese);
        foreach ($arr as $v)
        {
            $sql = "insert into qxyhzw VALUES (&#39;&#39;,&#39;{$uid}&#39;,&#39;{$v}&#39;)";
            $db->query($sql,0);
        }
        echo "ok";
        break;
}
Copy after login

##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>
Copy after login

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 = &#39;{$zhang}&#39;";$mm = $db->strQuery($sql)>0;
if($mm = $mi && !empty($mi)){    
    $_SESSION["zhang"] = $zhang;    
    header("location:chaxun.php");
}//else
//{
//    echo "登入失败";
//}
Copy after login
Jump to the main page, main page code:

Everyone’s main page is different of

<body><h1>主页面</h1>
Copy after login
<?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 = &#39;{$zhang}&#39;";$aql = $db->Query($sql);//根据角色代号查功能代号$attr = array();//定义一个存放功能代号的数组foreach ($aql as $v)
{   $jsid = $v[0];// 角色代号
    $ssql = "select rid from qxgnzw WHERE jid=&#39;{$jsid}&#39;";    $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 = &#39;{$k}&#39;";    
$arr = $db->Query($ql);    arr[0][0];    $arr[0][1];    echo "<p code=&#39;{$arr[0][0]}&#39;>{$arr[0][1]}</p>";
}?>
Copy after login
</body>
Copy after login

 

 

用php的用户体验不好,最好还是得用ajax

The above is the detailed content of php personnel authority management (RBAC). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles