I have successfully implemented policies for my Model class. Policy verification is working fine within the controller:
public function show(User $user, Student $student)
{
return $user->school_id === $student->school_id;
}
$request->user()->can('show', $student)
But middleware implementation for policies is not working, and I am always getting Unauthorised message.
$router->get('/students/{student}', [
'middleware' => ['scope:admin,moderator,teacher', 'can:show,student'],
'uses' => 'StudentController@show'
]);
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,
'can' => Illuminate\Auth\Middleware\Authorize::class,
]);
Spend a lot of time debugging the issue, and found out that function "guessPolicyName" under
\vendor\illuminate\auth\Access\Gate.php
is returning incorrect policy => .\Policies\6Policy (should have been StudentPolicy)
The numeric digit (6) is the student route parameter. Any help would be appreciated.