The Authorization Layer in Laravel: Consistent Access Control with Policies and Gates

In Laravel projects, authorization checks usually start out innocently: someone writes an if ($user->role === 'admin') at the top of a controller. Three months later, the same condition sits in fifteen places, each slightly different, and nobody is sure which one is correct. It's one of the most common kinds of technical debt we see in the projects we inherit.

The fix isn't a new package; it's using two pieces the framework already provides, with discipline: Gates and Policies.

When to use a Gate, when to use a Policy

A Gate is suited to ability checks that have nothing to do with a model: "can they export reports," "can they see the settings page," and so on. It's defined with Gate::define() and queried with Gate::allows(). A Policy, on the other hand, holds the rules gathered around a single model: who can view an order, who can update it, who can delete it.

Our rule of thumb: if there's an Eloquent model involved, use a Policy; if not, use a Gate. Piling model-based rules into Gates quickly makes the AuthServiceProvider file unreadable.

Gates are used for ability checks unrelated to a model, and Policies for the rules of a single Eloquent model.
The rule is simple: if there's an Eloquent model involved, use a Policy; if not, use a Gate.

Setting up a Policy correctly

The php artisan make:policy OrderPolicy --model=Order command generates the viewAny, view, create, update, and delete methods for you. Laravel matches models to policies automatically by naming convention; manual registration is only needed when you step outside that convention.

Inside a method, the rule should look at the relationship between the user and the model, not at a role name. For example, something like return $user->id === $order->user_id || $user->can('orders.manage');. That way, "order owner" and "manager" are defined in the same place, in a readable form.

Three small details round out Policy usage:

  • Instead of writing the super-admin exception into every method, grant it in one place inside Gate::before().
  • For guest users, use ?User $user in the method signature; otherwise, a request from a user who isn't logged in is rejected before it ever reaches the policy.
  • Policy methods should only return true/false; no side effects, no queries, no logging.

Where to call the check

Inside a controller, $this->authorize('update', $order) is the clearest route, and it automatically produces a 403 when it fails. If you're using a resource controller, you can wire up all the standard methods in a single line with authorizeResource(). At the route level, the ->can('update', 'order') middleware does the job.

On the Blade side, the @can directive is only for hiding a button. Hiding a button is not an authorization check; the real check must always be on the server-side request path. An endpoint that doesn't appear in the UI but can be reached directly by URL is the vulnerability we find most often in field reviews.

What to watch for in multi-tenant setups

In systems where multiple companies work in the same panel, a policy alone isn't enough. You also need to verify that the record resolved through route model binding belongs to that company. We usually handle this by locking queries to the tenant with a global scope, while keeping a $user->tenant_id === $model->tenant_id check inside the policy as a second line of defense. Relying on a single layer can, one day, turn into showing another company's data because of a forgotten scope.

Testing authorization rules

The biggest advantage of Policies is that they're testable. Inside an OrderPolicyTest, a small table of user, order, and expected result verifies all of the rules in seconds. Write at least one "allowed" and one "forbidden" scenario for each role. When a new role is added, these tests tell you which screens are affected.

In controller tests, too, include at least one unauthorized attempt using the actingAs($user)->put(...)->assertForbidden() pattern. Authorization holes can go unnoticed while the happy-path tests pass.

The first mistake is writing a policy and forgetting to call it; the rule exists, but no route uses it. The second is checking only on the list screen and skipping the check on the detail, update, or API endpoint. The third is defining an overly broad exception inside Gate::before(); a broad condition that reaches return true just once silently disables every rule. We recommend making "is there an authorize call on this endpoint?" a permanent item on your code review checklist whenever a new feature is completed.

Three common authorization mistakes and the standing question to add to code review to catch them.
The check question should be a permanent item in code review.

If the authorization checks in your existing Laravel project are scattered, or you want to build a solid permission model for a new panel, reach out through our contact page. Patching authorization in after the fact always costs more than getting it right from the start.