scope-util.ts 3.9 KB

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