原子授权是基于数据的授权。本章节讲述原子授权的基本概念和用法,更详细内容请参见: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

  1. 1async init(options) {
  2. 2 // init
  3. 3 if (options.version === 1) {
  4. 4 // add role rights
  5. 5 const roleRights = [
  6. 6 { roleName: 'system', action: 'create' },
  7. 7 { roleName: 'system', action: 'read', scopeNames: 'authenticated' },
  8. 8 { roleName: 'system', action: 'write', scopeNames: 0 },
  9. 9 { roleName: 'system', action: 'delete', scopeNames: 0 },
  10. 10 { roleName: 'system', action: 'clone', scopeNames: 0 },
  11. 11 { roleName: 'system', action: 'deleteBulk' },
  12. 12 { roleName: 'system', action: 'exportBulk' },
  13. 13 ];
  14. 14 await this.ctx.bean.role.addRoleRightBatch({ atomClassName: 'party', roleRights });
  15. 15 }
  16. 16
  17. 17 }

授权判断

可以通过中间件Api进行授权的判断,这里仅演示中间件的判断方式:

src/suite-vendor/test-party/modules/test-party/backend/src/routes.js

  1. 1 // test/atom/right(checked by middleware)
  2. 2 {
  3. 3 method: 'post',
  4. 4 path: 'test/atom/checkRightCreate',
  5. 5 controller: 'testAtomRight',
  6. 6 middlewares: 'test',
  7. 7 meta: { right: { type: 'atom', action: 'create' } },
  8. 8 },
  9. 9 {
  10. 10 method: 'post',
  11. 11 path: 'test/atom/checkRightRead',
  12. 12 controller: 'testAtomRight',
  13. 13 middlewares: 'test',
  14. 14 meta: { right: { type: 'atom', action: 'read' } },
  15. 15 },
  16. 16 {
  17. 17 method: 'post',
  18. 18 path: 'test/atom/checkRightWrite',
  19. 19 controller: 'testAtomRight',
  20. 20 middlewares: 'test',
  21. 21 meta: { right: { type: 'atom', action: 'write' } },
  22. 22 },
  23. 23 {
  24. 24 method: 'post',
  25. 25 path: 'test/atom/checkRightAction',
  26. 26 controller: 'testAtomRight',
  27. 27 middlewares: 'test',
  28. 28 meta: { right: { type: 'atom', action: 'partyOver' } },
  29. 29 },
名称 说明
meta 路由的元数据,可以指定与中间件相关的参数
right 全局中间件right的参数
type 授权类型,这里是原子授权
action 需要进行授权验证的原子指令代码/名称