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
- rrezartprebreza/spring-boot-skillstheir own version, first seen Aug 7, 2026
- param087/saas-starter-skillstheir own version, first seen Jun 6, 2026
- Iteam-company/claude-code-boilerplatestheir own version, first seen May 19, 2026
- YepAPI/skillstheir own version, first seen Apr 12, 2026
Other versions4 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 4
- rrezartprebreza/spring-boot-skills★ 293same name
- YepAPI/skills★ 8same name
- Iteam-company/claude-code-boilerplates★ 2same name
- param087/saas-starter-skills★ 2same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository49 other files it gives its agents.
- accessibilitySkill
- ai-evalsSkill
- ai-integrationSkill
- architecture-decisionsSkill
- audit-loggingSkill
- aws-servicesSkill
- browser-e2e-testingSkill
- celery-tasksSkill
- changelogSkill
- ci-cdSkill
- codebase-guideSkill
- code-reviewSkill
- commit-messageSkill
- cookie-auth-csrfSkill
- dashboard-data-designSkill
- db-concurrencySkill
- debugSkill
- dependency-auditSkill
- deploy-awsSkill
- design-systemSkill
- drf-apiSkill
- drf-zod-contractSkill
- frontend-perfSkill
- git-workflowSkill
- i18n-rtlSkill
- incident-responseSkill
- migrationsSkill
- money-decimalSkill
- nextjs-moduleSkill
- observabilitySkill
- perf-reviewSkill
- production-readinessSkill
- product-ux-designSkill
- rbac-permissionsSkill
- react-querySkill
- security-reviewSkill
- skill-authoringSkill
- tailwind-shadcnSkill
- tenant-session-switchSkill
- threat-modelingSkill
- verifySkill
- version-checkSkill
- visual-ui-designSkill
- webhook-handlerSkill
- websockets-channelsSkill
- write-testsSkill
- zod-formsSkill
- zustand-stateSkill
- example-skillSkill
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
- Very specific: line 60
Add a tenant-isolation test for every scoped resource (see write-tests): user A must get 404/empty for user B's object.
- Distinctive: line 64
Keep the invariant; change only what "the current tenant" resolves to.
“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.
Files
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:
- Every business model carries a non-null tenant FK (
entreprise). No nulls, no "global" business rows. - 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. - 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
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 itSubclass one fail-closed mixin instead of hand-writing
get_querysetper 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)- Object lookups reuse the scoped queryset, so per-object 404 is automatic —
never
Model.objects.get(pk=...)in a view (that bypasses scoping). - 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_idis 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()andperform_createstamping (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/departementFK 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
entrepriserows; resolve "current tenant" from anX-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/tenantid in the body or query string is never trusted — scope fromrequest.useronly. .none()on the empty/anonymous case, not an unfiltered queryset — fail closed.- Custom
@actionmethods and nested/related lookups need the same scoping; the mixin only covers the default queryset (seerbac-permissionsto gate actions). - The tenant filter prevents leaks, not N+1 — add
select_related/prefetch_relatedin the serializer (seeperf-review).
See also
rbac-permissionsdrf-apimigrationssecurity-reviewwrite-tests
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
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.

