Skip to content

Role-based access control (RBAC)

Introduction

RBAC (role-based access control): user, then role, then permission, so an administrator only needs to decide "what role this person has".

Why use it

  • Easier to manage: permission changes update the role, not every person one by one.
  • Easier to audit: you can clearly answer “who has which permissions, and why”.
  • Least privilege: each person gets only what the job needs; even if an account is stolen, damage is limited.

Core structure

User ──(many-to-many)── Role ──(many-to-many)── Permission
Alice → Order admin → order:read, order:update
Bob → Support → order:read

A permission is the finest-grained “action + resource”, such as order:read or order:refund. Code should check permissions, not role names, so adjusting role composition later does not require changing business code.

How to do it

  1. List permissions first, then design roles; do not invent roles up front.
  2. Deny by default: anything not explicitly granted is forbidden.
  3. Checks must happen on the backend. Hiding a button in the frontend is UX, not security.
  4. RBAC only answers “can this person do this kind of thing”, not “can they act on this one row”. For example, support can view orders, but whether they may view only their own customers needs an extra data-level check (see the broken-access-control discussion in Authentication and authorization).
  5. Log important permission changes and sensitive operations; see Audit log.

Component comparison

Simple role field

Hand-rolledOpsLow

Add a "role" column (such as admin, user) to the user table, and have code check this column to decide what is allowed.

Best forSmall projects with only two or three identities, such as "admin / regular user".

Avoid whenRoles will multiply, or you need to frequently adjust who can do what.

Pros

Fast to implement, and anyone reading the code can follow it

No separate permission tables, and no matching tools to maintain

When there are very few roles, this approach is enough

Cons

A person can have only one role, so anything richer falls short

Checks are scattered, so changing a rule means searching everywhere

You cannot clearly say what a role can do, which makes auditing hard

RBAC (roles + permission table)

Hand-rolledOpen sourceOpsLow

Create "user-role-permission" tables: a permission is a specific action (such as "delete order"), a role is a set of permissions, and a user can have multiple roles.

Best forInternal management systems and admin backends used by various job roles.

Avoid whenSmall projects with only two or three identities, where this is over-engineering.

Pros

Adjusting a role's abilities only changes data, not code

A clear list makes it easy to audit who can do what

The most common approach for admin systems, with plenty of material

Cons

You must design the tables, management UI, and permission cache, so upfront work is substantial

Without planning, permission naming gets messier over time

Conditions like "can only edit your own department's data" are not covered by a role table alone

ABAC (attribute-based decisions)

Hand-rolledOpen sourceOpsMedium

Instead of fixed roles, decide dynamically by attributes, for example allowing an operation only if "the order belongs to you", "it is currently working hours", or "same department".

Best forMulti-tenant applications with clear data ownership (such as "can only see your own orders").

Avoid whenThe rules are simple and roles solve them.

Pros

It can express fine rules such as editing only your own data

You do not add a new role for every exception

The conditions, once written, read close to how the business is described

Cons

Rules live in code, so people who are not developers struggle to review them

When someone is denied, the reason is harder to find than in a role table

Once there are many rules they easily conflict, so you rely on tests

Casbin / OpenFGA (permission engines)

Self-hosted open sourceOpen sourceOpsMedium

Hand permission rules to a dedicated engine: Casbin is a library embedded in code and OpenFGA is a standalone service, both good at "who has what relationship to which resource".

Best forSystems with complex, frequently changing permission rules, or that need unified permissions across multiple services.

Avoid whenPermission needs are simple and a role table is enough.

Pros

Rules stay apart from business code, so a rule change does not change the program

Roles, attributes, and resource ownership can all be expressed

Several languages already have a library or a service API

Cons

You first have to learn its rule syntax or its way of modeling

OpenFGA is deployed on its own, and its data must stay in sync

A permission check adds another call, so the path to a bug is longer

Official docs ↗

Checklist

  • There is a clear permission inventory and role mapping
  • Code checks permissions, not hard-coded role names
  • Deny by default; permissions are enforced on the backend
  • Data-level ownership checks coexist with role checks
  • Follow least privilege; admin roles are few
  • Permission changes and sensitive operations are audited