Buuka ogende ku bigirimu

Architecture and tenant isolation

Tenant → company → user, a schema per tenant and row-level security per company.

The platform is a Django application on PostgreSQL. It consists of many apps that usually come in pairs: a domain app (tasks) and its settings app (tasks_settings).

Organisation data model

Tenant
└── Company                  data ownership, roles, booked modules
    └── CompanyUser          a user's membership in a company (role, groups, extra permissions)
User ── TenantUser           role in the tenant, personal settings (language, time zone, theme)

The business view for administrators is in Structure: tenant, companies, users.

Two levels of isolation

  1. A schema per tenant. The apps' tables live in the tenant's PostgreSQL schema. They are not created by Django migrations but by each app's schema_init.py, which a registry discovers when a tenant is created or updated.
  2. Row-level security per company. Every domain table has a company_id. PostgreSQL row-level security compares it with the active company from the session (app.current_company_id, app.current_user_id). Without a matching context a query sees nothing – even if the application code has a bug.
# tasks/schema_init.py (shortened)
from customers.rls import CURRENT_USER, child_of, company_scoped

RLS_POLICIES = [
    {"table": "tasks_tasks",
     "using": f"({company_scoped()}) AND ({{table}}.status <> 'draft' OR {{table}}.created_by_id = {CURRENT_USER})"},
] + [
    {"table": t, "using": child_of("tasks_tasks", "task_id")}
    for t in ("tasks_assignees", "tasks_comments", "tasks_subtasks")
]

child_of() makes child tables visible when the parent row is visible – and is fail-closed: if RLS is not active on the parent table, the children are invisible too.

Important: Row-level security separates companies. Who may see a record within a company (creator, assignees, groups, view_all) is checked by the app itself – for tasks in tasks/access.py.

Next: Anatomy of a module and The permission model.

Obuyambi & Obuwagizi

Tofunye ky'obadde onoonya? Pulatifomu n'ekifo eky'obuyambi biri wala kunyiga kumu kwokka.