base.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const resources = [
  2. { value: 'art', label: '作品' },
  3. { value: 'art-daily', label: '每日一图' },
  4. { value: 'art-album', label: 'Art Album' },
  5. { value: 'category', label: '分类管理' },
  6. { value: 'translate', label: '多语翻译' },
  7. { value: 'user', label: '用户' },
  8. { value: 'role', label: '角色' },
  9. ];
  10. const actions = [
  11. { value: 'read', label: '查看' },
  12. { value: 'create', label: '创建' },
  13. { value: 'update', label: '更新' },
  14. { value: 'delete', label: '删除' },
  15. ];
  16. const possessions = [
  17. { value: 'own', label: '自己的' },
  18. { value: 'any', label: '全部' },
  19. ];
  20. const resourcesHash = resources.reduce((h, cur) => { h[cur.value] = cur.label; return h; }, {});
  21. const actionsHash = actions.reduce((h, cur) => { h[cur.value] = cur.label; return h; }, {});
  22. const possessionsHash = possessions.reduce((h, cur) => { h[cur.value] = cur.label; return h; }, {});
  23. /**
  24. * 将grant信息翻译成人可读的
  25. * @param {*} grants
  26. */
  27. function translate(grant) {
  28. let { resource, action, possession, attributes } = grant;
  29. resource = resourcesHash[resource] || resource;
  30. action = actionsHash[action] || action;
  31. possession = possessionsHash[possession] || possession;
  32. return { resource, action, possession, attributes };
  33. }
  34. function fullGrants() {
  35. let grants = [];
  36. resources.forEach(res => {
  37. actions.forEach(act => {
  38. grants.push({
  39. resource: res.value,
  40. action: act.value,
  41. possession: 'any',
  42. attributes: '*',
  43. })
  44. });
  45. })
  46. return grants;
  47. }
  48. module.exports = { resources, actions, possessions, translate, fullGrants }