??? Form requests can handle both validation and authorisation ? https://laravel.com/docs/master/validation#form-request-validation
Jun 17, 2016
19
Level 4
Calling authorize policy from a Form Request?
Hello! I have a quick question. I want all of my auth logic to be in a policy, but I use a form request class when something is posted for updating/creating so I want to be able to call $this->authorize('store', $post); inside the request NOT the controller.
IE:
// controller
public function store(StorePostRequest $request) {
}
// request
public function authorize()
{
$post = Post::findOrFail($this->route('post'));
return $this->authorize('store', $post);
}
Level 29
At-least read the whole documentation ? Abilities, policies, these are big words, don't let then confuse you, when you define ACL on object itself as to who can do what, its called policy. When you define it on user, its called ability.
Example: Ability
$gate->define('update-post', function ($user, $post) {
return $user->id === $post->user_id;
});
if (Auth::user()->can('update-post', $post)) {
// Update Post...
}
Policy
<?php
namespace App\Policies;
use App\User;
use App\Post;
class PostPolicy
{
/**
* Determine if the given post can be updated by the user.
*
* @param \App\User $user
* @param \App\Post $post
* @return bool
*/
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
}
if (Auth::user()->can('update', $post)) {
//
}
Where do you see the difference ?
2 likes
Please or to participate in this conversation.