Yes, the Gate::before callback will bypass your TicketPolicy::view() method if it returns a non-null value.
Here's how it works:
- When an ability is being checked (e.g.
Gate::authorize('view', $ticket);), Laravel will first run anyGate::beforecallbacks. - If your
Gate::beforereturns a non-null value (trueorfalse), that value is immediately considered the final answer for the authorization check, and no further policy methods will be called for that ability check. - If your
Gate::beforereturnsnull, Laravel falls through to the associated policy method (likeview) and evaluates that as normal.
So, with your code:
Gate::before(function (User $user, $ability) {
if ($user->hasAnyPermission(['view_any_ticket'])) {
return true;
}
return null;
});
If the user has the view_any_ticket permission, Gate::before returns true, and Laravel grants access for any ability (including 'view'), without ever calling your policy's view method.
To sum up:
If Gate::before returns true, the TicketPolicy::view() method is not called and access is granted immediately.
Reference:
The official Laravel documentation on Gates says:
"If the before callback returns a non-null result that result will be considered the result of the authorization check."
So in your scenario, yes, the global Gate::before will bypass the policy method if it returns true.