如何使用Hyperf框架進(jìn)行權(quán)限控制

如何使用Hyperf框架進(jìn)行權(quán)限控制

如何使用Hyperf框架進(jìn)行權(quán)限控制

引言:
在開發(fā)一個(gè)應(yīng)用程序時(shí),往往需要實(shí)現(xiàn)權(quán)限控制功能,以不同的角色給予用戶不同的權(quán)限。Hyperf框架是一個(gè)高性能的php微服務(wù)框架,提供了許多強(qiáng)大的功能和擴(kuò)展,其中包括靈活的權(quán)限控制。在本文中,我們將探討如何使用Hyperf框架實(shí)現(xiàn)權(quán)限控制,并提供具體的代碼示例。

一、創(chuàng)建權(quán)限表
首先,我們需要?jiǎng)?chuàng)建一個(gè)權(quán)限表,用于存儲(chǔ)各種權(quán)限信息。可以通過(guò)Hyperf的數(shù)據(jù)遷移功能來(lái)創(chuàng)建數(shù)據(jù)庫(kù)表。在終端中執(zhí)行以下命令來(lái)生成遷移文件:

php bin/hyperf.php gen:migration create_permissions_table

然后在生成的遷移文件中添加以下內(nèi)容:

<?php use HyperfDatabaseSchemaSchema; use HyperfDatabaseSchemaBlueprint; use HyperfDatabaseMigrationsMigration; use HyperfDbConnectionDb;  class CreatetPermissionsTable extends Migration {     /**      * Run the migrations.      */     public function up(): void     {         $tableName = 'permissions';         $exists = Db::table('information_schema.TABLES')             ->where('TABLE_SCHEMA', config('databases.default.dbname'))             -&gt;where('TABLE_NAME', $tableName)             -&gt;first();          if (!$exists) {             Schema::create($tableName, function (Blueprint $table) {                 $table-&gt;bigIncrements('id');                 $table-&gt;string('name')-&gt;unique()-&gt;comment('權(quán)限名稱');                 $table-&gt;string('guard_name')-&gt;default('web')-&gt;comment('守衛(wèi)名稱');                 $table-&gt;timestamps();             });         }     }      /**      * Reverse the migrations.      */     public function down(): void     {         Schema::dropIfExists('permissions');     } }

接下來(lái),我們需要在項(xiàng)目的主配置文件config/autoload/permissions.php中添加以下內(nèi)容:

