-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Policy not working #16184
Comments
Hello @vocal Shouldn't the 2 routes that are not working be like...
post is missing from those 2 routes... Also, I am unsure as to how you are getting This action is unauthorized instead of 404 Not Found. If you comment out the line having Lastly, I am unsure as to why you are really using Policies if you are not doing something like return $user->id == $policy->user_id;
//Instead of return $user->id === 1; Or is it just for testing purposes?
|
This action is unauthorized is exception throw by Illuminate\Auth\Access\AuthorizationException (403 Error) if i comment out i'm using return $user->id === 1; (user logged id = 1) or return true; for testing but still error at 2 routes edit() and show() if i using
instead of
then post.edit route working fine but post.show still 403 error |
I have found the problem. |
I had the same issue and I can confirm the error goes away after changing the
to:
I just don't understand why that's happening. It seems like Laravel docs don't even mention the |
Same problem here, more than two years later it appears. |
Laravel 5.7
i see root cause, getting policy via Illuminate\Auth\Access\Gate::getPolicyFor return null for argument is Model::id's value |
Same here class QueryPolicy
class QueryController
class AuthServiceProvider
EDIT: I had a type in my route
to this
|
Thanks a ton dude! I was going mad trying to strictly follow proper laravel conventions. |
I had the same issue on my user controller. On all my controllers i also made the mistake of using a plural with the authorizeResource command since my routnames are plurals like 'domain/leases'
changing it to
Fixed the issue |
Hi all,
i'm using Policy to authorization but it's not working on all route.
Description
1.Create default controller using
php artisan make:controller PostController --resource
2.Create policy
php artisan make:policy PostPolicy --model=Post
3.In PostPolicy return true for each action (view, create, update, delete)
public function view(User $user, Post $post) { return $user->id === 1; }
4.Register policy in the AuthServiceProvider
protected $policies = [ 'App\Model' => 'App\Policies\ModelPolicy', Post::class => PostPolicy::class, ];
5.In web.php add routes
Route::resource('post', 'PostController');
6.in PostController add authorizeResource method
public function __construct() { $this->authorizeResource(Post::class); }
7.Access to URL from browser.
METHOD
[GET] http://laravel.local:8000/post (post.index) ==> worked
[DELETE] http://laravel.local:8000/post/1 (post.destroy) ==> worked
[GET] http://laravel.local:8000/post/create (post.create) ==> worked
[POST] http://laravel.local:8000/post/create (post.store) ==> worked
[GET] http://laravel.local:8000/1/edit (post.edit) ==> Error : This action is unauthorized.
[GET] http://laravel.local:8000/1 (post.show) ==> Error : This action is unauthorized.
8.in post_index.blade
@can('view', $post) <a href="{{ route('post.show', ['post' => $post])}}">Show</a> @endcan
@can('update', $post) <a href="{{ route('post.edit', ['post' => $post])}}">Edit</a> @endcan
Result : Show, Edit link is display correct. (in view check policy is correct ??)
2 routes : edit and show is not working.
The text was updated successfully, but these errors were encountered: