Home  >  Article  >  PHP Framework  >  Auth permission setting and implementation in Thinkphp5

Auth permission setting and implementation in Thinkphp5

藏色散人
藏色散人forward
2020-07-27 13:23:414245browse

The following tutorial column of thinkphp framework will introduce to you the auth permission setting and implementation in Thinkphp5. I hope it will be helpful to friends in need!

Auth permission setting and implementation in Thinkphp5

1. Download the auth class and place it in the directory: extend\auth\auth.php

2. Execute the SQL statement in the class , you can create 3 tables in the database auth_group (user group table) auth_rule (authority rule table) auth_group_access (user and user group association table)

3. I want to add a level relationship to the rules (similar to unlimited Extreme classification) Add 3 fields to auth_rule (authority rule table) pid (parent id, 0 is top level authority) level (level) sort (sort), as shown below

4, first create the administrator table yourself, such as the admin table, add, delete, modify and check to design it normally. The group field is the user group to which it belongs

5.auth_group You can also make your own additions, deletions, modifications, id, user group name, status (on or off), rules (corresponding to the id of the rule table)

6 .auth_rule rule table additions, deletions, modifications and checks can also be made by yourself, id, name (controller/method), title (rule name) status (on or off),

7. When adding a user, select the user group to which the user belongs, uid (corresponding to the user id) group_id (corresponding to the id of the user group to which the user belongs), so that they are associated

8. Complete the member login function, set session('id') after successful login, and store the current logged-in member ID in the session

9. The most important thing The first step is to use the auth class for verification, and use

<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use auth\Auth; //引入suth类
class Common extends Controller
{
    public function _initialize(){
      //初始化判断用户是否已经登陆
      if(!session(&#39;uname&#39;)){
          $this->error(&#39;请先登陆系统!&#39;,&#39;login/index&#39;);
        }


        //获得当前页面的控制器 / 方法
       $request=Request::instance();
       $moudle=$request->module(); //获取当前控制器名称
       $con=$request->controller(); //获取当前控制器名称
       $action=$request->action();  //获取当前方法名称
       $this->assign(array(
               &#39;con&#39;=>$con,
               &#39;action&#39;=>$action,
       ));

       $rules=$con.&#39;/&#39;.$action;  //组合  控制器/方法
       $auth=new Auth(); //实例化auth类
       $notCheck=array(&#39;Index/index&#39;);  //都可以访问的页面
       if(session(&#39;uid&#39;)!=1){  //不是超级管理员才进行权限判断
          if(!in_array($rules,$notCheck)){  // 是否在开放权限里面
              if(!$auth->check($rules,session(&#39;uid&#39;))){   // 第一个参数  控制/方法   第二个参数:当前登陆会员的id
                 $this->error(&#39;没有权限&#39;,&#39;index/index&#39;);
               };
           }
        }




       }

in the public page common.php for final display;

The above is the detailed content of Auth permission setting and implementation in Thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete