thinkphp Study Notes (19) How to implement Rights management RBAC
Method of Action
Let's take a look at the five-sheet data.
Tb_user: User table
Role Permission Group table
Role_user: The corresponding relationship table of the permission group and the user
Node table: Represents the URL classification in the current project, accurate to the control of each method
Access table: Represents different types of URLs that can be accessed by different groups of users
Parameters that need to be set in config:
Set RBAC parameters ' user_auth_on ' =>true, ' User_auth_type ' =>1,//default authentication type 1 Login authentication 2 Even if authentication ' user_auth_key ' = ' authId ',// User authentication session tag ' admin_auth_key ' = ' Administrator ',//admin ID ' user_auth_model ' + ' user ',//default validation data table model ' Auth_pwd_ ENCODER ' + ' MD5 ',//user authentication password encryption method ' User_auth_gateway ' = '/public/login ',//default authentication gateway ' not_auth_module ' = ' public ', The default requires no authentication module ' require_auth_module ' = ', '//default needs authentication module ' not_auth_action ' = ', '//Default no Authentication operation ' require_auth_action ' = ',///The default requires authentication operation ' guest_auth_on ' and false,//whether to turn on visitor authorized access ' guest_auth_id ' + 0,//Tour Guest User ID (you can set a visitor group with ID 0 in the database group) ' Show_run_time ' =>true,//run time display ' Show_adv_time ' =>true,//show verbose run time ' show_db_ Times ' =>true,//shows database queries and writes ' Show_cache_times ' =>true,//show cache operations ' Show_use_mem ' =>true,//show memory overhead ' db_like_ Fields ' = ' title|remark ', ' rbac_role_table ' = ' think_role ', ' rbac_user_table ' = ' think_role_user ', ' rbac_ ' Access_table ' = ' think_access ', ' rbac_node_table ' = ' think_node ',
Public access methods that need to be set in Publicaction, (named configuration based on Not_auth_module in configuration)
Login (); Public Function Login () {$this->display (); }//can go to the example to copy public function Checklogin () {if (Empty ($_post[' username ')) {$this->error (' Account Error! ');} ElseIf (Empty ($_post[' password ')) {$this->error (' Password must be! //}elseif (Empty ($_post[' verify ')) {//$this->error (' Verification code must! ');} Generate authentication Condition $map = Array ();//Support Login $map[' username ']= $_post[' username '];//$map ["status"]=arr] with bind account Ay (' GT ', 0);//if ($_session[' verify ')! = MD5 ($_post[' verify ')) {//$this->error (' CAPTCHA Error! ');//}import (' ORG. Util.rbac '); $authInfo = Rbac::authenticate ($map); Use the user name, password, and status to authenticate if (false = = = $authInfo) {$this->error (' account not present or disabled! '); }else {if ($authInfo [' password ']! = MD5 ($_post[' password ')) {$this->error (' Password wrong! '); } $_session[c (' User_auth_key ')]= $authInfo [' id '];//$_session[' email ']= $authInfo [' Email '];// $_session[' loginusername ']= $authInfo [' nickname '];//$_session[' lastlogintime ']= $authInfo [' last_login_time '];//$_session[' login_count ']= $authInfo [' Login_count ' ]; if ($authInfo [' username ']== ' admin ') {$_session[' Administrator ']=true; }//Save login Information//$User =m (' User '),//$ip =get_client_ip ();//$time =time ();//$data = Array ();//$data [' ID ']= $a uthinfo[' id '];//$data [' Last_login_time ']= $time;//$data [' Login_count ']=array (' exp ', ' login_count+1 ');//$data [' Last_login_ip ']= $ip;//$User->save ($data);//cache Access rbac::saveaccesslist (); $this->success (' Login successful! ');} }//You can go to the example to copy public function Loginout () {if (Isset ($_session[c (' User_auth_key '))) {unset ($_session[c (' User_au Th_key ')]; unset ($_session); Session_destroy (); $this->assign ("Jumpurl", __url__. /login/'); $this->success (' Log out success! '); }else {$this->error (' already logged out! '); }}}?>
Commonaction The filtering method for all URLs in the
Assign (' Jumpurl ', php_file. C (' User_auth_gateway '));} Hint error message $this->error (L (' _valid_access_ '));}}}} Public Function Verify () {//Import Verification code class//Mode one: Import (' ORG. Util.image ');//Mode two: @ Represents the Lib folder for the current project (you need to copy it yourself or write a new class yourself)//import (' @.org. Image ')//image::buildimageverify ();//Extension Modify/** * @param string $length * @param string $mode type (0 letters, 1 digits, 2 uppercase letters, 3 lowercase letters, 4 Text, 5 mixed) * @param string $type image format * @param string $width width * @param string $height Height * buildimageverify ($length =4, $mod e=1, $type = ' png ', $width =48, $height =22, $verifyName = ' verify ') */image::buildimageverify (5,5, ' png ', 80,22);//Chinese Verification Code ( 2.0 There is a problem: Msubstr error)//1. Modify the FUNCTION::MSUBSTR//2. Add font TTF needs to be placed under the Image sibling directory//extension can go to the class file to view//image::gbverify ();}}? >
Login under the public folder. Html
Insert Title here