Upgraded auth permission management system demo of rbac in thinkphp Integration Series

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn about auth permission management.
Permission management is a standard for websites;

Unless used by individuals like blogs, the importance of permission management is self-evident;

Write auth permission management today;

Thinkphp has built-in auth permission class:/ThinkPHP/Library/Think/Auth. class. php

Execute the SQL statement to generate three tables: auth_rule, auth_group, and auth_group_access;

Create another users table by yourself. Of course, other names can also be created, but they must be described in the configuration items;

First, we will give a brief introduction to the functions of each table;

Users: User table. This is not nonsense;

Auth_group: User Group table, such as super Administrator group, common Administrator group, and editing. It also records the permissions of each management group;

Auth_group_access: user and group association table. For example, user 1 belongs to super administrator and user 2 belongs to common administrator and editing;

Auth_rule: Permission table. What is the specific permission;

If you have not read permission management, you are advised to read the source code first. You can thoroughly learn the same thing. The best way is to study the source code;

Here we will not focus on the principle of auth, but on a demo of auth;

Git source code: http://git.oschina.net/shuaibai123/thinkphp-bjyadmin

1: download and install the project first;

Click "Super Administrator Logon" and "Article Administrator Logon" respectively;

You will find that their permissions are different; the background menus are different;



2: menu management



To control which menus can be viewed by each administrator, menu management is required;

The operation is the admin_nav table in the demo.

3: permission management



The specific name and content of each permission. Here I usually correspond to the menu;

However, it will be a little more than menu management; compared with the two images, you can see that the menu is generally added, deleted, and modified;

Operate on the auth_rule table in the demo;


4. User Group Management



Add a management group and assign permissions to each management group. The selected permissions indicate that you have the permission to view or operate the group;



5: Administrator list



List all administrators. You can add an administrator or modify the Administrator's Management Group;




After such a structure is built, you only need to complete the following code in AdminBaseController. class. php;

/Application/Common/Controller/AdminBaseController. class. php $ Auth = new \ Think \ Auth ();
$ Rule_name = MODULE_NAME. '/'. CONTROLLER_NAME. '/'. ACTION_NAME;
$ Result = $ auth-> check ($ rule_name, $ _ SESSION ['user'] ['id']);
If (! $ Result ){
$ This-> error ('you are not authorized to access ');
}
This is also the reason why BaseController is built in/Application/Common/Controller in the thinkphp directory structure design experience summary;
This article is Bai Junyao Original article, reproduced do not need to contact me, but please indicate from BAI Junyao blog http://www.baijunyao.com

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.