Protect edit function in TagController with a policy
MODIFY
app/Http/Controllers/TagController.php
app/Http/Controllers/TagController.php
@@ -34,16 +34,18 @@ class TagController extends Controller {
}
public function edit(Tag $tag) {
+ $this->authorize('edit', $tag);
+
return view('tags.edit', compact('tag'));
}
public function update(Request $request, Tag $tag) {
+ $this->authorize('update', $tag);
+
$request->validate([
'name' => 'required|max:255'
]);
- $this->authorize('update', $tag);
-
$tag->name = $request->input('name');
$tag->save();

MODIFY
app/Policies/TagPolicy.php
app/Policies/TagPolicy.php
@@ -6,6 +6,10 @@ use App\User;
use App\Tag;
class TagPolicy {
+ public function edit(User $user, Tag $tag) {
+ return $user->spaces->contains($tag->space_id);
+ }
+
public function update(User $user, Tag $tag) {
return $user->spaces->contains($tag->space_id);
}

Protect edit function in TagController with a policy