SaaS

Organisations, memberships, and a boot-enforced boundary that stops one tenant reading another's data.

Wiring

funktor(
    config = config,
    auth = { useKarango() },
    saas = { useKarango() },   // or useMonko()
)

This registers the organisation and membership repositories, the orgs API, and — importantly — the isolation boot check and request guard described below.

Storage

Two storages, both backend-agnostic (Karango or Monko):

orgs.findBySlug("acme")
orgs.ensureBySlug(slug = "acme", name = "Acme Inc")
orgs.create(organisation)
orgs.save(organisation)

orgMembers.sessionMembershipsOf(UserId(user._id))   // -> Set<OrgMembership>

An organisation carries a slug, a name and an OrgStatus. Only Active organisations should be sign-in-able — see Auth for how a realm turns memberships into a session.

Slugs are subdomain-safe

Slugs end up in hostnames, so Slugs validates them for that: 2–63 characters, and normalize() trims and lower-cases. validationError(slug) returns a human-readable reason rather than a bare boolean, so an API can say what was wrong.

Org isolation — the part that matters

Two interfaces turn tenant isolation from a convention into something the framework enforces.

// 1. Mark the entity as owned by an organisation
data class Invoice(
    override val org: Ref<Organisation>,
    val total: Money,
) : OrgAware

// 2. The route's params must carry the addressed organisation
data class InvoiceParams(
    override val org: Stored<Organisation>,
    val invoice: Stored<Invoice>,
) : OrgAwareParam

Marking an entity OrgAware is the developer's opt-in. Once marked:

  • At boot, any route resolving that entity is forced to be OrgAwareParam — a route that loads an org-owned entity without knowing which organisation the request addresses fails app start, not review.
  • At request time, the guard compares each loaded entity's org reference against the request's organisation. A mismatch is refused.
The boot check is the interesting half. A runtime guard only protects the routes someone remembered to route through it; a boot check protects the ones they forgot, which are the ones that leak.

The one contract to get right

OrgAware.org must carry the organisation's canonical _id (collection-qualified, e.g. organisation/acme), because the guard compares on _id. A persisted Ref<Organisation> satisfies this automatically. If you construct one by hand, use _id — never a bare _key.

Getting it wrong fails closed: every same-org request 404s. That makes it an availability landmine rather than a data leak, which is the right direction to be wrong in — but it is a confusing outage, so get it right.

Enforcement lives in the saas module, not in the marker interfaces. An app that uses OrgAware without installing funktor(saas = { … }) gets the interfaces and none of the checks.