This package allows you to manage permissions using Gates with Route Names.
You can install the package via composer:
composer require fireworkweb/laravel-gates
This package comes 2 middlewares:
Gate
- Checks current route gates, if no matching gate, breaksGateOptional
- Checks current route gates, if no matching gate, logs
You can add them inside your app/Http/Kernel.php
file.
protected $routeMiddleware = [
// ...
'gate' => \Fireworkweb\Gates\Middlewares\Gate::class,
'gate_optional' => \Fireworkweb\Gates\Middlewares\GateOptional::class,
];
Here is an example:
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;
}
}
That will register a gate posts.edit
and on route posts/1/edit
it will check if you on App\Policies\Post@edit
injecting route parameters.
You have commands to help you find routes without gate:
# 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
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email contact@fireworkweb.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.