tmyers273/laravel-ownership
Composer 安装命令:
composer require tmyers273/laravel-ownership
包简介
Allows ownership verification on route model binding
README 文档
README
A simple package to do Model ownership checks automatically via Laravel's Route Model Binding.
What's it do?
// Let's whip up a few Users and a Post to demonstrate $owner = factory(User::class)->create(); $post = factory(Post::class)->create([ 'user_id' => $owner->id ]); $otherUser = factory(User::class)->create(); $adminUser = factory(USer::class)->create([ 'is_admin' => true ]);
// This will be a successful call, since $owner owns the $post
$this->be($owner);
$this->json('PATCH', '/post/' . $post->id, [
'title' => 'Edited by Owner'
]);
// This will fail, since $otherUser does not own the $post
$this->be($otherUser);
$this->json('PATCH', '/post/' . $post->id, [
'title' => 'Edited by Owner'
]);
// This will be a successful call, since $adminUser can override the ownership check (by default)
$this->be($adminUser);
$this->json('PATCH', '/post/' . $post->id, [
'title' => 'Edited by Owner'
]);
// This will fail, since 12345 is a made up Post id
$this->be($owner);
$this->json('PATCH', '/post/12345', [
'title' => 'Edited by Owner'
]);
Installation
composer require tmyers273/laravel-ownership
php artisan vendor:publish --tag=laravel-ownership-config --tag=laravel-ownership-trait
Add OwnsModels trait to your User model
use App\Traits\OwnsModels; class User extends Authenticatable { use OwnsModels; }
Add OwnedByUser to each model you want to validate ownership on.
use TMyers273\Ownership\OwnedByUser; class Post extends Model { use OwnedByUser; }
Modify the isAdmin and owns methods on the OwnsModels trait to fit your needs!
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2019-07-06