Yotaku development log (1), yotaku Development Log _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Yotaku development log (1), yotaku development log. Yotaku development log (1): yotaku development log 2015-12-1821: 17: 46 views the ThinkPHP framework for several consecutive days. Currently, role-based user access permission control is displayed. The related code is as follows: development logs of yotaku (1) and development logs of yotaku

21:17:46

After reading the ThinkPHP framework for several days, we can see role-based user access control.

The related code is as follows:

Database

User table (administrator)

Mg_id Mg_name Mg_pwd Mg_time Mg_role_id
0 Creatint 123 2587413547 1
1 Yotaku 123 258744984 4
Creaate table 'SW _ manager' ('MG _ id' int (11) not null AUTO_INCREMENT, 'MG _ name' varchar (32) not null, 'MG _ pwd' varchar (32) not null, 'MG _ time' int (10) unsigned not null comment 'time', 'MG _ role_id 'tinyint (3) unsigned not null default '0' COMMENT 'role ID', primary key ('MG _ id') ENGINE = InnoDB AUTO_INCREMENT = 1 default charset = utf8;

Permission table

Auth_id (permission ID) Auth_name (permission name) Auth_pid (parent id) Auth_c (controller) Auth_a (Operation method) Auth_path (full path) Auth_level (permission level)
100 Product Center 0 '' '' 100 0
101 Product presentation 100 ManagerController Show 100-101 1
Create table 'SW _ auth' ('auth _ id' smallint (6) unsigned not null AUTO_INCREMENT, 'auth _ name' varchar (20) not null comment 'permission name ', 'auth _ pid 'smallint (6) unsigned not null comment 'parent ID', 'auth _ c' varchar (32) not null default ''comment 'controller ', 'auth _ a' varchar (32) not null default ''' COMMENT 'Operation method', 'auth _ path' varchar (32) not null comment 'full path ', 'auth _ level' tinyint (4) not null default '0' comment' level', primary key ('auth _ id ')) ENGING-InnoDB AUTO_INCREMENT = 1 default charset = utf8;

Role table

Role_id Role_name Role_auth_ids Role_auth_ac
0 Site owner 1, 3, 9 Operator-controller, operator-controller ,...
1 Senior Administrator 1, 2, 3, 9, 12 Operator-controller, operator-controller ,...
Create table 'SW _ role' ('role _ id' smallint (6) unsigned not null AUTO_INCREMENT, 'role _ name' varchar (20) not null comment 'role name ', 'role _ auth_ids 'varchar (128) not null default ''comment' permission id, 1, 3 ,.. ', 'role _ auth_ac' text COMMENT' Controller 2-Operation 3, Controller 1-Operation 6 ,... ', primary key ('role _ id') ENGINE = InnoDB AUTO_INCREMENT = 1 default charset = utf8;

Data simulation:

1. permission data

Product Center (product display, latest product, classification management, subclass management) Advanced Management (user message, message book, product order, file management) system management (basic settings, style management, homepage settings, administrator list)

Top-level permission insert into sw_auth values (100, 'product Center', 0, '','', 101, 0); insert into sw_auth values (, 'advanced management', 0, '','', 102); insert into sw_auth values (103, 'system management', 0, '','',); insert into sw_auth values, 'permission management', 0, '','', 104); insert into sw_auth values (100, 'product display', 'goods', 'show ', '2014-100 ', 1); insert into sw_auth values (104, 'newest product', 105, 'Goods', 'showlist', '2014-100 ', 1 ); insert into sw_auth values (106, 'classification management', 100, 'goods', 'Cate', '2017-123', 1); insert into sw_auth values (100, 'user message', 101, 'goods', 'Word', '2017-100', 1); insert into sw_auth values (101, 'guestbook ', 107, 'goods', 'wordsbook', '2017-101 ', 1); insert into sw_auth values (108, 'Basic settings', 109, 'goods', 'set ', '2014-102 ', 1); insert into sw_auth values (109, 'style management', 110, 'Goods', 'css ', '2014-102', 1 ); insert into sw_auth values (111, 'User list', 103, 'goods', 'userlist', '2017-103 ', 1); insert into sw_auth values (111, 'role management', 103, 'goods', 'Role', '2017-100', 1); insert into sw_auth values (103, 'permission list', 112, 'goods', 'auth', '2017-103 ', 1 );

2. Role data

All permissions of sw_role site Master (103,104,105,106,107,108,109) administrator partial permissions (104,105,109) moderator partial permissions (103,108)

Insert into sw_role values (10, 'site ow', '000000', 'goods-show, Goods-showlist, Goods-cate, Goods-words, Goods-wordsbook, goods-set, Goods-css '); insert into sw_role values (11, 'admin', '2016', 'goods-showlist, Goods-cate, Goods-css '); insert into sw_role values (12, 'moderator ', '20160301', 'goods-show, Goods-set ');

3. Process Description

Obtain the user's role id in the Index controller, and then obtain the role permission to determine whether to display the data Index controller ---> left method ---> left.html template Index controller

// (1) obtain the record information based on the user ID
        $mg_id = session('admin_id');        
// D ('manager') instantiates a Manager Model object.
        $manager_info = D('Manager')->find($mg_id);        $role_id = $manager_info['mg_role_id'];        
// (2) obtain the record information based on role_id
        $role_info = D('Role')->find($role_id);        $auth_ids = $role_info['role_auth_ids'];        
// (3) obtain specific permissions based on $ auth_ids
        $auth_infoA = D('Auth')->where("auth_level=0 and auth_id in($auth_ids)")->select();
// Parent level
        $auth_infoB = D('Auth')->where("auth_level=1 and auth_id in($auth_ids)")->select();
// Sub-level
        $this->assign('auth_infoA',$auth_infoA);        $this->assign('auth_infoB',$auth_infoB);        
// Upload to template
        $this->assign('auth_info',$auth_info);        $this->display();

4. Template left.html

{Foreach $ auth_infoA as $ k => $ v}     
Background = {$ smarty. const. ADMIN_IMG_URL}/menu_bt.jpg> {$ v. auth_id}) href = "javascript: void (0);" >{$ v. auth_name}
     {Foreach $ auth_infoB as $ k2 =>$ v2} {if $ v2.auth _ pid ==$ v. auth_id}            {/If} {/foreach}     
{$ Smarty. const. ADMIN_IMG_URL}/menu_icon.gif "width = 9>{$ Smarty. const. _ MODULE __}/{$ v2.auth _ c}/{$ v2.auth _ a} "target = right >{$ v2.auth _ name}
{/Foreach}

Role (1), yotaku development log 2015-12-1821: 17: 46 views the ThinkPHP framework for several consecutive days. Currently, role-based user access control is displayed. The related code is as follows: number...

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.