Yotaku Development Log (1), Yotaku Development Log _php Tutorial

Source: Internet
Author: User

Yotaku Development Log (1), Yotaku development log


2015-12-18 21:17:46

Looking at the thinkphp framework for several days in a row, we currently see role-based user access rights control.

The relevant 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 (one) not null auto_increment,    ' mg_name ' varchar (+) NOT NULL,    ' Mg_ PWD ' varchar (+) NOT NULL,     ' mg_time ' int (ten) 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 (method of operation) Auth_path (full path) auth_level (Permission level)
100 Product Center 0 '' '' 100 0
101 Product Show 100 Managercontroller Show 100-101 1
CREATE TABLE ' Sw_auth ' (    ' auth_id ' smallint (6) unsigned not NULL auto_increment,    ' auth_name ' varchar (a) NOT NULL COMMENT ' permission name ',    ' auth_pid ' smallint (6) unsigned NOT NULL COMMENT ' parent ID ',    ' auth_c ' varchar (+) NOT null DEFAULT ' C ' Omment ' controller ',    ' auth_a ' varchar (+) NOT null DEFAULT ' COMMENT ' action method ',    ' auth_path ' varchar (+) 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 Station Master 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 (a) NOT NULL COMMENT ' role name ',    ' role_auth_ids ' varchar (+) not NULL DEFAULT ' COMMENT ' Permissions id,1,3,.. ',    ' role_auth_ac ' text COMM ENT ' 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, new products, classification management, sub-category Management) advanced Management (user message, guestbook, product ordering, file management) system management (Basic settings, style management, home settings, Administrator list)

top-level permissionsINSERT into Sw_auth values (100, ' Product center ', 0, ' ', ' ', 100,0); insert into Sw_auth values (101, ' Advanced Management ', 0, ', ', ', 101,0); Insert int o Sw_auth values (102, ' System Management ', 0, ', ', ', 102,0); insert into Sw_auth values (103, ' Rights Management ', 0, ', ', 103,0);Secondary PermissionsINSERT into Sw_auth values (104, ' product display ', ' Goods ', ' Show ', ' 100-104 ', 1); INSERT into Sw_auth values (105, ' latest product ', "," Goo DS ', ' showlist ', ' 100-105 ', 1); INSERT into Sw_auth values (106, ' Category Management ', ' + ', ' Goods ', ' Cate ', ' 100-106 ', 1); INSERT INTO Sw_ Auth VALUES (107, ' User message ', 101, ' Goods ', ' words ', ' 101-107 ', 1); INSERT into Sw_auth values (108, ' Guestbook ', 101, ' Goods ', ' Wordsboo K ', ' 101-108 ', 1); INSERT into Sw_auth values (109, ' Basic Settings ', 102, ' Goods ', ' Set ', ' 102-109 ', 1); INSERT into Sw_auth values (110, ' Style management ', 102, ' Goods ', ' css ', ' 102-110 ', 1 '; INSERT into Sw_auth values (111, ' user list ', 103, ' Goods ', ' userlist ', ' 103-111 ', 1); INSERT into Sw_auth values (112, ' character management ', 103, ' Goods ', ' role ', ' 103-112 ', 1); INSERT into Sw_auth values (113, ' Permissions list ', 103, ' Goo DS ', ' auth ', ' 103-113 ', 1);

2. Role data

Sw_role Station Master All rights (103,104,105,106,107,108,109) Admin partial permissions (104,105,109) moderator partial permissions (103,108)

role INSERT INTO sw_role values (10, ' Station master ', ' 100,101,102,103,104,105,106,107,108,109,110,111,112,113 ', ' Goods-show,goods-showlist,goods-cate,goods-words,goods-wordsbook,goods-set,goods-css '); insert into sw_role values (11, ' admin ', ' 100,102,104,105,109 ', ' goods-showlist,goods-cate,goods-css '); insert into sw_role values (12, ' moderator ', ' 100,101,103,106,108,113 ', ' goods-show,goods-set ');

3. Process description

The index controller obtains the user's role ID, and then obtains the role permission to judge whether to show the data index controller--->left method--->left.html template index controller

//(1) Get the recorded information by user ID
        $MG _id = Session (' admin_id ');        
//d (' manager ') instantiates a manager's model object
        $manager _info = D (' manager ')->find ($mg _id);        $role _id = $manager _info[' mg_role_id ');        
//(2) Obtain the information of the record according to ROLE_ID
        $role _info = D (' role ')->find ($role _id);        $auth _ids = $role _info[' role_auth_ids ');        
//(3) obtain specific authority according to $auth_ids
        $auth _infoa = D (' auth ')->where ("Auth_level=0 and auth_id in ($auth _ids)")->select ();
//Parent
        $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 the 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}
    {$v. auth_id} > {foreach $auth _infob as $k 2=> $v 2} {if $v 2.auth_pid = = $v. auth_id}                    {/if}               {/foreach}                     
{$smarty. Const.admin_img_url} /menu_icon.gif "Width=9>{$smarty. const.__module__} /{$v 2.auth_c}/{$v 2.auth_a}"Target=right> {$v 2.auth_name}          
{/foreach}  

http://www.bkjia.com/PHPjc/1081947.html www.bkjia.com true http://www.bkjia.com/PHPjc/1081947.html techarticle Yotaku Development Log (1), Yotaku Development log 2015-12-1821:17:46 continuously looked at the thinkphp framework for several days, currently see role-based user access control. The relevant code is as follows: Number ...

  • Related Article

    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.