TP thinkphp Rights Management function

Source: Internet
Author: User

Currently, in the TP framework do the Rights Management sub-RBAC (old) and Auth (recommended) authentication mode;

The old TP version is encapsulated in RBAC authentication;

The new point is beginning to be managed using the AUTH approach. It is recommended to use this method;

Implementation Step One: Introduction of Class Library Auth.class.php

Implementing step Two: Creating a data table

A. Menu table CREATE table ' Wifi_admin_nav ' (' id ' int (one) unsigned not NULL auto_increment COMMENT ' menu table ', ' pid ' int (one) unsigned Default ' 0 ' COMMENT ' belongs to menu ', ' name ' varchar (COMMENT ') ' Menu name ', ' MCA ' varchar (255) Default ' COMMENT ' module, CONTROLLER  , method ', ' ico ' varchar () default ' COMMENT ' font-awesome icon ', ' order_number ' int (one) unsigned default NULL COMMENT ' sort ', PRIMARY KEY (' id ')) engine=myisam auto_increment=43 DEFAULT charset=utf8;insert into ' Wifi_admin_nav ' VALUES (' 1 ', ' 0 ', ' System settings ', ' admin/shownav/config ', ' cog ', ' 1 '); INSERT into ' Wifi_admin_nav ' VALUES (' 2 ', ' 1 ', ' Menu Management ', ' admin/nav/index ', null , null); INSERT INTO ' Wifi_admin_nav ' VALUES (' 7 ', ' 4 ', ' Rights Management ', ' admin/rule/index ', ', ' 1 '); insert INTO ' Wifi_admin_nav ' V Alues (' 4 ', ' 0 ', ' Privilege control ', ' admin/shownav/rule ', ' Expeditedssl ', ' 2 '); INSERT into ' Wifi_admin_nav ' VALUES (' 8 ', ' 4 ', ' User Group Management ') , ' Admin/rule/group ', ' ', ' 2 '); INSERT into ' Wifi_admin_nav ' VALUES (' 9 ', ' 4 ', ' admin list ', ' admin/rule/admin_user_list ', ', ' 3 '); INSERT into ' wifi_admIn_nav ' VALUES (' 16 ', ' 0 ', ' member Management ', ' admin/shownav/', ' users ', ' 4 '); INSERT into ' Wifi_admin_nav ' values (' 17 ', ' 16 ', ' membership list ') , ' Admin/user/index ', ', ', null); INSERT into ' Wifi_admin_nav ' VALUES (' 36 ', ' 0 ', ' article Management ', ' admin/shownav/posts ', ' th ', ' 6 '); INSERT into ' Wifi_admin_nav ' VALUES (' 37 ', ' 36 ', ' article List ', ' Admin/posts/index ', ' ", null); B. user Group table CREATE table ' Wifi_auth_gro Up ' (' id ' int (one) unsigned not null auto_increment, ' title ' char (+) ' NOT null DEFAULT ' ', ' status ' tinyint (1) Not NUL L default ' 1 ', ' Rules ' text COMMENT ' rule id ', PRIMARY KEY (' id ')) engine=myisam auto_increment=8 default Charset=utf8 COMM ent= ' user groups table '; INSERT into ' Wifi_auth_group ' VALUES (' 1 ', ' super admin ', ' 1 ', ' 6,96,20,1,2,3,4,5,64,126,21,7,8,9,10,11,12,13,14,15,16,123,124,125,19,104,105,106,107,108,109,110,111,112,117 ') INSERT into ' wifi_auth_group ' VALUES (' 2 ', ' Product manager ', ' 1 ', ' 6,96,1,2,3,4,56,57,60,61,63,71,72,65,67,74,75,66,68,69,70,73,77,78,82,83,88,89,90,99,91,92,97,98,104,105,106,107,108,118 , 109,110,111,112,117,113,114 ');Nsert into ' wifi_auth_group ' VALUES (' 4 ', ' article editor ', ' 1 ', ' 6,96,57,60,61,63,71,72,65,67,74,75,66,68,69,73,79,80,78,82,83,88,89,90,99,100,97,98,104,105,106,107,108,118,109,110,111,1 12,117,113,114 '); c. User and User Group Relationship table CREATE table ' wifi_auth_group_access ' (' uid ' int (one) unsigned not NULL COMMENT ' user ID ', ' gro up_id ' int (one) unsigned not NULL COMMENT ' user group ID ', UNIQUE key ' uid_group_id ' (' uid ', ' group_id '), key ' uid ' (' uid '), key ' group_id ' (' group_id ')) Engine=myisam DEFAULT Charset=utf8 comment= ' user Group Schedule '; INSERT into ' wifi_auth_group_access ' VALUES (' 1 ', ' 1 '); insert INTO ' wifi_auth_group_access ' values (' 211 ', ' 2 '); insert INTO ' wifi_auth_group_access ' values (' 203 ', ' 4 ');d. Permission table CREATE table ' Wifi_auth_rule ' (' id ' int (one) unsigned not NULL auto_increment, ' pid ' int (one) unsigned NO T null default ' 0 ' COMMENT ' parent id ', ' name ' char (a) NOT null default ' COMMENT ' rule uniquely identifies ', ' title ' char (NOT NULL DEFAU) LT ' COMMENT ' rules Chinese name ', ' status ' tinyint (1) Not NULL DEFAULT ' 1 ' COMMENT ' state: 1 normal, 0 disabled ', ' type ' TiNyint (1) unsigned NOT null default ' 1 ', ' condition ' char (+) NOT null default ' COMMENT ' regular expression, NULL indicates existence on validation, not NULL for conditional validation ', PRIMARY key (' id '), UNIQUE key ' name ' (' name ')) Engine=myisam auto_increment=127 DEFAULT charset=utf8 comment= ' Rules table '; inse  RT into ' wifi_auth_rule ' values (' 1 ', ' ' + ', ' admin/shownav/nav ', ' Menu Management ', ' 1 ', ' 1 ', '); INSERT into ' wifi_auth_rule ' values (' 2 ', ' 1 ', ' admin/nav/index ', ' menu list ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 3 ', ' 1 ', ' admin/nav/add ', ' add menu ', ' 1 ', ' 1 ', '); insert INTO ' Wifi_auth_rule ' VALUES (' 4 ', ' 1 ', ' admin/nav/edit ', ' modify Menu ', ' 1 ', ' 1 ', '); insert INTO ' wifi_ Auth_rule ' VALUES (' 5 ', ' 1 ', ' admin/nav/delete ', ' delete menu ', ' 1 ', ' 1 ', '); INSERT into ' wifi_auth_rule ' values (' + ', ' 0 ', ' Ad Min/shownav/rule ', ' Privilege control ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 7 ', ' + ', ' admin/rule/index ', ' Rights Management ', ' 1 ', ' 1 ', '); insert INTO ' Wifi_auth_rule ' VALUES (' 8 ', ' 7 ', ' Admin/rule/add ', ' Add Permissions ', ' 1 ', ' 1 ', '); insert INTO ' Wifi_auth_rul E ' VALUES (' 9 ', ' 7 ', ' AdMin/rule/edit ', ' Modify Permissions ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' Ten ', ' 7 ', ' admin/rule/delete ', ' Delete permissions ', ' 1 ', ' 1 ', '); insert INTO ' Wifi_auth_rule ' VALUES (' one ', ' + ', ' admin/rule/group ', ' User group Management ', ' 1 ', ' 1 ', '); insert INTO ' wifi_auth_ Rule ' VALUES (' n ', ' One ', ' admin/rule/add_group ', ' Add user group ', ' 1 ', ' 1 ', ') ', INSERT into ' wifi_auth_rule ' values (' 13 ', ' 11 ', ' Admin/rule/edit_group ', ' Modify user groups ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' + ', ' one ', ' Admin/rule/delete_  Group ', ' Delete user group ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' ' ", ' one ', ' admin/rule/rule_group ', ' Assign permissions ', ' 1 ', ' 1 ', Insert INTO ' wifi_auth_rule ' VALUES (' + ', ' one ', ' admin/rule/check_user ', ' Add member ', ' 1 ', ' 1 ', '); insert INTO ' Wifi_aut H_rule ' VALUES (' n ', ' + ', ' admin/rule/admin_user_list ', ' admin list ', ' 1 ', ' 1 ', '); INSERT into ' wifi_auth_rule ' values (' 20 ' , ' 0 ', ' admin/shownav/config ', ' system set ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 6 ', ' 0 ', ' admin/index/index ', ' Background home ', ' 1 ', ' 1 ', '); INSERT inTo ' Wifi_auth_rule ' values (' 1 ', ' admin/nav/order ', ' menu sort ', ' 1 ', ' 1 ', '); INSERT into ' wifi_auth_rule ' values (' 96 ',  ' 6 ', ' admin/index/welcome ', ' Welcome interface ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 104 ', ' 0 ', ' admin/shownav/posts ', ' Article Management ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 104 ', ' admin/posts/index ', ' article List ', ' 1 ', ' 1 ', '); inser T into ' wifi_auth_rule ' VALUES (' 106 ', ' the ' ", ' admin/posts/add_posts ', ' Add article ', ' 1 ', ' 1 ', '); INSERT into ' wifi_auth_rule ' VALUES (' 107 ', ' a ', ' admin/posts/edit_posts ', ' modified article ', ' 1 ', ' 1 ', '); INSERT into ' wifi_auth_rule ' values (' 108 ', ' 105 ', ' Admin/posts/delete_posts ', ' delete article ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 109 ', ' 104 ', ' admin/posts/ Category_list ', ' categorical list ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' A ', ' 109 ', ' admin/posts/add_category ', ' Add points Class ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 111 ', ' 109 ', ' admin/posts/edit_category ', ' Modify Classification ', ' 1 ', ' 1 ', '); SERT into ' wifi_auth_rule ' VALUES(' 109 ', ' admin/posts/delete_category ', ' delete category ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 117 ', ' 109 ', ' Ad Min/posts/order_category ', ' sorted sort ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 123 ', ' One ', ' Admin/rule/add_user_  To_group ', ' Set as Administrator ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' 124 ', ' one ', ' admin/rule/add_admin ', ' Add admin ', ' 1 ', ' 1 ', '); INSERT into ' Wifi_auth_rule ' VALUES (' A ', ' ' One ', ' admin/rule/edit_admin ', ' modify admin ', ' 1 ', ' 1 ', ');

Implement step three: Add in Project Profile config.php

' auth_config ' = =array        (true// authentication switch        //  authentication mode, 1 is always certified; 2 is a login certificate.         // User Group table        // User/user Group Relationship table        / permissions Table        ' auth_user ' = ' wifi_admin '// user table    )

Implementation step Four: Create the common controller, define the _initialize method, and detect the current user's permissions (All rights-authenticated classes need to inherit this class)

<? PHP class extends action{    publicfunction  _initialize () {       import (' ORG. Util.auth '); // Load Class Library       $auth=new  auth ();        if (! $auth->check (module_name. ' -‘. Action_name,session (' uid ')){            $this->error (' You do not have permission ')       ;     }}}

Implementation Step Five:

A. Menu management: Admin_nav in Demo

B. Rights Management: Auth_rule in Demo

C. User group management: ibid.

D. User management: Ibid.

Demo needs Dabigatran 172020040 look for me to

TP thinkphp Rights Management function

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.