fireworkweb / laravel-gates
使用路由名称的Gates进行Laravel权限管理
v0.10
2023-05-10 16:54 UTC
Requires
- php: ^7.3|^8.0
- illuminate/support: ^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0
- phpunit/phpunit: ^9.0|^10.0
README
此包允许您使用路由名称的Gates来管理权限。
安装
您可以通过composer安装此包
composer require fireworkweb/laravel-gates
包中间件
此包提供了2个中间件
Gate
- 检查当前路由Gates,如果没有匹配的Gates,则中断GateOptional
- 检查当前路由Gates,如果没有匹配的Gates,则记录
您可以在您的app/Http/Kernel.php
文件中添加它们。
protected $routeMiddleware = [ // ... 'gate' => \Fireworkweb\Gates\Middlewares\Gate::class, 'gate_optional' => \Fireworkweb\Gates\Middlewares\GateOptional::class, ];
用法
以下是一个示例
Route::middleware('gate')->group(function () { // ... Route::get('posts/{post}/edit')->name('posts.edit'); });
<?php namespace App\Policies; use App\Post; use App\User; use Fireworkweb\Gates\Traits\HasGates; class PolicyWithResourceGates { use HasGates; protected static function gateRouteName() : string { return 'posts'; } protected static function gateAbilities() : array { return [ 'edit' => 'edit', ]; } public function edit(User $user, Post $post) { return $user->id === $post->user_id; } }
这将注册一个Gates posts.edit
,在路由 posts/1/edit
上,它会检查你是否在 App\Policies\Post@edit
中,并注入路由参数。
命令
您有命令可以帮助您查找没有Gates的路由
# it will get the routes that has `gate` middleware fwd artisan gates:routes-without-gate # in case you are using a custom middleware name or want to check the optional one fwd artisan gates:routes-without-gate gate_optional
测试
composer test
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全性
如果您发现任何与安全性相关的问题,请通过电子邮件contact@fireworkweb.com而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。