Files
marqdo/ext/security/tenant.mq.md
cflmy 9331af78e2 release(2.8.0): 合入 CHANGELOG/README/发版说明/skills prove 任务;CI 链接指向 doc/releases
- CHANGELOG.md:v2.8.0 条目(中英双语);
- README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明);
- doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制);
- skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/
  require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过);
- 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
2026-11-18 10:29:26 +08:00

2.5 KiB
Executable File

markup, title, import
markup title import
occlusion Request tenant selection for database-backed web resources
plugin
lib/plugin.mq.md

Tenant selection identifies a request context; authentication and membership are separate decisions. Pair this leaf with explicit scoped routes in ext/data/api.mq.md. Use it before listening and retain the returned application after every binding. The implementation reuses the existing native tenant resolver.

attach

参数 类型 默认 说明
app any required Prepared application to receive tenant context
mode text "path" path, subdomain or header
param text "t" Path capture/query key or header name; use X-Tenant-Id explicitly for header mode
column text "tenant_id" Database column used by scoped queries
default_scope bool False Native default scoping for supported database-backed request handlers
返回 类型 说明
result any Updated application with native request tenant resolution

Use app, mode="path", param="t", column="tenant_id" and default_scope=False. Path mode first resolves the named route capture, then its query key, then the documented path segments. Subdomain mode uses the first label of a host with at least three labels. Header mode reads the selected header. The native resolver lowercases and keeps ASCII letters, digits, underscores and hyphens; it is not a general tenant-name codec.

Grant plugin and keep the returned application. A later scoped JSON route rejects a missing resolved tenant and adds the tenant-column filter. The default flag applies only to native handlers that support it; ordinary direct database calls do not gain automatic request context. Tenant ids arriving from query parameters, headers or hostnames must be checked against authenticated membership by the application's actual policy.

The local two-tenant example intentionally allows anonymous selection to demonstrate filtering. That behavior cannot establish production access isolation between users. No database row is changed and no socket opens in this binding. Develop resolver behavior in the native tenant package, and verify both selected tenants plus a request without a selector.

We native = Resolve the maintained tenant implementation and Load the existing request-context boundary. Return [Attach the explicit request tenant policy](web_app_tenant(app=app, mode=mode, param=param, column=column, default_scope=default_scope)).