Provides a variety of official and user-released code examples. For code reference, you are welcome to exchange and learn to enhance the Auth function and Auth structure. The detailed function code is manually implemented by TPer.
// Role table
Create table 'Task _ auth_group '(
'Id' mediumint (8) unsigned not null AUTO_INCREMENT, // role id
'Title' char (100) not null default '', // role name
'Status' tinyint (1) not null default '1', // role status 1: enabled 2: Disabled
'Rules' char (80) not null default '', // role-owned permission rule id multi-permission rule id used, connected
Primary key ('id') // set as the PRIMARY KEY
) ENGINE = MyISAM AUTO_INCREMENT = 1 default charset = utf8
// User permission table
Create table 'Task _ auth_group_access '(
'Uid' mediumint (8) unsigned not null, // corresponding user ID
'Group _ id' mediumint (8) unsigned not null, // corresponding role id
Unique key 'uid _ group_id '('uid', 'Group _ id'), // UNIQUE index group
KEY 'uid' ('uid'), // Common Index
KEY 'group _ id' ('group _ id') // Common Index
) ENGINE = MyISAM default charset = utf8
// The permission rule table supports Condition rules.
Create table 'Task _ auth_rule '(
'Id' mediumint (8) unsigned not null AUTO_INCREMENT, // auto-increment rule id
'Name' char (80) not null default '', // permission rule ID
'Title' char (20) not null default '', // permission rule name
'Type' tinyint (1) not null default '1', // permission rule type 1: url 2: menu (I don't know what this is)
'Status' tinyint (1) not null default '1', // permission rule status 1: enabled 2: Disabled
'Condition 'Char (100) not null default '', // permission rule condition
'Mid 'int (10) unsigned not null default '1', // permission module id
'Level' tinyint (1) unsigned not null default '0', // menu level 0: NOT menu 1: Left menu 2: top menu 3: Bottom menu
Primary key ('id'), // set the primary key to id
Unique key 'name' ('name') // UNIQUE index
) ENGINE = MyISAM AUTO_INCREMENT = 7 default charset = utf8
// Module table
Create table 'Task _ Les '(
'Id' int (10) unsigned not null AUTO_INCREMENT, // auto-increment module id
'Name' varchar (20) not null, // module ID
'Title' varchar (20) not null, // Module name
'Level' tinyint (1) unsigned not null default '1', // Module level 1: Application 2: Controller
'Pid 'tinyint (1) unsigned not null default '0', // module parent id 0: Application 1: Controller
Primary key ('id') // set the primary key id
) ENGINE = MyISAM AUTO_INCREMENT = 8 default charset = utf8
// User table
Create table 'Task _ user '(
'Id' int (10) unsigned not null AUTO_INCREMENT COMMENT 'user id ',
'Username' varchar (20) not null comment 'user login name ',
'Password' varchar (32) not null comment 'user logon password ',
'Name' varchar (20) not null default '0' COMMENT 'user nickname ',
'Qq' varchar (15) not null default '0' COMMENT 'user qq ',
'Tel 'varchar (11) not null default '0' COMMENT 'user phone ',
'Logindate' int (10) unsigned not null default '0' comment' logon time ',
'Createdate' int (10) unsigned not null default '0' comment' User Creation time ',
'Loginnums' int (10) unsigned not null default '0' COMMENT 'user logon times ',
Primary key ('id '),
Unique key 'username' ('username ')
) ENGINE = MyISAM AUTO_INCREMENT = 3 default charset = utf8
ThinkPHP public controller code
Namespace Common \ Controller;
Use Think \ Controller;
Use Think \ Auth;
Class CommonController extends Controller {
Protected function _ initialize (){
$ Sess_auth = session ('auth ');
// You have not logged on to the background
If (! $ Sess_auth ){
$ This-> error ('Sorry, you have not logged on! ', U ('login/Index '));
}
// Obtain the left menu
$ This-> assign ("menu", menu (1 ));
// Super administrator verification free
If ($ sess_auth ['id'] = C ("Administrator ")){
Return true;
}
$ Auth = new Auth ();
// Check Common User Permissions
If (! $ Auth-> check (MODULE_NAME. '/'. CONTROLLER_NAME. '/'. ACTION_NAME, $ sess_auth ['id']) {
$ This-> error ("no permission ");
}
}
}
ThinkPHP public function File
// Obtain menu data
Function menu ($ level ){
// Obtain the menu from a non-Super Administrator
If (session ('auth ') ['id']! = C ("Administrator ")){
$ Auth = new \ Think \ Auth;
$ Tmp_rules = $ auth-> getGroups (session ("auth") ['id']);
$ Rules = explode (",", $ tmp_rules [0] ['rules']);
Foreach ($ rules as $ k ){
$ Rule = M ("auth_rule")-> find ($ rules [$ k]);
If ($ rule ['level'] = $ level ){
$ Menu [] = $ rule;
}
}
} Else {
$ Rules = M ("auth_rule")-> where (array ('level' => $ level)-> field ("id, level")-> select ();
Foreach ($ rules as $ k => $ v ){
$ Rule = M ("auth_rule")-> find ($ v ['id']);
If ($ rule ['level'] = $ level ){
$ Menu [] = $ rule;
}
}
}
Return $ menu;
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB