Design an Access Control System
🔐 What is an access control system? It is the part of an app that answers one question many times a second: may this user do this thing here? A user signs in, opens an invoice, clicks "refund", and before anything happens the app asks the access control system. It answers yes or no based on the user's roles and the rules attached to them.
Interviewers ask for this under several names: "design RBAC", "design IAM policies", "design roles and permissions for a B2B app". They are the same design. Role-based access control (RBAC) is the core: permissions go on roles, and users get roles. IAM-style policies add explicit deny rules that beat any allow. B2B adds organizations: the same person can be an admin at one customer and a viewer at another. We build all three into one small system, and cover attribute rules ("only the owner may edit") and custom roles per customer as follow-ups.
Requirements
"Design the permission system for our B2B SaaS app. Admins should be able to define roles, give roles to users, and the app needs to check permissions on every request."
The prompt sounds simple, but almost every word hides a decision. Who is an admin of what? What does a permission look like? Can roles build on each other? The questions below pin those down.
Clarifying Questions
You: What does a permission look like? Is it per object, like "invoice 42", or per kind of object?
Interviewer: Per kind. A permission is an action on a resource type, like invoice:read or user:delete. Checks on a single object can come later.
You: Can one grant cover many permissions? An admin with a hundred separate grants sounds painful.
Interviewer: Yes. Either part may be a wildcard: invoice:* is everything on invoices, *:read is read on everything, and *:* is everything.
You: Can roles build on each other, so an editor automatically has everything a viewer has?
Interviewer: Yes. A role can inherit from other roles, and inheritance goes all the way up. Don't allow loops.
You: Is a user's role global, or does it depend on the customer organization?
Interviewer: Per organization. A consultant can be an admin at one customer and a viewer at another. Every check happens inside one organization.
You: Do we only add permissions, or can a role also take them away?
Interviewer: Both. Security wants explicit denies, like "contractors may never touch billing", and a deny must win over any allow, even one that comes from another role.
You: If no rule matches, is the answer yes or no?
Interviewer: No. Nothing is allowed unless something allows it.
You: Do we also handle login, passwords and tokens?
Interviewer: No. Someone else tells you who the user is. You only decide what they may do.
The deny question changes the most code. With allows only, a check can stop at the first match. With denies, the order of rules must not matter, so we have to look at every matching rule before we answer. We will see what goes wrong if we skip that.
💡 Interview tip: Separate authentication (who are you?) from authorization (what may you do?) in the first two minutes. Interviewers often leave the prompt vague on purpose to see if you mix them up. This design is only the second one.
Final Requirements
- A permission is
resource:action. A grant or deny may use*for either part, and*matches anything. - Admins create roles and attach rules to them: allow rules (
grant) and deny rules (deny). - A role can inherit from other roles and then has all their rules, including the rules of their parents. Inheritance is live: a rule added to a parent later applies to every role below it. A link that would create a cycle is refused.
- Users get roles inside an organization. Roles in one organization have no effect in another.
- A check gathers the rules of all the user's roles in that organization (including inherited ones) that match the permission. Any matching deny means no. Otherwise a matching allow means yes. No match means no.
- Anyone can list a user's effective roles in an organization, for an admin screen or for debugging.
Out of scope
- Authentication: passwords, sessions, tokens.
- Permissions on single objects and conditions like "only the owner" (we add them in Extensibility).
- Custom roles defined by each customer (also in Extensibility).
- Storing roles in a database and an admin UI.