Home>Article>PHP Framework> About the installation and use of Thinkphp 6.0 authentication plug-in Think-authz
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!
This extension requires PHP 7.1 and ThinkPHP 6.0. For TP 5.1, please use Think-Casbin.
Usecomposer
Installation:
composer require casbin/think-authz
Register the service and add it to the application’s global public fileservice.php
:
return [ // ... tauthz\TauthzService::class,];
Publish configuration files and database migrations Files:
php think tauthz:publish
This will automatically generate theconfig/tauthz-rbac-model.conf
andconfig/tauthz.php
files.
Execute the migration tool (Ensure the database configuration information is correct):
php think migrate:run
This will create a table namedrules
.
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');
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}
It provides a very richAPI
to facilitate ## Various operations of #Policy:
Enforcer::getAllRoles(); // ['writer', 'reader']Get the authorization rules of all roles:
Enforcer::getPolicy();Get all roles of a user:
Enforcer::getRolesForUser('eve'); // ['writer']Get all users of a certain role:
Enforcer::getUsersForRole('writer'); // ['eve']Determine whether the user has a certain role:
Enforcer::hasRoleForUser('eve', 'writer'); // true or falseAdd a role to the user:
Enforcer::addRoleForUser('eve', 'writer');Give Permissions given to a user or role:
// to user Enforcer::addPermissionForUser('eve', 'articles', 'read'); // to role Enforcer::addPermissionForUser('writer', 'articles','edit');Delete a user's role:
Enforcer::deleteRoleForUser('eve', 'writer');Delete all roles of a user:
Enforcer::deleteRolesForUser('eve');Delete a single role:
Enforcer::deleteRole('writer');Delete a permission:
Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).Delete a permission for a user or role:
Enforcer::deletePermissionForUser('eve', 'articles', 'read');Delete all permissions for a user or role:
// to user Enforcer::deletePermissionsForUser('eve'); // to role Enforcer::deletePermissionsForUser('writer');Get the user Or all permissions of a role:
Enforcer::getPermissionsForUser('eve'); // return arrayDetermine whether a user has a certain permission
Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or falseMore
APIRefer to Casbin API (https://casbin.org/ docs/en/management-api).
\tauthz\middleware\Basic::classmiddleware:
Route::get('news/:id','News/Show') ->middleware(\tauthz\middleware\Basic::class, ['news', 'read']);
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!