(Note · · 6 min read)

Your roles stopped fitting about six customers ago

Four roles were enough at the start. Then a customer asked for a manager who can approve leave but cannot see salaries, and the only way to deliver it was a fifth role, then a sixth, each a near-copy of another with one difference nobody can remember.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

Check permissions, not roles

The decisive design choice is what the code asks. A check for whether the user is an admin hard-codes the role structure into every call site, and changing the structure then means editing all of them.

A check for whether the user may approve payroll keeps the structure out of the code entirely. The role becomes a named bundle of permissions — data, not logic — and a new customer requirement becomes a new bundle rather than a code change.

On the payroll platform I build, that is 139 permissions behind 7 built-in roles, with roughly 1,200 routes checking permissions rather than role names. The number of roles is not the interesting figure; the fact that none of them is referenced in a condition is.

Name permissions after actions

A permission should correspond to something a person does: view a payslip, approve leave, export employee data, change a bank account. Those names survive refactoring because they describe the business, not the code.

Separate reading from writing and from approving. Collapsing them means the only way to let somebody see a report is to let them change it, which is how over-broad access gets granted by people with no intention of doing so.

Permission is not the same as scope

Two questions look alike and are not: may this person view payslips, and whose payslips may they view. A manager viewing their own team's records and an administrator viewing everyone's hold the same permission with different scope.

Handle scope as a filter applied to the query, not as another permission. Otherwise every new organisational shape multiplies the permission list, and the person granting access cannot tell which of two similar names to pick.

Enforce it where the data is

Permissions decide what the interface offers and must be re-checked on the server for every request. A hidden button is never the guard: the request it would have sent can still be sent by hand, and an interface built from the same permission list gives a false sense that the checking is done.

Put the check as close to the data as you can manage — in the handler or the data layer rather than in a route table — so a new entry point cannot reach the data without passing it.

Make the grants inspectable

Someone will eventually have to answer who can see salaries. That question should be answerable by reading the data, not by reasoning about code, which means a permission list a person can be shown, and a record of who granted what and when.

Log refused attempts as well as granted ones. A denial is how you find both a misconfigured account and someone probing for what they can reach.

Migrating an existing system

You do not have to rewrite it at once. Introduce permissions alongside the roles, define each existing role as a bundle, then replace role checks with permission checks a handler at a time. The behaviour is unchanged throughout, which is what makes the change safe to do gradually.

Common questions

Should code check roles or permissions?

Permissions. A check for whether a user is an admin hard-codes the role structure into every call site; a check for whether they may approve payroll keeps it out, so roles become named bundles of permissions — data rather than logic — and a new requirement is a new bundle, not a code change.

How should permissions be named?

After actions a person takes: view a payslip, approve leave, export employee data. Those names survive refactoring because they describe the business. Keep reading, writing and approving separate, or the only way to let someone see a report is to let them change it.

What is the difference between permission and scope?

Permission is whether someone may view payslips; scope is whose. A manager and an administrator can hold the same permission with different scope. Apply scope as a query filter, not as more permissions, or every organisational shape multiplies the list.

Is hiding a button in the interface enough?

No. A hidden button is never the guard — the request it would have sent can still be made by hand. Permissions decide what the interface offers and must be re-checked on the server for every request, as close to the data as possible.

Need something like this built?