Skip to content

Guide · November 26, 2026 · 3 min read

Role-based permissions with wildcards in Django REST Framework

Design per-tenant RBAC for a Django SaaS: app.resource.action permission strings, wildcards, per-member overrides, role levels and keeping the Next.js UI in sync.


Every B2B SaaS ends up needing roles: owners who can delete the workspace, admins who manage people, members who do the work, viewers who only read. Django’s built-in permissions are global to the user, so they do not fit a product where the same person has different roles in different workspaces. Here is a permission model that does, and the traps that make one silently fail.

Permission strings: app.resource.action

Name every permission after what it protects:

tenants.members.invite
billing.subscriptions.change_plan
support.tickets.list

Store a role’s permissions as a list (a PostgreSQL ArrayField works well) and give each membership two adjustment lists:

class Role(BaseModelUUID):
    name = models.CharField(max_length=50)
    permissions = ArrayField(models.CharField(max_length=100), default=list)

class TenantMember(BaseModelUUID):
    role = models.ForeignKey(Role, on_delete=models.PROTECT)
    additional_permissions = ArrayField(models.CharField(max_length=100), default=list)
    revoked_permissions = ArrayField(models.CharField(max_length=100), default=list)

The effective set is (role permissions ∪ additional) − revoked. That lets you give one editor the right to export data without creating a new role for them.

Wildcards

Listing every permission for every role does not scale. Support patterns instead:

Pattern Grants
*.* everything (the owner)
billing.* every permission in the billing app
billing.invoices.* every action on invoices
*.*.list every list action, everywhere (part of a read-only role)
billing.*.list every list action in billing
*.invoices.list listing invoices in any app

The check stays cheap — a handful of set lookups:

def has_permission(self, permission):
    perms = self.get_permissions()
    if "*.*" in perms or permission in perms:
        return True
    app, resource, action = permission.split(".")
    return any(
        candidate in perms
        for candidate in (
            f"{app}.*",
            f"{app}.{resource}.*",
            f"*.*.{action}",
            f"{app}.*.{action}",
            f"*.{resource}.{action}",
        )
    )

Derive the permission from the view

Rather than decorating every endpoint, derive the required permission from the ViewSet: app label, resource name and DRF action (list, retrieve, create, update, destroy, with partial_update folded into update). Custom actions map explicitly to an existing permission. Put it in a mixin so a new ViewSet is protected by default.

APIViews that are not ViewSets do not get the mixin — call the check by hand there.

The trap: permissions that do not exist

Keep a catalog of every permission the API can require. Two failure modes come from forgetting it:

  1. A new action that is not in the catalog. No role holds it, so it is denied to everyone except the owner (who holds *.*). Nobody notices until a customer’s admin cannot do something.
  2. The frontend gating on a string the API never checks. A typo like billing.subscriptions.update instead of …change_plan hides the button from every role but the owner, silently.

Fix both with a single source of truth: the catalog lives in the API, default roles are built from it, a management command back-fills existing tenants when it changes, and tests assert that every permission the frontend uses exists in the catalog.

Some rules are not permission strings

Two checks need the relationship between the caller and the target, not a string:

  • Managing another member: compare role levels, so an admin cannot edit, demote or remove the owner — or another admin, if you choose.
  • Assigning a role: nobody may hand out a role above their own.

Without these, an admin with tenants.members.update can promote themselves to owner.

Load memberships once

Permission checks run on almost every request. Resolve the caller’s membership (with its role) once in the tenant middleware and reuse it. Outside a request, cache memberships briefly and invalidate on member or role saves, or a revoked permission keeps working until the cache expires.

Mirror it in the UI — but only mirror

In Next.js, a usePermissions() hook that implements the same wildcard rules lets the interface hide what the user cannot do. It is a convenience. The API is the check that counts.

Plans add a second layer: a member may have permission to invite people while the workspace’s plan has no seats left. Gate the UI on both — permission and plan limit — and enforce both on the API.

Checklist

  • Permissions on the membership, not the user
  • app.resource.action strings with wildcard support
  • Per-member additions and revocations
  • Permission derived from the ViewSet; manual checks in plain APIViews
  • One catalog; default roles built from it; back-fill command; tests that the UI’s strings exist
  • Role-level checks for managing members and assigning roles
  • Membership loaded once per request; cache invalidated on change
  • UI mirrors permissions and plan limits; API enforces both

SaaS Dock ships this model end to end — catalog, default roles, wildcard matching, per-member overrides, role-level guards and the matching Next.js hooks. Read how it fits into multi-tenancy with Django and Next.js.

Frequently asked questions

Why not use Django's built-in permissions for a multi-tenant SaaS?
Django's auth permissions are global: a user either has a permission or not, everywhere. In a multi-tenant SaaS the same person can be an owner in one workspace and a viewer in another, so permissions have to live on the membership (user + tenant), not on the user.
How do wildcard permissions work?
A permission is a string like billing.invoices.list. A role can hold an exact string or a pattern: billing.* grants every billing permission, billing.invoices.* every action on invoices, and *.*.list every list action in every app. The owner role holds *.* — everything.