Home  >  Article  >  PHP Framework  >  How to use Auth authentication authority operation in ThinkPHP6?

How to use Auth authentication authority operation in ThinkPHP6?

WBOY
WBOYOriginal
2023-06-12 08:23:152193browse

With the development of Internet applications, permission management has become an indispensable part of application development. During the development process, we need to assign different permissions to different users to achieve data security and operation permission control. The Auth authentication permission operation in the ThinkPHP6 framework provides us with a simple and easy-to-use solution.

What is Auth authentication permission operation?

Auth is a permission management plug-in in the ThinkPHP6 framework. It implements a set of efficient and controllable permission management mechanisms by defining and managing roles, users, permissions, rules, etc. Its advantages include simple operation, strong adaptability, high controllability, and good scalability. It is widely used in enterprise-level applications.

Here we explain how to use Auth to implement permission management in ThinkPHP6.

Authentication authority mechanism

Before using Auth, we need to understand its authentication authority mechanism. Its roles, users, permissions, rules and other concepts are defined as follows:

  1. Role: refers to a permission unit that assigns permissions to one or more users.
  2. User: An individual in the system, which can be a real user or a system account.
  3. Permission: refers to the operation behavior authorized for users (or roles), similar to the permission settings in the database.
  4. Rule: refers to setting some restrictions for permissions, such as time period restrictions, restricted IPs, etc.

Specifically, we can define some common permissions as follows:

  1. Menu permissions: refers to access control of a certain menu of the application, which allows different The user sees different menu items.
  2. Operation permissions: refers to access control for a certain operation behavior of the application, which allows different users to have different operation permissions.
  3. Data permissions: refers to controlling access to a certain data in the application, which can restrict users to only see or modify the data associated with them.
  4. Field permissions: refers to access control of a certain field in the application, which can restrict users to only see or modify the fields they are associated with.

Authentication permission operation implementation

With the basic concept of authentication permission mechanism, we can start the Auth authentication permission operation in ThinkPHP6.

  1. Install the Auth plug-in

First we need to ensure that the Auth plug-in has been installed in the application, which can be installed through the following command:

composer require topthink/think-auth
  1. Introduction Auth middleware

Using Auth in an application requires the use of middleware, which is configured in config/middleware.php:

return [
    //全局中间件列表
    'global' => [
            hinkmiddlewareSessionInit::class,
            hinkmiddlewareLoadLangPack::class,
            hinkmiddlewareCheckRequestCache::class,
            hinkmiddlewareSendFile::class,
    ],
    //中间件别名
    'alias' => [
        'auth' =>     hinkmiddlewareAuth::class,
    ],
];
  1. Define permission rules

When initializing the application, we need to define some permission rules, which will be used for permission authentication, for example:

use thinkacadeAuth;

//定义规则
Auth::rule('admin.user/index', 'checkAdmin');
Auth::rule('admin.user/add', 'checkAdmin');
Auth::rule('admin.user/edit', 'checkAdmin');
Auth::rule('admin.user/del', 'checkAdmin');

In the above code, we define basic user management permission rules, Without these permissions, the corresponding controller operations cannot be accessed.

  1. Role and user authorization

We need to authorize roles and users in the application, which can be authorized during application initialization:

//定义角色
Auth::group('admin', function () {
    //设置角色权限
    Auth::setRule([
        'admin.user/index',
        'admin.user/add',
        'admin.user/edit',
        'admin.user/del',
    ]);
});

//定义用户并授权
Auth::user('admin', function () {
    Auth::addToGroup('admin')//添加角色
        ->addPermission(['admin.user/add'])//添加权限
        ->removePermission(['admin.user/del']);//移除权限
});

In In the above code, we define a role named admin and set the corresponding permission rules. Then we defined a user named admin, who has the admin role, authorized the admin.user/add permission, and removed the admin.user/del permission.

  1. Authentication

Before we perform permission authentication, we can first judge roles, users, permissions, etc. in the controller:

use thinkacadeAuth;

class User extends Controller
{
    //进行认证
    public function index()
    {
        //验证用户是否登录,没有登录则跳转到登录页面
        if (!Auth::check()) {
            return redirect('admin/auth/login');
        }

        //验证是否为超级管理员,是则直接放行
        if (Auth::isSuperAdmin()) {
            return $this->view->assign('username', Auth::getUser()['username'])->fetch();
        }

        //验证是否为管理员角色,是则验证权限,否则跳转到其他页面
        if (Auth::group('admin')->check()) {
            if (Auth::check('admin.user/index')) {
                return $this->view->assign('username', Auth::getUser()['username'])->fetch();
            } else {
                return redirect('admin/index/model_error');
            }
        } else {
            return redirect('admin/index/role_error');
        }
    }
}

In the above code, we perform user login verification, super administrator verification, role and permission verification, etc., and finally return to the corresponding page or jump.

Summary

Through the above operations, we can use the Auth authentication permission operation in the ThinkPHP6 framework to achieve simple, efficient, and controllable application permission management to ensure data security and operation permission control. During use, in order to ensure the security of the application, we need to carefully set various permission rules, limit the corresponding user and role permissions, and improve the scalability and controllability of the application.

The above is the detailed content of How to use Auth authentication authority operation in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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