Same posts, different eyes.

Write once who can see and edit which rows. Your lists, Edit buttons and search follow.

Ada is an admin: she sees and edits every post.

acme.test/posts
Search: launch3 results
  • Q4 launch plan: found
  • Launch day checklist: found
  • Globex launch recap: found
Posts6 of 6 · 6 editable
  • Q4 launch planAcmeby Leacan edit
  • Pricing page copyAcmeby Marccan edit
  • Launch day checklistAcmeby Samcan edit
  • Release notes 2.4Acmeby Leacan edit
  • Brand refresh briefGlobexby Tomcan edit
  • Globex launch recapGlobexby Tomcan edit
composer require lomkit/laravel-access-control

Why one file

Two rules drift. One control cannot.

Without it, a policy answers “may Sam open this post?” and a scope answers “which posts does Sam list?”. Written twice, they disagree.

Before

PostPolicy.php + Post.php
two rules
// app/Policies/PostPolicy.php
public function view(User $user, Post $post): bool
{
return $user->can('view all posts')
|| $post->client_id === $user->client_id
|| $post->author_id === $user->id;
}
// app/Models/Post.php
public function scopeVisibleTo(Builder $query, User $user): void
{
if (! $user->can('view all posts')) {
$query->where('author_id', $user->id);
}
}

Sam may view his own posts only.

His list never shows post 2. The policy still opens it by its URL, because it belongs to his company. No test fails.

After

PostControl.php
one rule
Levels::of(
GlobalPerimeter::new()
->allowed(fn (User $user, Ability $ability) => $user->can("{$ability} all posts"))
->unrestricted(),
ClientPerimeter::new()
->allowed(fn (User $user, Ability $ability) => $user->client_id !== null && $user->can("{$ability} client posts"))
->should(fn (User $user, Post $post) => $post->client_id === $user->client_id)
->query(function (Builder $query, User $user): void {
$query->where('client_id', $user->client_id);
})
->searchKeys(name: 'client', row: fn (Post $post) => $post->client_id, reader: fn (User $user) => $user->client_id),
OwnPerimeter::new()
->allowed(fn (User $user, Ability $ability) => $user->can("{$ability} own posts"))
->should(fn (User $user, Post $post) => $post->author_id === $user->id)
->query(function (Builder $query, User $user): void {
$query->where('author_id', $user->id);
})
->searchKeys(name: 'author', row: fn (Post $post) => $post->author_id, reader: fn (User $user) => $user->id),
),

The policy, the lists and the search read the same file.

Behind $user->can(), the controlled() scope and controlled searches. And a test proves they agree:

$this->assertControlAgrees(PostControl::class, $sam, Post::uncontrolled()->get());

laravel-rest-api

Your API follows too.

With laravel-rest-api 3.6 or later, there is nothing to wire: one route line, and each user gets their own rows.

routes/api.php
Rest::resource('posts', PostsController::class);
  • Ada

    POST /api/posts/search
    → posts 1, 2, 3, 4, 5, 6

  • Marc

    POST /api/posts/search
    → posts 1, 2, 3, 4

  • Lea

    POST /api/posts/search
    → posts 1, 2, 3, 4

  • Guest

    POST /api/posts/search
    → no row

How the laravel-rest-api extension works

One class per model. Every read agrees.

composer require lomkit/laravel-access-control

PHP 8.3+ · Laravel 13 · MIT