search
HomePHP FrameworkThinkPHPAuth permission setting and implementation in Thinkphp5

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. If there is any infringement, please contact admin@php.cn delete
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools