Designing Role-Based Access Control That Lasts

Roles multiply until nobody can say who may do what. How to model permissions instead, enforce them in the database, and test the rules that actually leak.

3 min read

Every permission system starts as a short list and becomes a mess the first time someone asks for an exception. The mess is not caused by having too many roles. It is caused by storing the roles instead of the thing the roles are made of.

Seven roles is a symptom, not a design

A CRM I built ended up with seven roles: owner, admin, manager, sales, support, finance, and read-only. That is a reasonable starting set and a terrible long-term one, because within a month the client asked for “a manager who can see finance reports but not edit them.”

There are two ways to answer that. Add an eighth role, or notice that the request is about a permission, not a person.

Model permissions, not roles

Three tables carry the whole model:

  • permissions, the verbs, named as resource:action, e.g. invoice:approve
  • role_permissions, which verbs a named bundle contains
  • user_roles, who holds which bundle, scoped to a tenant

The tenant column is the part people forget. In a multi-tenant system a role is a property of the relationship between a user and a tenant, not of the user, the same person can be an admin in one and read-only in another. That lesson came out of building a multi-tenant CRM and it is the one that costs most to retrofit.

The permission matrix is a document, not code

Before writing any policy, write the grid. Resources down the side, actions across the top, roles as overlapping sets. It is boring and it surfaces the contradictions while they are still cheap.

Here is the shape that worked for that CRM, compressed:

Resource Owner Admin Manager Sales Support Finance Read-only
Contact full full full edit own view - view
Deal full full full edit own - view view
Invoice full full view - - approve view
User full manage - - - - -

Two things jump out of that grid and neither is obvious from a list of role names. Support cannot see deals, which is deliberate. Finance can approve an invoice but not edit its line items, which is the entire point of separation of duties.

Enforce in the database, not the view

Application checks produce good error messages. They are also one forgotten if away from a leak. Postgres row-level security is the backstop:

create policy invoice_finance_only on invoices
  for select
  using (
    tenant_id = (auth.jwt() ->> 'tenant_id')::uuid
    and (
      (auth.jwt() ->> 'role') = 'finance'
      or exists (
        select 1 from role_permissions rp
        join user_roles ur on ur.role_id = rp.role_id
        where ur.user_id = auth.uid()
          and rp.permission = 'invoice:approve'
      )
    )
  );

The habit worth building: write the policy first, then write the feature. If the feature needs data the policy will not return, that is a design conversation, not a reason to loosen the policy.

Testing access rules

Permission bugs do not look like bugs. They look like a page with fewer rows on it, which nobody reports.

  1. 01

    Test the negative, always

    For every role and action, assert that the wrong role is refused. Positive tests pass trivially; the negative ones are where the model is wrong.

  2. 02

    Test the matrix as data

    Encode the grid above as rows in a test fixture and loop over it, the way you would [any expensive computation](/writing/automated-daily-content-pipeline/). Adding a role then means adding a row, and the suite tells you what you forgot.

  3. 03

    Test across tenants explicitly

    Sign in as tenant A, attempt to fetch an id you know belongs to tenant B, assert 404 rather than 403. A 403 confirms the record exists, which is itself a leak.

  4. 04

    Test the aggregates

    Counts, sums and dashboards do not go through row policies in most frameworks. They are the classic quiet leak.

The leaks I have actually seen

The dangerous row is always the one with no logged-in user attached to it.

The short version

Model permissions, scope roles to the tenant, enforce in the database, and test the refusals rather than the grants. Every one of those is cheaper on day one than on the day somebody asks for the eighth role.

On this page

A practical cybersecurity and DR checklist

Access control, backups, patching, incident response and DR drills: the operational checks a small IT team can actually run, not the policy version.

3 min read

Why a Standalone HTML Dashboard Beat a Heavy BI Tool

A single self-contained HTML file, refreshed by a scheduled script, beat a licensed BI platform. Where that approach wins, and where it stops scaling.

3 min read

Designing TO-BE Flows for Servicing, Underwriting and Claims

Four layers for insurance operations: identity, AI, orchestration and the core system. Where straight-through processing works, and where it does not.

4 min read