@aooth/arbac
包括非洲区域局在内的电池——再出口 @aooth/arbac-core 并增加了一个流利的角色建设者、可重复使用的特权工厂、范围相接的公用事业,以及用于打字资源/行动联盟的代码CLI.
安装
pnpm add @aooth/arbac
文档
完整文件、 API 参考文献和食谱: https://aooth.moost.org/arbac/* (中文(简体) )* 联合国
拒绝是二进制( 资源, 动作)
.deny(resource, action) 整个短路 (resource, action) tuple——没有范围或过滤参数。 一旦一个拒绝规则的行动 regex匹配, evaluate() 返回时 { allowed: false } 在任何其他角色出现之前 allow 范围甚至被构建(参见 arbac-core/src/arbac.ts:126-131) (中文(简体) ).
如果你需要"藏在条件X的行",把它表示为一种 allow 带有排除X的过滤器,而不是部分否认:
// ✗ Not supported — no row-level deny.
// .deny("tasks", "query", { scope: { filter: { status: "secret" } } })
// ✓ Use a filtered allow instead.
allowTableRead<UserAttrs, Scope>("tasks", {
scope: (attrs) => ({
filter: { tenantId: attrs.tenantId, status: { $ne: "secret" } },
}),
});
被钉住 UNION-03 财务报告和审定财务报表 UNION-05 内 packages/e2e-demo/test/arbac-union.spec.ts ——无分镜否认短道速滑允许,通配符 .deny("tasks", "*") 403s 两行均markDone)和定时(new).
多重角色工会的范围 $or ——允许取胜
当一个用户各有多个角色时 allow 同样的 (resource, action),其范围通过 mergeScopeFilters (见 packages/arbac/src/scope/filter.ts) (中文(简体) ). 如果任何瞄准镜的滤镜匹配, 则一行匹配 。 后果: 如果一个范围没有过滤器( 规则注册没有 scope,或与 filter: undefined 编号 : filter: {}),联盟向宇宙扩张,对兄弟会范围限制较大的过滤器被默默地放下.
// Role "viewer" — tenant-scoped read on tasks.
.use(allowTableRead("tasks", {
scope: (attrs) => ({ filter: { tenantId: attrs.tenantId } }),
}))
// Role "tasks-everywhere" — unscoped read on tasks (e.g., for an audit role).
.allow("tasks", "query") // no scope ← contributes universe to the union
// A user with both roles sees ALL tasks (universe wins), not just their tenant.
这是自然的 $or 语义:"租行"与"一行"的结合是"一行". 外出:不要给其他角色试图过滤的资源提供无范围许可. 被钉住 arbacRowFilter 未定义的强制测试 packages/arbac-moost/src/db/shared-read-helpers.spec.ts。 。 。 .
许可证
麻省理工学院