原子授权
是基于数据的授权。本章节讲述原子授权
的基本概念和用法,更详细内容请参见:CabloyJS:原子授权
授权内容
针对原子类型
的原子指令
授权,如以下授权记录
角色 | 原子类型 | 原子指令 |
---|---|---|
system | party | create |
数据范围授权
在授权时可以指定权限的数据范围,如以下授权记录
角色 | 原子类型 | 原子指令 | 数据范围 |
---|---|---|---|
system | party | read | 财务部 |
角色
system
仅能读取财务部
创建的party
数据
授权途径
原子授权
有三种途径。在这里仅通过初始授权
途径,给相关的角色分配合适的初始权限
1. 授权记录
角色 | 原子类型 | 原子指令 | 数据范围 |
---|---|---|---|
system | party | create | |
system | party | read | authenticated |
system | party | write | 自己的数据 |
system | party | delete | 自己的数据 |
system | party | clone | 自己的数据 |
system | party | deleteBulk | |
system | party | exportBulk |
2. 授权代码
src/suite-vendor/test-party/modules/test-party/backend/src/bean/version.manager.js
- 1async init(options) {
- 2 // init
- 3 if (options.version === 1) {
- 4 // add role rights
- 5 const roleRights = [
- 6 { roleName: 'system', action: 'create' },
- 7 { roleName: 'system', action: 'read', scopeNames: 'authenticated' },
- 8 { roleName: 'system', action: 'write', scopeNames: 0 },
- 9 { roleName: 'system', action: 'delete', scopeNames: 0 },
- 10 { roleName: 'system', action: 'clone', scopeNames: 0 },
- 11 { roleName: 'system', action: 'deleteBulk' },
- 12 { roleName: 'system', action: 'exportBulk' },
- 13 ];
- 14 await this.ctx.bean.role.addRoleRightBatch({ atomClassName: 'party', roleRights });
- 15 }
- 16
- 17 }
授权判断
可以通过中间件
或Api
进行授权的判断,这里仅演示中间件
的判断方式:
src/suite-vendor/test-party/modules/test-party/backend/src/routes.js
- 1 // test/atom/right(checked by middleware)
- 2 {
- 3 method: 'post',
- 4 path: 'test/atom/checkRightCreate',
- 5 controller: 'testAtomRight',
- 6 middlewares: 'test',
- 7 meta: { right: { type: 'atom', action: 'create' } },
- 8 },
- 9 {
- 10 method: 'post',
- 11 path: 'test/atom/checkRightRead',
- 12 controller: 'testAtomRight',
- 13 middlewares: 'test',
- 14 meta: { right: { type: 'atom', action: 'read' } },
- 15 },
- 16 {
- 17 method: 'post',
- 18 path: 'test/atom/checkRightWrite',
- 19 controller: 'testAtomRight',
- 20 middlewares: 'test',
- 21 meta: { right: { type: 'atom', action: 'write' } },
- 22 },
- 23 {
- 24 method: 'post',
- 25 path: 'test/atom/checkRightAction',
- 26 controller: 'testAtomRight',
- 27 middlewares: 'test',
- 28 meta: { right: { type: 'atom', action: 'partyOver' } },
- 29 },
名称 | 说明 |
---|---|
meta | 路由的元数据,可以指定与中间件相关的参数 |
right | 全局中间件right 的参数 |
type | 授权类型,这里是原子 授权 |
action | 需要进行授权验证的原子指令 代码/名称 |
评论: