michaeljennings/route-guards
Composer 安装命令:
composer require michaeljennings/route-guards
包简介
Provides a convenient place to authorize routes when model policies cannot be used
README 文档
README
Installation
To install the package run
composer require michaeljennings/route-guards
Once the package is installed, add the MichaelJennings\RouteGuards\GuardRoutes middleware to you App\Http\Kernel.php,
if you are using model bindings makes ure to register it after the SubstituteBinding middleware.
protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, \MichaelJennings\RouteGuards\GuardRoutes::class, ], ];
Usage
Registering Guards
You can guard a route in 3 different ways.
Firstly, you can chain a guard method on the route.
Route::get('/products', 'ProductController@index')->guard(ProductGuard::class);
Or you can set the guard in the route action.
Route::get('/products', ['uses' => 'ProductController@index', 'guard' => ProductGuard::class]);
Finally, you can specify the guard in a route group.
Route::group(['prefix' => 'products', 'guard' => ProductGuard::class], function () { Route::get('/', 'ProductController@index'); });
Writing Guards
By default we will try to hit an authorize method on the route guard. The authorize method should return true if the user is allowed to access the route, and false if they aren't.
use Illuminate\Routing\Route; use MichaelJennings\RouteGuards\RouteGuard; class ProductGuard extends RouteGuard { public function authorize(Route $route): bool { return Auth::user()->hasPermission('products.read'); } }
Occasionally you might want to authorize one endpoint in a group differently to the others. For example, in the below routes we want to check for a different to view the products and to create a product
Route::group(['prefix' => 'products', 'guard' => ProductGuard::class], function () { Route::get('/', 'ProductController@index'); Route::post('/', 'ProductController@store'); });
To do this simply add a method with the same name as the controller method to your route guard.
use Illuminate\Routing\Route; use MichaelJennings\RouteGuards\RouteGuard; class ProductGuard extends RouteGuard { public function index(Route $route): bool { return Auth::user()->hasPermission('products.read'); } public function store(Route $route): bool { return Auth::user()->hasPermission('products.create'); } }
Model Bindings
For certain routes you may also want to check that a user can access a specific record, for example in the route below we want to make sure the user can access a record before they can make updates to it.
Route::put('/products/{product}', 'ProductController@update')->guard(ProductGuard::class);
By default we will attempt to use model bindings to access the model from the route and pass it into the authorize method.
use Illuminate\Routing\Route; use MichaelJennings\RouteGuards\RouteGuard; class ProductGuard extends RouteGuard { // This works for authorize public function authorize(Route $route, Model $model): bool { // Check the user can access the model } // It also works for the custom methods public function update(Route $route, Model $model): bool { // Check the user can access the model } }
Custom Bindings
If you aren't using model bindings but still want to take advantage of finding your resource in the route guard you
can override the find method.
use Illuminate\Routing\Route; use MichaelJennings\RouteGuards\RouteGuard; class ProductGuard extends RouteGuard { public function __construct(ProductRepository $productRepository) { $this->productRepository = $productRepository; } // This works for authorize public function authorize(Route $route, Model $model): bool { // Check the user can access the model } protected function find(Route $route, string $binding) { return $this->productRepository->find( $route->parameter($binding) ); } }
Using Multiple Guards
In the route below we have to parameters in one route that need to be guarded differently.
Route::get('/products/{product}/variants/{variant}', 'Product\VariantController@show');
We can do this by setting two guards on the route, and telling them which parameter they guard.
Route::get('/products/{product}/variants/{variant}', 'Product\VariantController@show') ->guard(ProductGuard::class, 'product') ->guard(VariantGuard::class, 'variant');
You can also define this on a route group by providing an array where the key is the parameter the guard will protect.
Route::group(['prefix' => 'products/{product}/variants/{variant}', 'guards' => [ 'product' => ProductGuard::class, 'variant' => Variant::guard ]], function () { Route::get('/', 'Product\VariantController@show'); });
Customising Exceptions
If authorization fails we will throw the Illuminate\Auth\Access\AuthorizationException exception. If you want to
change this you can override the authorizationFailed method.
use Illuminate\Routing\Route; use MichaelJennings\RouteGuards\RouteGuard; class ProductGuard extends RouteGuard { public function authorize(Route $route): bool { return false; } protected function authorizationFailed(): void { throw new CustomException(); } }
Occasionally you might want to throw a different exception for your index endpoint than your create endpoint. You can this by taking the name of the method and adding failed to it.
use Illuminate\Routing\Route; use MichaelJennings\RouteGuards\RouteGuard; class ProductGuard extends RouteGuard { public function index(Route $route): bool { return false; } protected function indexFailed(): void { throw new IndexException(); } public function create(Route $route): bool { return false; } protected function createFailed(): void { throw new CreateException(); } }
michaeljennings/route-guards 适用场景与选型建议
michaeljennings/route-guards 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.68k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 michaeljennings/route-guards 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 michaeljennings/route-guards 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-19