How I structure role-based access in business applications
Hiding a button is not security. A permission model that survives contact with a real business needs one catalogue, enforcement in the service layer, and a short list of actions that only two people may ever perform.
/Updated /7 min read
The short answer: to control who can access a business application, keep every role's permissions in one catalogue, check them on the server in the function that does the work, decide which records each person can see separately from what they can do, keep the few actions that rewrite financial history to the owner and finance, and make revoking access take effect on the next request.
Most business applications get permissions wrong in the same order. First everyone is an admin. Then someone does something expensive by accident, so a role column appears. Then the interface starts hiding buttons, and for a while it looks like the problem is solved — until someone finds the URL the button pointed at.
Here is the structure I keep coming back to, and why each part of it exists.
One catalogue, defined in code
A permission model spread across middleware, components and database checks cannot be audited, because nobody can enumerate it. I keep a single static matrix: every module, every action, and what each role may do.
// roles × modules × actions, in one place
export const permissions = {
owner: { invoices: ['read', 'create', 'issue', 'void'], projects: ['read', 'create', 'edit'] },
finance: { invoices: ['read', 'create', 'issue', 'void'], projects: ['read'] },
sales: { invoices: ['read'], projects: ['read', 'create', 'edit'] },
} as const;
Two things follow from this being data rather than scattered conditionals. It can be printed — you can put the whole matrix in front of the business owner and have a real conversation about it. And it can be tested: a single test asserts the count of permissions, so nobody adds a capability to a role without that showing up in a diff.
There is no permission-editing interface. A CRM for a small business does not need one, and every admin screen that edits its own authorisation model is a way to lock yourself out or quietly promote someone.
Enforce in the service layer, not in the component
The rule I would put on a wall: hiding a button is not security.
export async function voidInvoice(ctx: Context, id: number) {
assertCan(ctx, 'invoices', 'void'); // the boundary is here
// …
}
The interface should still hide the button — showing people controls they cannot use is bad design. But the check that matters sits in the function that does the work, where every caller has to go through it: the form, the API route, the background job, the script someone runs at 11pm.
If your only check is in the component, your permission model is a suggestion.
Some actions are not about roles at all
Every system I have built has a handful of operations that destroy or rewrite financial history: voiding an issued invoice, creating a credit note, reversing a payment, deleting an expense, editing an exchange rate.
Those are not "admin" actions. They are owner-and-finance actions, and I deliberately exclude the general administrator role from them. It is the one place where the matrix is not a neat hierarchy, and that is the point — the hierarchy is a convenience, not a truth about the business.
Record scope is a separate question from action scope
"Can this user edit deals?" and "can this user edit that deal?" are different questions, and conflating them produces either a system where salespeople edit each other's pipeline or one where nobody can cover for a colleague on holiday.
I model them separately: an action permission, plus a record scope of all, own, or none. The query layer applies the scope; the service layer applies the action.
Roles, record scope or attribute rules: which do you need?
Three different questions hide inside "who can access what", and each has its own mechanism:
| The question | Mechanism | Example |
|---|---|---|
| What may this kind of user do? | Roles (RBAC) | Sales can create and edit deals but only read invoices |
| Which records may they do it to? | Record scope | A salesperson edits their own deals; a manager edits the team's |
| Does it depend on the record's state? | Attribute rules | An invoice can be edited until it is issued, never after |
Most business applications need the first two from day one. The third arrives one rule at a time, and each rule is best written as a named check in the service layer, next to the action it guards, where a test can find it.
A person who works across departments does not need a special role. Give them two roles, or a role whose record scope covers both departments: their permissions are the union of their roles, and the scope decides whose records they see.
Push it into the database when the data justifies it
In one CRM the data was sensitive enough that application-level checks alone were not good enough — a mistake in one query would have exposed rows across the business. There, authorisation lives in row-level security policies: the database decides what the signed-in user can read, and the application cannot accidentally ask for more.
That has a real cost. Policies are harder to read than a function, and every query gets slower. I would not reach for it in a typical internal tool. I would reach for it again the moment the data is regulated, or the blast radius of one bad where clause is somebody's personal information.
Revoking access has to be immediate
Removing someone's role should change what they can do on their next click, not when a token expires. That is the argument for sessions stored on the server and checked on every request: delete the session, or change the role it points at, and the next request is refused.
In that design every protected page and action looks the session up before it does anything; the cookie on its own proves nothing. Signed tokens can be made to behave, but only by keeping them short-lived and re-checking the user's current role before anything sensitive, at which point most of their advantage is gone.
Test the denials, not just the permissions
Permission bugs are almost never "an admin could not do something". They are "somebody could do something they should not", and nobody notices because nothing visibly breaks. So the tests that matter are the refusals:
- one test per role that asserts the full set of what it may do, so any change to the matrix shows up in a diff;
- for each action that destroys or rewrites history, a test that calls the service function as every role that should be refused and expects it to throw;
- a record-scope test: a user with
ownscope asks for a colleague's record by id and gets not-found.
Where this is used
The student recruitment platform for London School of Excellence separates what admins, managers and agents can see. The property and investment CRM goes one step further and enforces authorisation in the database with row-level security. Tenant isolation, the same idea one level up, is covered in multi-tenant scoping in PostgreSQL.
If you are planning a system where different people must see different records, that is the work described on CRM & workflow systems. Whether that system should be a CRM product or built for you is the subject of custom CRM vs off-the-shelf CRM.
What I would check in an existing system
- Can you print the permission model? If not, nobody knows what it is.
- Where is it enforced? If the answer is "in the UI", it is not enforced.
- Which actions destroy history, and who can perform them?
- Does "can edit" distinguish between all records and the user's own?
- What happens to a signed-in session when someone's access is revoked? If the answer involves waiting for a token to expire, the revocation is a suggestion too.
Common questions
What is role-based access control?
Role-based access control (RBAC) gives permissions to roles, such as finance or sales, and puts people into roles, so access is managed per job rather than per person. It answers what each kind of user may do; which records they may do it to is a separate rule.
Is hiding buttons in the interface enough?
No. Hiding a button only changes what people see. Anyone who calls the underlying URL or API still reaches the action, unless the server checks the permission in the function that performs it.
When should permissions be enforced in the database?
When the data is regulated or personal, or when one wrong query would expose someone else's records. Row-level security makes the database itself refuse the rows, at the cost of harder debugging and slower queries.
Should a business be able to edit its own roles in the app?
Usually not in a small or mid-sized business system. A screen that edits the permission model is a way to lock yourself out or quietly promote someone; a matrix kept in code changes through review, and every change is visible.