Auth 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!
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('uname')){ $this->error('请先登陆系统!','login/index'); } //获得当前页面的控制器 / 方法 $request=Request::instance(); $moudle=$request->module(); //获取当前控制器名称 $con=$request->controller(); //获取当前控制器名称 $action=$request->action(); //获取当前方法名称 $this->assign(array( 'con'=>$con, 'action'=>$action, )); $rules=$con.'/'.$action; //组合 控制器/方法 $auth=new Auth(); //实例化auth类 $notCheck=array('Index/index'); //都可以访问的页面 if(session('uid')!=1){ //不是超级管理员才进行权限判断 if(!in_array($rules,$notCheck)){ // 是否在开放权限里面 if(!$auth->check($rules,session('uid'))){ // 第一个参数 控制/方法 第二个参数:当前登陆会员的id $this->error('没有权限','index/index'); }; } } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to the error reported when deploying thinkphp5 in Pagoda: 1. Open the Pagoda server, install the php pathinfo extension and enable it; 2. Configure the ".access" file with the content "RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]”; 3. In website management, just enable thinkphp’s pseudo-static.

Solution to thinkphp5 url rewriting not working: 1. Check whether the mod_rewrite.so module is loaded in the httpd.conf configuration file; 2. Change None in AllowOverride None to All; 3. Modify the Apache configuration file .htaccess to "RewriteRule ^ (.*)$ index.php [L,E=PATH_INFO:$1]" and save it.

In modern software development, identity authentication is a very important security measure. Auth0 is a company that provides identity authentication services. It can help developers quickly implement multiple identity authentication methods (including OAuth2, OpenIDConnect, etc.) and provide safe and reliable authentication services. In this article, we will introduce how to use Auth0 for authentication in JavaAPI development. Step 1: Create an Auth0 account and register the application. First, we need to

Methods for thinkphp5 to obtain the requested URL: 1. Use the "$request = Request::instance();" method of the "\think\Request" class to obtain the current URL information; 2. Use the built-in helper function "$request-> url()" to obtain the complete URL address including the domain name.

thinkphp5 post cannot get a value because TP5 uses the strpos function to find the app/json string in the content-type value of the Header. The solution is to set the content-type value of the Header to app/json.

How to remove the thinkphp5 title bar icon: 1. Find the favicon.ico file under the thinkphp5 framework public; 2. Delete the file or choose another picture to rename it to favicon.ico and replace the original favicon.ico file.

Introduction Supabase is a self-proclaimed "open source Firebase alternative". I've been interested in working with Supbase for a while and thought I'd try using their authentication API to set up authentication for a Vue.js3 application. First of all, why should you use SupabaseAuth? The bottom line is that if you're using Supabase as your data store, (which has some pretty sweet benefits), SupabaseAuth is the only way you can manage access to that data. Secondly, although SupabaseAuth also has many different functions. User permissions without middleware (row-level security via Postgres)

Using Auth0 to implement PHP security verification Introduction: In modern web development, security verification is a crucial part. In order to protect users' privacy and data security, we need to take measures to ensure that only authorized users can access sensitive information or perform specific operations. Auth0 is a popular authentication and authorization platform that provides simple and powerful solutions to help us achieve secure verification. This article will introduce how to use Auth0 to implement PHP security verification and provide code
