# @aooth/arbac 中文文档

规范地址：https://npmcn.com/package/%40aooth/arbac/

版本：0\.2\.6
npm 元数据更新时间：2026-11-06T20:48:39.355Z
中文译文更新时间：2026-11-06T21:04:40.708Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40aooth/arbac
原文对照：https://npmcn.com/package/%40aooth/arbac/?lang=original
项目主页：https://github.com/moostjs/aoothjs/tree/main/packages/arbac#readme
源码仓库：https://github.com/moostjs/aoothjs

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：162 kB，8 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2026-05-17T21:03:27.159Z
- 最新版本发布：2026-11-06T20:48:39.142Z
- 近 30 天下载量：2335（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：92（近 12 个月 92 个）

## 中文简介

电池 -- -- 包括美国航天局:建筑商API、特权工厂、范围合并公用事业

## 安装

```sh
npm install @aooth/arbac
```

也可以使用：`pnpm add @aooth/arbac`、`yarn add @aooth/arbac`、`bun add @aooth/arbac`

## README

[![aoothjs](https://aooth.moost.org/logo.svg)](https://aooth.moost.org/)

# @aooth/arbac

包括非洲区域局在内的电池——再出口 `@aooth/arbac-core` 并增加了一个流利的角色建设者、可重复使用的特权工厂、范围相接的公用事业,以及用于打字资源/行动联盟的代码CLI.

[**文 件 ∗∗**](https://aooth.moost.org/arbac/)

* * *

## 安装

```bash
pnpm add @aooth/arbac
```

## 文档

完整文件、 API 参考文献和食谱: **[https://aooth.moost.org/arbac/\* (中文(简体) )](https://aooth.moost.org/arbac/*)\* 联合国**

-   [概念](https://aooth.moost.org/arbac/concepts)
-   [角色构建者](https://aooth.moost.org/arbac/builder)
-   [私人工厂](https://aooth.moost.org/arbac/privileges)
-   [范围合并](https://aooth.moost.org/arbac/scopes)
-   [代码CLI](https://aooth.moost.org/arbac/codegen)
-   [API 参考](https://aooth.moost.org/api/arbac)

## 拒绝是二进制( 资源, 动作)

`.deny(resource, action)` 整个短路 `(resource, action)` tuple——没有范围或过滤参数。 一旦一个拒绝规则的行动 regex匹配, `evaluate()` 返回时 `{ allowed: false }` 在任何其他角色出现之前 `allow` 范围甚至被构建(参见 `arbac-core/src/arbac.ts:126-131`) (中文(简体) ).

如果你需要"藏在条件X的行",把它表示为一种 `allow` 带有排除X的过滤器,而不是部分否认:

```ts
// ✗ 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: {}`),联盟向宇宙扩张,对兄弟会范围限制较大的过滤器被默默地放下.

```ts
// 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`。 。 。 .

## 许可证

麻省理工学院

## 常见问题

### @aooth/arbac 是什么？

电池 -- -- 包括美国航天局:建筑商API、特权工厂、范围合并公用事业

### 如何安装 @aooth/arbac？

在项目目录运行 npm install @aooth/arbac，或使用 pnpm add / yarn add / bun add。它还提供命令行工具 aoothjs-arbac-codegen，可以用 npx aoothjs-arbac-codegen 直接运行。

### @aooth/arbac 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @aooth/arbac 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @aooth/arbac 可以商用吗？

@aooth/arbac 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @aooth/arbac 的下载量有多少？

根据 npm 官方下载统计，@aooth/arbac 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,335 次，日均约 78 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @aooth/arbac 还在维护吗？

最新版本 0\.2\.6 发布于 2026/11/07（今天）；至今共发布 92 个版本，其中过去 12 个月发布了 92 个；首次发布于 2026/05/18。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

