I need to authorize users on the forum. So in the blade I have @can('editPost', $post)before showing the form for the answer on the topic. My class PostPolicyhas a method editPostthat authenticates if it belongs to the user.
However, the problem occurs when I want to perform a simple check, for example deletePost(). This checks if there is anyAuth::user()->isAdmin
public function deletePost(User $user) {
return Auth::user()->isAdmin;
}
However, this will not even be called as I am not passing an instance Post
My application in the real world is much more complicated, but I use it isAdminas a simple example.
I think that defining $gate->define('deletePost', 'App\Policies\PostPolicy@deletePost');in AuthServiceProvidercould work, but ultimately would separate my definitions and methods and, ultimately, into a big mess of applicationAuthServiceProvider
source
share