agentleFS
DeadlymindDeadlymind/nanolama/skills/multi-tenancy/SKILL.md

multi-tenancy

Skill1 starsChanged 5 months agoMITView on GitHub

Enforces tenant isolation on a Django/DRF app where every business model carries a non-null tenant FK (entreprise) and ViewSets auto-filter fail-closed by the current tenant. Use when adding a tenant-scoped model, writing or reviewing a ViewSet or queryset, wiring a TenantScopedViewSet mixin, fixing a cross-tenant data leak, or asking how entreprise scoping works here. Not for role/permission gating (see rbac-permissions) or generic serializer/router setup (see drf-api).

4 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Add a tenant-isolation test for every scoped resource (see write-tests): user A must get 404/empty for user B's object.

    line 60
  2. Distinctive:

    Keep the invariant; change only what "the current tenant" resolves to.

    line 64

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Multi-tenancy (fail-closed tenant isolation)

When to use

Adding or reviewing any business model or API that must never expose one tenant's rows to another. On this stack, tenant isolation is the single most important invariant — treat a missing tenant filter as a security bug, not a style nit.

Pattern

Three rules, held everywhere:

  1. Every business model carries a non-null tenant FK (entreprise). No nulls, no "global" business rows.
  2. Reads are scoped in get_queryset(), fail-closed. The tenant comes from the authenticated user, never from the request body/query string. No tenant → return .none(), never all rows.
  3. Writes set the tenant server-side in perform_create/perform_update, so a client can never assign a row to another tenant.

Access within a tenant is a separate concern — gate it with rbac-permissions.

Steps / idioms

  1. Give the model a non-null tenant FK and a related name:

    class Invoice(models.Model):
        entreprise = models.ForeignKey(
            "tenants.Entreprise", on_delete=models.CASCADE,
            null=False, related_name="invoices",
        )
        # ... business fields
        class Meta:
            indexes = [models.Index(fields=["entreprise"])]  # every filter hits it
  2. Subclass one fail-closed mixin instead of hand-writing get_queryset per view:

    # tenants/mixins.py
    class TenantScopedViewSet(viewsets.ModelViewSet):
        tenant_field = "entreprise_id"                     # override for indirect ownership
    
        def get_queryset(self):
            user = self.request.user
            tenant_id = getattr(user, "entreprise_id", None)
            if not user.is_authenticated or tenant_id is None:
                return super().get_queryset().none()      # fail closed
            return super().get_queryset().filter(**{self.tenant_field: tenant_id})
    
        def perform_create(self, serializer):
            serializer.save(entreprise=self.request.user.entreprise)
  3. Object lookups reuse the scoped queryset, so per-object 404 is automatic — never Model.objects.get(pk=...) in a view (that bypasses scoping).
  4. Add a tenant-isolation test for every scoped resource (see write-tests): user A must get 404/empty for user B's object.

Variants

Keep the invariant; change only what "the current tenant" resolves to.

  • One entreprise = one company (default). user.entreprise_id is the tenant.
  • Indirect ownership (tenant reached through a parent). Some models have no direct tenant FK — they belong to one through a parent row. Make the mixin field configurable with a class attribute (tenant_field = "entreprise_id" by default) and filter on it (.filter(**{self.tenant_field: tenant_id})). For an indirectly owned model, set the traversal path, e.g. tenant_field = "parent__entreprise". The fail-closed .none() and perform_create stamping (via the parent) still apply — and index the parent FK the path joins on, or every scoped read scans.
  • Sub-teams / departments inside a tenant. Tenant FK still gates isolation; add a second, optional team/departement FK and layer it as an RBAC/visibility filter on top of the tenant filter — never as a replacement for it.
  • Agency operating over client sub-accounts. Model the client as the tenant and give agency users an explicit, audited membership across several entreprise rows; resolve "current tenant" from an X-Tenant-style selector validated against that membership, then filter by it. Still fail-closed: an unknown/foreign tenant → .none().

Adapt to your repo

Rename Entreprise/entreprise, the accessor path (user.entreprise_id vs user.profile.entreprise_id), and the app label to match your project. If your tenant is resolved from a request header (agency variant), validate it against server-side membership before trusting it. Confirm the FK is null=False in the migration.

Gotchas

  • A client-supplied entreprise/tenant id in the body or query string is never trusted — scope from request.user only.
  • .none() on the empty/anonymous case, not an unfiltered queryset — fail closed.
  • Custom @action methods and nested/related lookups need the same scoping; the mixin only covers the default queryset (see rbac-permissions to gate actions).
  • The tenant filter prevents leaks, not N+1 — add select_related/prefetch_related in the serializer (see perf-review).

See also

  • rbac-permissions
  • drf-api
  • migrations
  • security-review
  • write-tests

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.