scope-util.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { ALL_SIGN, type Scope } from '@growi/core/dist/interfaces';
  2. // Data structure for the final merged scopes
  3. // biome-ignore lint/suspicious/noTsIgnore: Suppress auto fix by lefthook
  4. // @ts-ignore - Scope type causes "Type instantiation is excessively deep" with tsgo
  5. interface ScopeMap {
  6. [key: string]: Scope | ScopeMap;
  7. }
  8. // Input object with arbitrary action keys (e.g., READ, WRITE)
  9. type ScopesInput = Record<string, any>;
  10. function parseSubScope(
  11. parentKey: string,
  12. subObjForActions: Record<string, any>,
  13. actions: string[],
  14. ): ScopeMap {
  15. const result: ScopeMap = {};
  16. for (const action of actions) {
  17. if (typeof subObjForActions[action] === 'string') {
  18. // Safe: parseScopes only accepts SCOPE constant which contains valid Scope strings
  19. result[`${action.toLowerCase()}:${parentKey.toLowerCase()}`] =
  20. subObjForActions[action] as Scope;
  21. subObjForActions[action] = undefined;
  22. }
  23. }
  24. const childKeys = new Set<string>();
  25. for (const action of actions) {
  26. const obj = subObjForActions[action];
  27. if (obj && typeof obj === 'object') {
  28. Object.keys(obj).forEach((k) => {
  29. childKeys.add(k);
  30. });
  31. }
  32. }
  33. for (const ck of childKeys) {
  34. if (ck === 'ALL') {
  35. for (const action of actions) {
  36. const val = subObjForActions[action]?.[ck];
  37. if (typeof val === 'string') {
  38. // Safe: parseScopes only accepts SCOPE constant which contains valid Scope strings
  39. result[`${action.toLowerCase()}:${parentKey.toLowerCase()}:all`] =
  40. val as Scope;
  41. }
  42. }
  43. continue;
  44. }
  45. const newKey = `${parentKey}:${ck}`;
  46. const childSubObj: Record<string, any> = {};
  47. for (const action of actions) {
  48. childSubObj[action] = subObjForActions[action]?.[ck];
  49. }
  50. result[newKey] = parseSubScope(newKey, childSubObj, actions);
  51. }
  52. return result;
  53. }
  54. export function parseScopes({
  55. scopes,
  56. isAdmin = false,
  57. }: {
  58. scopes: ScopesInput;
  59. isAdmin?: boolean;
  60. }): ScopeMap {
  61. const actions = Object.keys(scopes);
  62. const topKeys = new Set<string>();
  63. // Collect all top-level keys (e.g., ALL, ADMIN, USER) across all actions
  64. for (const action of actions) {
  65. Object.keys(scopes[action] || {}).forEach((k) => {
  66. topKeys.add(k);
  67. });
  68. }
  69. const result: ScopeMap = {};
  70. for (const key of topKeys) {
  71. // Skip 'ADMIN' key if isAdmin is true
  72. if (!isAdmin && (key === 'ADMIN' || key === 'ALL')) {
  73. continue;
  74. }
  75. if (key === 'ALL') {
  76. const allObj: ScopeMap = {};
  77. for (const action of actions) {
  78. const val = scopes[action]?.[key];
  79. if (typeof val === 'string') {
  80. // Safe: parseScopes only accepts SCOPE constant which contains valid Scope strings
  81. allObj[`${action.toLowerCase()}:all`] = val as Scope;
  82. }
  83. }
  84. result.ALL = allObj;
  85. } else {
  86. const subObjForActions: Record<string, any> = {};
  87. for (const action of actions) {
  88. subObjForActions[action] = scopes[action]?.[key];
  89. }
  90. result[key] = parseSubScope(key, subObjForActions, actions);
  91. }
  92. }
  93. return result;
  94. }
  95. /**
  96. * Determines which scopes should be disabled based on wildcard selections
  97. */
  98. export function getDisabledScopes(
  99. selectedScopes: Scope[],
  100. availableScopes: string[],
  101. ): Set<Scope> {
  102. const disabledSet = new Set<Scope>();
  103. // If no selected scopes, return empty set
  104. if (!selectedScopes || selectedScopes.length === 0) {
  105. return disabledSet;
  106. }
  107. selectedScopes.forEach((scope) => {
  108. // Check if the scope is in the form `xxx:*`
  109. if (scope.endsWith(`:${ALL_SIGN}`)) {
  110. // Convert something like `read:*` into the prefix `read:`
  111. const prefix = scope.replace(`:${ALL_SIGN}`, ':');
  112. // Disable all scopes that start with the prefix (but are not the selected scope itself)
  113. availableScopes.forEach((s: Scope) => {
  114. if (s.startsWith(prefix) && s !== scope) {
  115. disabledSet.add(s);
  116. }
  117. });
  118. }
  119. });
  120. return disabledSet;
  121. }
  122. /**
  123. * Extracts all scope strings from a nested ScopeMap object
  124. */
  125. export function extractScopes(obj: Record<string, any>): string[] {
  126. let result: string[] = [];
  127. Object.values(obj).forEach((value) => {
  128. if (typeof value === 'string') {
  129. result.push(value);
  130. } else if (typeof value === 'object' && !Array.isArray(value)) {
  131. result = result.concat(extractScopes(value));
  132. }
  133. });
  134. return result;
  135. }