Home> PHP Framework> ThinkPHP> body text

About the installation and use of Thinkphp 6.0 authentication plug-in Think-authz

藏色散人
Release: 2020-11-10 14:35:38
forward
3315 people have browsed it

The following is theThinkPHPtutorial column to introduce to you the Thinkphp 6.0 authentication plug-in Think-authz, I hope it will be helpful to friends in need!

Installation

This extension requires PHP 7.1 and ThinkPHP 6.0. For TP 5.1, please use Think-Casbin.

UsecomposerInstallation:

composer require casbin/think-authz
Copy after login

Register the service and add it to the application’s global public fileservice.php:

return [ // ... tauthz\TauthzService::class,];
Copy after login

Publish configuration files and database migrations Files:

php think tauthz:publish
Copy after login

This will automatically generate theconfig/tauthz-rbac-model.confandconfig/tauthz.phpfiles.

Execute the migration tool (Ensure the database configuration information is correct):

php think migrate:run
Copy after login

This will create a table namedrules.

Usage

Quick Start

After successful installation, you can use it like this:

use tauthz\facade\Enforcer; // adds permissions to a user Enforcer::addPermissionForUser('eve', 'articles', 'read'); // adds a role for a user. Enforcer::addRoleForUser('eve', 'writer'); // adds permissions to a rule Enforcer::addPolicy('writer', 'articles','edit');
Copy after login

You can Check if a user has a certain permission:

// to check if a user has permission if (Enforcer::enforce("eve", "articles", "edit")) { // permit eve to edit articles} else { // deny the request, show an error}
Copy after login

Use Enforcer Api

It provides a very richAPIto facilitate ## Various operations of #Policy:

Get all roles:

Enforcer::getAllRoles(); // ['writer', 'reader']
Copy after login
Get the authorization rules of all roles:

Enforcer::getPolicy();
Copy after login
Get all roles of a user:

Enforcer::getRolesForUser('eve'); // ['writer']
Copy after login
Get all users of a certain role:

Enforcer::getUsersForRole('writer'); // ['eve']
Copy after login
Determine whether the user has a certain role:

Enforcer::hasRoleForUser('eve', 'writer'); // true or false
Copy after login
Add a role to the user:

Enforcer::addRoleForUser('eve', 'writer');
Copy after login
Give Permissions given to a user or role:

// to user Enforcer::addPermissionForUser('eve', 'articles', 'read'); // to role Enforcer::addPermissionForUser('writer', 'articles','edit');
Copy after login
Delete a user's role:

Enforcer::deleteRoleForUser('eve', 'writer');
Copy after login
Delete all roles of a user:

Enforcer::deleteRolesForUser('eve');
Copy after login
Delete a single role:

Enforcer::deleteRole('writer');
Copy after login
Delete a permission:

Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
Copy after login
Delete a permission for a user or role:

Enforcer::deletePermissionForUser('eve', 'articles', 'read');
Copy after login
Delete all permissions for a user or role:

// to user Enforcer::deletePermissionsForUser('eve'); // to role Enforcer::deletePermissionsForUser('writer');
Copy after login
Get the user Or all permissions of a role:

Enforcer::getPermissionsForUser('eve'); // return array
Copy after login
Determine whether a user has a certain permission

Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false
Copy after login
More

APIRefer to Casbin API (https://casbin.org/ docs/en/management-api).

Using middleware

This extension package comes with a

\tauthz\middleware\Basic::classmiddleware:

Route::get('news/:id','News/Show') ->middleware(\tauthz\middleware\Basic::class, ['news', 'read']);
Copy after login

The above is the detailed content of About the installation and use of Thinkphp 6.0 authentication plug-in Think-authz. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!