Auth Permissions Control

Source: Internet
Author: User

A Permissions Introduction
The so-called rights control, mostly in the management background to use. such as the Super Administrator login, will get all the action
Control Authority, Certification commissioner, only for members to do certification operations; Audit specialist, only the content of the audit, deletion, refinement
Operations, and so on. Then thinkphp provides a built-in Auth.class.php class to implement permission control
System, this class provides three tables: Think_auth_rule (authentication rules table), Think_auth_group (user group
Table), think_auth_group_access (User and Group correspondence table). And, of course, to combine the users you create
Table for the corresponding

Two Simple Login
First step: Create a Indexcontroller under the Admin module . Class. PHP (which is the default), create a
Build index method, output a word can.

 1  namespace Admin\controller;  2   Common\controller\authcontroller;  3   Class  Indexcontroller extends   Authcontroller { 5  public  function   index () { 6  echo  ' Backstage home! ' 

This is accessed via URL: http://localhost/demo39/admin/index/index .

Step Two: Create the Controller folder under the Common public module under the Weibo root directory, and inside
Create a authcontroller. class. PHP classes, which are used for permission control.

 namespace Common\controller;  use   Think\controller;  use   Think\auth;  class  Authcontroller extends   Controller { Span style= "color: #0000ff;"   >protected  function   _initialize () {   $auth  = new   Auth ();  if  (!->check () {  $this -      Error (' No permissions '  '); }    }  }

The method used here is fixed to: _initialize () . Due to Authcontroller. Class. PHP inherits the
Controller class, so the first step of the Index class is changed to inherit it. The check () function is called

Thinkphp/library/think/auth.class. PHP Internal check () function.

 1  namespace Admin\controller;  2   Common\controller\authcontroller;  3  class  indexcontroller extends   Authcontroller { 4  public  function   index () { 5  echo  ' background home! ' 

At this time, we re-visit the background home page, there is no permission. Mainly $auth->check () Verify that none
The reason for the passage of the law.

Step three: Create a weibo/admin/controller/logincontroller.class.php template for index.html .

Write the login code in weibo/admin/view/login/index.html :

<formMethod= "POST"Action= "{: U (' Index ')}">    <P>User name:<inputtype= "text"name= "User" /></P>    <P><inputtype= "Submit"value= "Login" /></P></form> 

Write code in weibo/admin/controller/logincontroller.class.php :

1<?PHP2 namespace Admin\controller;3  UseThink\controller;4 5 classLogincontrollerextendscontroller{6      Public functionindex () {7         if(is_post) {8             $login=Array();9             Switch(I (' user ',NULL,false)) {Ten                  Case' Admin ': One                   $login[' UID ']=1; A                   $login[' User ']= ' admin '; -                    Break; -                  Case' Test ': the                   $login[' UID ']=2; -                   $login[' User ']= ' test '; -                    Break; -                  Case' Guest ': +                   $login[' UID ']=3; -                   $login[' User ']= ' guest '; +                    Break; A                 default: at                   $this->error (' No login user exists '); -                 } -                 if(Count($login)) { -Session (' Auth ',$login); -                     $this->success (' Landing success ', U (' Index/index ')); -                 } in}Else { -                 $this-display (); to             } +         } -          Public functionlogout () { theSession (' [Destroy] '); *             $this->success (' exit Success ', U (' Login/index ')); $         }Panax Notoginseng}

The LoginController.class.php class that is logged in here can only inherit the Controller, otherwise it cannot
Run. Because inheriting the Authcontroller class is a class that requires permission control.

Fourth step: Improve the Authcontroller class of the authorization verification process.

1<?PHP2 namespace Common\controller;3  UseThink\controller;4  UseThink\auth;5 6 classAuthcontrollerextendsController {7     protected function_initialize () {8         $sess _auth=session (' auth ');9         if(!$sess _auth) {Ten             $this->error (' Illegal access, jumping login page ', U (' Login/index ')); One         } A         if($sess _auth[' UID ']==1) { -             return true; -         } the         $auth=NewAuth (); -         if(!$auth->check (module_name. ' /‘. Controller_name. ' /‘. Action_name,$sess _auth[' UID '])) { -             $this->error (' No Permissions ', U (' Login/logout '))); -         } +     } -}

Auth Permissions Control

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.