Laravel 5.11 Authorization doesn't read Policies
Hi,
I was excited to try out the new authorisation feature in Laravel and it works fine if I define the abilities directly in the AuthServiceProvider. But when I tried to create a policy and register it, it just throws an Unauthorised error without even reading the policy.
Whatever I write in the protected $policies array, it is not taken into account.
<?php
namespace App\Providers;
use App\Article;
use App\Policies\ArticlePolicy;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
Article::class => ArticlePolicy::class,
];
/**
* Register any application authentication / authorization services.
*
* @param \Illuminate\Contracts\Auth\Access\Gate $gate
* @return void
*/
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
}
}
Anybody facing similar problems?
BTW, is there a way to call authorise() in the controller without any arguments like so?
public function update(Request $request)
{
$this->authorize();
//...
}
As the method can be resolved automatically, there must be a way to check only for a user attribute without needing an object.
Thanks
@gchaumont there is a better way to do it...
public function create()
{
$this->authorize('create', Post::class);
//....
}
Should trigger the policy based on the class name.
Please or to participate in this conversation.