DEV Community

Cover image for How to List All Authorization Gates in Your Laravel Application [1/4]
Nick Ciolpan for Graffino

Posted on

How to List All Authorization Gates in Your Laravel Application [1/4]

While Laravel doesn't provide an Artisan command specifically for listing gates, there's a valid reason for this. The need to inspect these gates doesn't arise frequently. If such a situation does occur, you can rely on the ol’ debugging tools.

But for now I need to put together something quick and explore my options. That’s a job for the tinker command which allows us to evaluate PHP and Laravel code on-the-fly and see the results immediately, which can be incredibly useful in our scenario.

When it comes to what you can do with the Gates class, although the available methods are not explicitly exposed in the official Laravel documentation, contracts and facades serve as excellent sources of documentation for what you can accomplish with them.

Here, I am retrieving all the defined abilities and listing their names to provide a quick overview.

use Illuminate\Contracts\Auth\Access\Gate;

$gates = app(Gate::class)->abilities();
echo implode("\n", array_keys($gates));
Enter fullscreen mode Exit fullscreen mode

What the abilities method gives us is an associative array with key names and the corresponding closures - because that's how gate-based authorization works in Laravel. Gates provide a simple, Closure (a type of anonymous function in PHP) based approach to authorization.

  "viewHorizon" => Closure($user) {#446 …4},
  "viewTelescope" => Closure($user) {#449 …4},
Enter fullscreen mode Exit fullscreen mode

For now, we'll output a list with all their names. Helpful? A bit, yes - I can now at least know what to search for and inspect in my codebase. Should we be able to take a quick look at the contents as well? Well, the whole closure aspect of it requires some extra tinkering before we can look under their hood.

Top comments (0)