$this->authorize( 'view', DocsReportPolicy::class);
and
Gate::authorize('viewAny', DocsReportPolicy::class);
are different codes to do exactly the same thing.
In your policy, you only show the view function, so I assume that you don't have completed the viewAny one.
Furthermore you give another example of what you have tried.
$this->authorize('view', $user);
Here you are passing the user to the function, which has no sense if the aim is to check if the authenticated user can view the docs report.
What you have to know.
-
the authenticated user is automatically injected in all policy functions
-
the policies are automatically binded to the associated model if you respect the naming convention, for example
CategoryPolicyfor theCategorymodel -
$this->authorize('view', $user);means that you check if the authenticated user can view the$user -
$this->authorize( 'view', DocsReportPolicy::class);can't work, you don't have to pass the policy class, but the model class -
$this->authorize( 'view', DocsReport::class);will work and means that you check if the authenticated user can view any docs report, but it would be better to useviewAnyfor this -
$this->authorize( 'viewAny', DocsReport::class);is better to check if the authenticated can view any docs report -
$this->authorize( 'view', $docsReport);is better to check if the authenticated user can view a specific docs report
In the policy class, you have (among others) these functions.
public function viewAny(User $user) // the injected authenticated user
{
...
}
public function view(User $user, DocsReport $docsReport) // the injected authenticated user and an instance of a docs report model
{
...
}
Finally you have to register manually your policy because it's in a subfolder of the Policies folder.
But this is wrong.
Gate::policy(DocsReportController::class, DocsReportPolicy::class);
You have to register the policy with the model and not with the controller.
Gate::policy(DocsReport::class, DocsReportPolicy::class);