raid / core-gate
Raid Core Gate 包
Requires
- php: ^8.2
- raid/core-command: *
This package is auto-updated.
Last update: 2024-09-28 00:53:23 UTC
README
该包负责处理系统中所有门。
安装
composer require raid/core-gate
配置
php artisan core:publish-gate
用法
class PostController extends Controller { /** * Invoke the controller method. */ public function __invoke(Post $post) { Post::gates('show', $post); // or using the authorize method. Post::gates()->authorize('show', $post); return response()->json([ 'resource' => $post, ]); } }
如何工作
让我们从可门控类示例 Post
模型开始。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Raid\Core\Gate\Traits\Gate\Gateable; class Post extends Model { use Gateable; }
可门控类必须使用 Gateable
特性。
定义我们的门,我们有两种方法。
但请记住,如果定义了 getGates
方法,则将为该门控类忽略 config/gate.php
中的门。
- 在门控类中定义
getGates
方法,并在config/gate.php
中定义门控类。
<?php namespace App\Models; use App\Http\Gates\PostGate; use Illuminate\Database\Eloquent\Model; use Raid\Core\Gate\Traits\Gate\Gateable; class Post extends Model { use Gateable; /** * Get gateable gates. */ public static function getGates(): array { return [ // here we define our gate classes. PostGate::class, ]; } }
在 config/gate.php
中的门控类。
'gateables' => [ // here we define our gateable class. Post::class, ],
- 或者在
config/gate.php
中定义带有门控的门。
'gates' => [ // here we define our gateable class. Post::class => [ // here we define our gate classes. PostGate::class, ], ],
门
现在,让我们创建我们的门类 PostGate
。
您可以使用此命令创建门类。
php artisan core:make-gate PostGate
<?php namespace App\Http\Gates; use Raid\Core\Gate\Gates\Contracts\GateInterface; use Raid\Core\Gate\Gates\Gate; class PostGate extends Gate implements GateInterface { /** * {@inheritdoc} */ public const ACTIONS = []; }
门类必须实现 GateInterface
接口。
门类必须扩展 Gate
类。
门类必须定义 ACTIONS
常量,这是将使用 Illuminate\Support\Facades\Gate
类定义的门方法。
让我们先定义我们的门方法。
<?php namespace App\Http\Gates; use App\Models\Post; use App\Models\User; use Raid\Core\Gate\Gates\Contracts\GateInterface; use Raid\Core\Gate\Gates\Gate; class PostGate extends Gate implements GateInterface { /** * {@inheritdoc} */ public const ACTIONS = [ 'show', ]; /** * Determine if the user can show the post. */ public function show(User $user, Post $post): bool { return $post->isPublic() || $user->isAdmin() || $user->isAuthor($post); } }
太好了,现在我们可以在 PostController
中使用 show
方法/操作。
<?php namespace App\Http\Controllers; use App\Models\Post; class PostController extends Controller { /** * Invoke the controller method. */ public function __invoke(Post $post) { Post::gates()->authorize('show', $post); return response()->json([ 'resource' => $post, ]); } }
gates
方法是一个静态方法,它将从门控特性中调用。
authorize
方法是一个将导致调用 Illuminate\Support\Facades\Gate
类授权方法的函数。
如果门方法返回 false
,则 authorize
方法将抛出 Illuminate\Auth\Access\AuthorizationException
异常。
authorize
方法将基于相关门类中定义的操作返回。
您可以使用 Illuminate\Support\Facades\Gate
按如下方式授权门控操作
<?php namespace App\Http\Controllers; use App\Models\Post; class PostController extends Controller { /** * Invoke the controller method. */ public function __invoke(Post $post) { Gate::authorize('post.show', $post); return response()->json([ 'resource' => $post, ]); } }
我们可以通过在类中定义 gateableName
方法来覆盖门控类名。
<?php namespace App\Models; use App\Http\Gates\PostGate; use Illuminate\Database\Eloquent\Model; use Raid\Core\Gate\Traits\Gate\Gateable; class Post extends Model { use Gateable; /** * Get gateable name. */ public static function gateableName(): string { return 'post'; } }
就是这样。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。
致谢
安全
如果您发现任何与安全相关的问题,请通过电子邮件联系,而不是使用问题跟踪器。
关于 Raid
Raid 是由 穆罕默德·哈利德 创建的 PHP 框架,并由 穆罕默德·哈利德 维护。
支持 Raid
Raid 是一个 MIT 许可的开源项目。这是一个独立的项目,其持续开发得以实现。