Organizations
使用 Federation 管理全局或 Bureau 作用域的 Organization 与成员关系
Organizations
OrganizationsService 管理 Organization、Membership、Join Request 和组织治理角色。Federation 是这些关系的唯一权威来源。
Organization 不保存产品后端地址,也不管理 Space、文档、Repository 等产品资源。Bureau 从产品可信配置取得 Bureau 地址,并携带当前 user_token 请求 Bureau。Bureau 验证用户身份、查询 Membership,再执行自己的资源权限。
- 客户端和产品后端 API:Organizations Federation API
- 产品后端接入:Bureau 接入 Organizations
安装
import { Federation } from "@downcity/bureau";
import { OrganizationsService } from "@downcity/services";
const federation = new Federation({ database });
federation.use(new OrganizationsService({
max_organizations_per_user: 3,
}));额度统计用户作为 Owner 持有的全部 active Organization,不按 Bureau 重复计算。归档会释放额度,Owner 转移会把额度交给新 Owner。
Organization 作用域
创建 Federation 全局 Organization:
await bureau.service("organizations").action("create").invoke({
name: "Genesis Research",
scope_type: "federation",
});创建当前 Bureau Organization:
await bureau.service("organizations").action("create").invoke({
name: "Genesis Research",
scope_type: "bureau",
});federation Organization 可以由同一 Federation 下任意 Bureau Token 中的成员访问。bureau Organization 只接受 user_token.bureau_id 与 scope_bureau_id 相同的请求。Bureau 作用域从已验证 Token 写入,客户端不能提交或覆盖 scope_bureau_id。
全局不等于公开。Organizations Service 不提供公开搜索;用户仍需通过邀请链接、二维码或产品流程取得 organization_id。
Membership
Membership 表示一个 User 与 Organization 之间的一段成员关系。每次成功加入都会生成新的 membership_id。退出或被移除后,旧记录永久保持 removed;重新加入会创建另一条 Membership。
await bureau.service("organizations").action("join-requests/create").invoke({
organization_id: "org_01J...",
});Owner、Admin、Member 只表达 Organization 治理角色,不代表产品中的 editor、viewer 或 repository push 权限。
调用产品 Bureau
Bureau 把同一个 user_token 发送给产品预先配置的 HTTPS Bureau:
await bureau.post("https://bureau.example.com/v1/projects", {
organization_id: "org_01J...",
});Bureau 使用 Federation JWKS 验证 Token,并通过 membership/get 查询当前 Membership。Organizations Service 不再签发 organization_token,也不向用户提供的 URL 投递撤权事件。
数据库与归档
Organizations Service 支持 better-sqlite3、PostgreSQL 与 Cloudflare D1。三种 Runtime 统一使用 context.transaction();Service 不判断数据库类型。
Organization 归档不可恢复。归档会停止治理写入并释放 Owner 额度。产品资源是否保留、归档或删除,由 Bureau 决定。