Gate::after(): returning a not null value do not alter the result of the authorization

263 views Asked by At

As per laravel official documentation,

if the after callback returns a non-null result that result will be considered the result of the check.

But when I declare after callback and overwrite the value as false, it still returns the previous value.

What am I missing? How to override the value in Gate::after ?

Gate::define('edit-settings', function ($user = null) {
    return true;
});
Gate::after(function ($user = null, $ability = null, $result = null, $arguments = null) {
    return false; //this have no effect
});

// ------------

Gate::allows('edit-settings'); // return true!
2

There are 2 answers

1
V-K On
  1. This behavior can be if you have the Laravel < 5.7
  2. You process the request without authorization
  3. Gate::after(function ($user = null, $ability = null, $result = null, $arguments = null) - params should not be null. I think it can cause this error
1
Sĩ Cái Thế On

because your 'edit-settings' gate always return a boolean value, so Gate::after is not called.

this article can help.