@ casbinjs/ core (中文(简体) )
框架不可预见授权核心 卡斯宾JS完整的实例,包括基于角色的获取、资源分组、行动继承和框架整合,见 主读取器组合 笼盖核心 使用干净的ASync API进行许可检查和运行时间政策管理.
安装
npm install @casbinjs/core
使用量
创建授权者
import { createAuthorizer } from '@casbinjs/core';
const authorizer = await createAuthorizer({
subject: 'alice',
organization: 'org-1',
model: `
[request_definition]
r = sub, res, org, act
[policy_definition]
p = sub, res, org, act, eft
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && r.org == p.org && r.res == p.res && r.act == p.act
`,
policies: [
['p', 'alice', 'document:123', 'org-1', 'read', 'allow'],
],
});
检查权限
await authorizer.can('read', 'document:123'); // true
await authorizer.canAny(['read', 'write'], 'document:123'); // true
await authorizer.canAll(['read', 'write'], 'document:123'); // false
运行时改变政策
await authorizer.addPolicy(['p', 'alice', 'document:123', 'org-1', 'write', 'allow']);
await authorizer.removePolicy(['p', 'alice', 'document:123', 'org-1', 'write', 'allow']);
await authorizer.replacePolicies([
['p', 'alice', 'document:456', 'org-1', 'read', 'allow'],
]);
API 密码
createAuthorizer(options?)
创建并初始化一个 Authorizer 举个例子.
| 选项 | 类型 | 说明 |
|---|---|---|
model | string | 卡斯宾模型定义. 回到一个基础 sub, obj, act 如果省略则使用模型. |
policies | string[][] | 初步政策规则. |
subject | string | 当前用户/ 接受权限检查的主体 . |
organization | string | 许可检查的组织范围. |
Authorizer
| 方法 | 签署 | 说明 |
|---|---|---|
can | (action, resource) => Promise<boolean> | 请检查access-date=中的日期值 (帮助). |
canAny | (actions, resource) => Promise<boolean> | 回返 true 如果至少允许一个动作. |
canAll | (actions, resource) => Promise<boolean> | 回返 true 只有允许一切行动. |
addPolicy | (policy) => Promise<void> | 增加政策规则. |
removePolicy | (policy) => Promise<void> | 删除政策规则 . |
replacePolicies | (policies) => Promise<void> | 从解剖角度取代所有政策. |
getEnforcer | () => Enforcer | null | 进入核心执行器. |