Home PHP Framework ThinkPHP Auth permission setting and implementation in Thinkphp5

Auth permission setting and implementation in Thinkphp5

Jul 27, 2020 pm 01:23 PM
auth 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;);
               };
           }
        }




       }
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I get an error when deploying thinkphp5 in Pagoda? What should I do if I get an error when deploying thinkphp5 in Pagoda? Dec 19, 2022 am 11:04 AM

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.

What should I do if thinkphp5 url rewriting fails? What should I do if thinkphp5 url rewriting fails? Dec 12, 2022 am 09:31 AM

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.

Using Auth0 for authentication in Java API development Using Auth0 for authentication in Java API development Jun 18, 2023 pm 05:30 PM

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

How to get the requested URL in thinkphp5 How to get the requested URL in thinkphp5 Dec 20, 2022 am 09:48 AM

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.

What should I do if thinkphp5 post cannot get the value? What should I do if thinkphp5 post cannot get the value? Dec 06, 2022 am 09:29 AM

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 thinkphp5 title bar icon How to remove thinkphp5 title bar icon Dec 20, 2022 am 09:24 AM

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.

How to use Supabase Auth method in Vue3 How to use Supabase Auth method in Vue3 May 28, 2023 am 08:39 AM

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)

Implementing PHP security authentication using Auth0 Implementing PHP security authentication using Auth0 Jul 25, 2023 pm 02:09 PM

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

See all articles