Add TagPolicy
MODIFY
app/Http/Controllers/TagsController.php
app/Http/Controllers/TagsController.php
@@ -29,15 +29,11 @@ class TagsController extends Controller {
return redirect('/tags');
}
- public function edit($id) {
- $tag = Tag::find($id);
-
+ public function edit(Tag $tag) {
return view('tags.edit', compact('tag'));
}
- public function update(Request $request, $id) {
- $tag = Tag::find($id);
-
+ public function update(Request $request, Tag $tag) {
$tag->name = $request->input('name');
$tag->save();

ADD
app/Policies/TagPolicy.php
+<?php
+
+namespace App\Policies;
+
+use App\User;
+use App\Tag;
+
+class TagPolicy {
+ public function update(User $user, Tag $tag) {
+ return $user->id === $tag->user_id;
+ }
+}

MODIFY
app/Providers/AuthServiceProvider.php
app/Providers/AuthServiceProvider.php
@@ -15,7 +15,8 @@ class AuthServiceProvider extends ServiceProvider
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
'App\Earning' => 'App\Policies\EarningPolicy',
- 'App\Spending' => 'App\Policies\SpendingPolicy'
+ 'App\Spending' => 'App\Policies\SpendingPolicy',
+ 'App\Tag' => 'App\Policies\TagPolicy'
];
/**

MODIFY
routes/web.php
routes/web.php
@@ -29,8 +29,8 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/tags', 'TagsController@index')->name('tags');
Route::get('/tags/create', 'TagsController@create')->name('tags.create');
Route::post('/tags', 'TagsController@store');
- Route::get('/tags/{id}/edit', 'TagsController@edit')->name('tags.edit');
- Route::patch('/tags/{id}', 'TagsController@update');
+ Route::get('/tags/{tag}/edit', 'TagsController@edit')->name('tags.edit')->middleware('can:update,tag');
+ Route::patch('/tags/{tag}', 'TagsController@update')->middleware('can:update,tag');
Route::delete('/tags/{id}', 'TagsController@destroy')->name('tags.destroy');
Route::get('/settings', 'SettingsController@index')->name('settings');

Add TagPolicy