<?php return [     'default' => [         'guard_name' =&gt; 'web',         'permissions' =&gt; [             // 在這里添加你的權(quán)限             'create_post',             'edit_post',             'delete_post',             // ...         ],     ], ];

然后在命令行中運(yùn)行以下命令執(zhí)行數(shù)據(jù)庫(kù)遷移:

php bin/hyperf.php migrate

二、定義用戶角色模型
在Hyperf框架中,我們需要定義一個(gè)用戶模型,該模型用于管理用戶的角色和權(quán)限。我們可以通過(guò)繼承HyperfDatabaseModelModel類來(lái)創(chuàng)建一個(gè)用戶模型。在終端中執(zhí)行以下命令來(lái)生成用戶模型:

php bin/hyperf.php gen:model User

然后在生成的用戶模型文件中添加以下代碼:

namespace AppModel;  use HyperfDbConnectionModelModel; use HyperfUtilsApplicationContext;  class User extends Model {     protected $guarded = [];      public function roles()     {         return $this-&gt;belongsToMany(Role::class);     }      public function hasPermission($permission)     {         foreach ($this-&gt;roles as $role) {             if ($role-&gt;hasPermission($permission)) {                 return true;             }         }         return false;     }      public function givePermission($permission)     {         $permissionModel = Permission::where('name', $permission)-&gt;first();         if (!$permissionModel) {             throw new Exception("Permission {$permission} does not exist.");         }         $this-&gt;permissions()-&gt;sync($permissionModel, false);     }      public function revokePermission($permission)     {         $permissionModel = Permission::where('name', $permission)-&gt;first();         if (!$permissionModel) {             throw new Exception("Permission {$permission} does not exist.");         }         $this-&gt;permissions()-&gt;detach($permissionModel);     }      public function permissions()     {         return $this-&gt;belongsToMany(Permission::class, 'user_permissions');     } }

三、定義角色模型
在Hyperf框架中,我們也需要定義一個(gè)角色模型,該模型用于管理角色和權(quán)限。同樣,我們可以通過(guò)繼承HyperfDatabaseModelModel類來(lái)創(chuàng)建一個(gè)角色模型。在終端中執(zhí)行以下命令來(lái)生成角色模型:

php bin/hyperf.php gen:model Role

然后在生成的角色模型文件中添加以下代碼:

namespace AppModel;  use HyperfDbConnectionModelModel;  class Role extends Model {     protected $guarded = [];      public function users()     {         return $this-&gt;belongsToMany(User::class);     }      public function permissions()     {         return $this-&gt;belongsToMany(Permission::class);     }      public function hasPermission($permission)     {         return $this-&gt;permissions-&gt;contains('name', $permission);     }      public function givePermission($permission)     {         $permissionModel = Permission::where('name', $permission)-&gt;first();         if (!$permissionModel) {             throw new Exception("Permission {$permission} does not exist.");         }         $this-&gt;permissions()-&gt;sync($permissionModel, false);     }      public function revokePermission($permission)     {         $permissionModel = Permission::where('name', $permission)-&gt;first();         if (!$permissionModel) {             throw new Exception("Permission {$permission} does not exist.");         }         $this-&gt;permissions()-&gt;detach($permissionModel);     } }

四、定義權(quán)限模型
在Hyperf框架中,我們還需要定義一個(gè)權(quán)限模型,該模型用于管理權(quán)限信息。同樣地,我們可以通過(guò)繼承HyperfDatabaseModelModel類來(lái)創(chuàng)建一個(gè)權(quán)限模型。在終端中執(zhí)行以下命令來(lái)生成權(quán)限模型:

php bin/hyperf.php gen:model Permission

然后在生成的權(quán)限模型文件中添加以下代碼:

namespace AppModel;  use HyperfDbConnectionModelModel;  class Permission extends Model {     protected $guarded = [];      public function roles()     {         return $this-&gt;belongsToMany(Role::class);     } }

五、定義權(quán)限中間件
接下來(lái),我們需要?jiǎng)?chuàng)建一個(gè)權(quán)限中間件,用于檢查用戶是否有足夠的權(quán)限訪問(wèn)某個(gè)路由。在終端中執(zhí)行以下命令來(lái)生成中間件:

php bin/hyperf.php gen:middleware PermissionMiddleware

然后在生成的中間件文件中添加以下代碼:

namespace AppMiddleware;  use HyperfHttpMessageStreamSwooleStream; use HyperfHttpServerContractRequestInterface; use HyperfUtilsContext; use PsrContainerContainerInterface; use PsrHttpMessageResponseInterface; use PsrHttpServerMiddlewareInterface; use PsrHttpServerRequestHandlerInterface;  class PermissionMiddleware implements MiddlewareInterface {     protected $container;      protected $request;      public function __construct(ContainerInterface $container, RequestInterface $request)     {         $this-&gt;container = $container;         $this-&gt;request = $request;     }      public function process($request, RequestHandlerInterface $handler): ResponseInterface     {         $user = $this-&gt;request-&gt;getAttribute('user');         $permissions = $this-&gt;request-&gt;route-&gt;permission;          if ($user &amp;&amp; $user-&gt;hasPermission($permissions)) {             return $handler-&gt;handle($request);         }          return $this-&gt;response(403, 'Forbidden');     }      protected function response($code, $message)     {         $data = [             'code' =&gt; $code,             'message' =&gt; $message,         ];          return Context::get(ResponseInterface::class)-&gt;withBody(new SwooleStream(json_encode($data)));     } }

六、使用權(quán)限中間件
在路由定義中,我們可以通過(guò)使用->middleware(‘permission:xxx’)來(lái)給路由設(shè)置對(duì)應(yīng)的權(quán)限中間件。在終端中執(zhí)行以下命令來(lái)生成路由文件:

php bin/hyperf.php gen:controller PermissionController

然后在生成的路由文件中添加以下代碼:

namespace AppController;  use AppMiddlewarePermissionMiddleware; use HyperfHttpServerAnnotationController; use HyperfHttpServerAnnotationMiddleware; use HyperfHttpServerAnnotationRequestMapping;  /**  * @Controller  * @Middleware(PermissionMiddleware::class)  */ class PermissionController {     /**      * @RequestMapping(path="/permission", methods="get")      * @Middleware("permission:create_post")      */     public function createPost()     {         // 處理創(chuàng)建文章的邏輯     }      /**      * @RequestMapping(path="/permission", methods="get")      * @Middleware("permission:edit_post")      */     public function editPost()     {         // 處理編輯文章的邏輯     }      /**      * @RequestMapping(path="/permission", methods="get")      * @Middleware("permission:delete_post")      */     public function deletePost()     {         // 處理刪除文章的邏輯     } }

七、使用示例
在需要進(jìn)行權(quán)限控制的地方,我們可以通過(guò)以下方式來(lái)檢查用戶是否擁有足夠的權(quán)限:

$user = User::find(1);  if ($user-&gt;hasPermission('edit_post')) {     // 給用戶權(quán)限來(lái)編輯文章 } else {     // 權(quán)限不足 }

八、總結(jié)
本文介紹了如何使用Hyperf框架進(jìn)行權(quán)限控制的詳細(xì)步驟,并提供了具體的代碼示例。通過(guò)使用Hyperf框架提供的權(quán)限管理功能,我們可以輕松地為我們的應(yīng)用程序?qū)崿F(xiàn)靈活的權(quán)限控制功能。希望本文對(duì)您有所幫助,謝謝閱讀!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊13 分享