scope.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // If you want to add a new scope, you only need to add a new key to the ORIGINAL_SCOPE object.
  2. export const ORIGINAL_SCOPE_ADMIN = {
  3. admin: {
  4. top: {},
  5. app: {},
  6. security: {},
  7. markdown: {},
  8. customize: {},
  9. import_data: {},
  10. exporet_data: {},
  11. data_transfer: {},
  12. external_notification: {},
  13. slack_integration: {},
  14. legacy_slack_integration: {},
  15. user_management: {},
  16. user_group_management: {},
  17. audit_log: {},
  18. plugin: {},
  19. ai_integration: {},
  20. full_text_search: {},
  21. },
  22. } as const;
  23. export const ORIGINAL_SCOPE_USER = {
  24. user: {
  25. info: {},
  26. external_account: {},
  27. password: {},
  28. api: {
  29. api_token: {},
  30. access_token: {},
  31. },
  32. in_app_notification: {},
  33. other: {},
  34. },
  35. base: {
  36. ai_assistant: {},
  37. page: {},
  38. share_link: {},
  39. bookmark: {},
  40. questionnaire: {},
  41. },
  42. } as const;
  43. export const ORIGINAL_SCOPE = {
  44. ...ORIGINAL_SCOPE_ADMIN,
  45. ...ORIGINAL_SCOPE_USER,
  46. } as const;
  47. export const ACTION = {
  48. READ: 'read',
  49. WRITE: 'write',
  50. } as const;
  51. type ACTION_TYPE = typeof ACTION[keyof typeof ACTION];
  52. export const ALL_SIGN = '*';
  53. export const ORIGINAL_SCOPE_WITH_ACTION = Object.values(ACTION).reduce(
  54. (acc, action) => {
  55. acc[action] = ORIGINAL_SCOPE;
  56. return acc;
  57. },
  58. {} as Record<ACTION_TYPE, typeof ORIGINAL_SCOPE>,
  59. );
  60. type FlattenObject<T> = {
  61. [K in keyof T]: T[K] extends object
  62. ? (keyof T[K] extends never
  63. ? K
  64. : `${K & string}:${FlattenObject<T[K]> & string}`)
  65. : K
  66. }[keyof T];
  67. type AddAllToScope<S extends string> =
  68. S extends `${infer X}:${infer Y}`
  69. ? `${X}:${typeof ALL_SIGN}` | `${X}:${AddAllToScope<Y>}` | S
  70. : S;
  71. type ScopeOnly = FlattenObject<typeof ORIGINAL_SCOPE_WITH_ACTION>;
  72. type ScopeWithAll = AddAllToScope<ScopeOnly>;
  73. export type Scope = ScopeOnly | ScopeWithAll;
  74. // ScopeConstantsの型定義
  75. type ScopeConstantLeaf = Scope;
  76. type ScopeConstantNode<T> = {
  77. [K in keyof T as Uppercase<string & K>]: T[K] extends object
  78. ? (keyof T[K] extends never
  79. ? ScopeConstantLeaf
  80. : ScopeConstantNode<T[K]> & { ALL: Scope })
  81. : ScopeConstantLeaf
  82. };
  83. type ScopeConstantType = {
  84. [A in keyof typeof ORIGINAL_SCOPE_WITH_ACTION as Uppercase<string & A>]:
  85. ScopeConstantNode<typeof ORIGINAL_SCOPE> & { ALL: Scope }
  86. };
  87. const buildScopeConstants = (): ScopeConstantType => {
  88. const result = {} as Partial<ScopeConstantType>;
  89. const processObject = (obj: Record<string, any>, path: string[] = [], resultObj: Record<string, any>) => {
  90. Object.entries(obj).forEach(([key, value]) => {
  91. const upperKey = key.toUpperCase();
  92. const currentPath = [...path, key];
  93. const scopePath = currentPath.join(':');
  94. if (value == null) {
  95. return;
  96. }
  97. if (typeof value === 'object' && Object.keys(value).length === 0) {
  98. resultObj[upperKey] = `${scopePath}` as Scope;
  99. }
  100. else if (typeof value === 'object') {
  101. resultObj[upperKey] = {
  102. ALL: `${scopePath}:${ALL_SIGN}` as Scope,
  103. };
  104. processObject(value, currentPath, resultObj[upperKey]);
  105. }
  106. });
  107. };
  108. processObject(ORIGINAL_SCOPE_WITH_ACTION, [], result);
  109. return result as ScopeConstantType;
  110. };
  111. export const SCOPE = buildScopeConstants();