@stoker-platform/utils 0.5.77 → 0.5.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ import { getAccessFields } from "./getAccessFields.js";
5
5
  import { getDependencyFields } from "./getDependencyFields.js";
6
6
  import { roleHasOperationAccess } from "../access/roleHasOperationAccess.js";
7
7
  import { isRelationField } from "./isRelationField.js";
8
+ import { fieldRoleProjectionAccess, getFieldAccessGroupFields } from "../access/fieldAccess.js";
8
9
  export const getRoleFields = (collection, role) => {
9
10
  const indexFields = [];
10
11
  const { fields, roleSystemFields, preloadCache, queries } = collection;
@@ -15,7 +16,7 @@ export const getRoleFields = (collection, role) => {
15
16
  required: true,
16
17
  });
17
18
  fields.forEach((field) => {
18
- if (!field.access || field.access.includes(role)) {
19
+ if (fieldRoleProjectionAccess(field, role)) {
19
20
  indexFields.push(field);
20
21
  }
21
22
  });
@@ -35,7 +36,7 @@ export const getRoleFields = (collection, role) => {
35
36
  const queryField = getField(fields.concat(systemFieldsSchema), query.field);
36
37
  if ((!query.roles || query.roles.includes(role)) &&
37
38
  queryField &&
38
- (!queryField.access || queryField.access?.includes(role)) &&
39
+ fieldRoleProjectionAccess(queryField, role) &&
39
40
  systemFields.includes(query.field)) {
40
41
  indexFields.push(getField(systemFieldsSchema, query.field));
41
42
  }
@@ -52,16 +53,6 @@ export const getDependencyAccessFields = (field, collection, schema) => {
52
53
  const indexFields = [];
53
54
  const { fields, preloadCache } = collection;
54
55
  const systemFieldsSchema = getSystemFieldsSchema();
55
- if (preloadCache?.range) {
56
- preloadCache.range.fields.forEach((field) => {
57
- if (systemFields.includes(field)) {
58
- indexFields.push(getField(systemFieldsSchema, field));
59
- }
60
- else {
61
- indexFields.push(getField(fields, field));
62
- }
63
- });
64
- }
65
56
  const dependentRoles = new Set();
66
57
  const dependentFields = getDependencyFields(collection, schema);
67
58
  Object.entries(dependentFields).map(([fieldName, collectionValues]) => {
@@ -76,6 +67,16 @@ export const getDependencyAccessFields = (field, collection, schema) => {
76
67
  dependentRoles.forEach((role) => {
77
68
  indexFields.push(...getAccessFields(collection, role));
78
69
  });
70
+ if (preloadCache?.range) {
71
+ preloadCache.range.fields.forEach((field) => {
72
+ if (systemFields.includes(field)) {
73
+ indexFields.push(getField(systemFieldsSchema, field));
74
+ }
75
+ else if (Array.from(dependentRoles).some((role) => fieldRoleProjectionAccess(getField(fields, field), role))) {
76
+ indexFields.push(getField(fields, field));
77
+ }
78
+ });
79
+ }
79
80
  return [...new Set(indexFields)];
80
81
  };
81
82
  export const getDependencyIndexFields = (field, collection, schema) => {
@@ -121,6 +122,71 @@ export const getDependencyIndexFields = (field, collection, schema) => {
121
122
  indexFields.push(...getDependencyAccessFields(field, collection, schema));
122
123
  return [...new Set(indexFields)];
123
124
  };
125
+ export const getFieldAccessGroupRoles = (groupKey, collection, roleGroup) => {
126
+ // eslint-disable-next-line security/detect-object-injection
127
+ const applicableRoles = collection.fieldAccessGroups?.[groupKey]?.applicableRoles || [];
128
+ return roleGroup.roles.filter((role) => applicableRoles.includes(role));
129
+ };
130
+ export const getFieldAccessGroupKey = (groupKey, roleGroupKey) => {
131
+ return `${groupKey}-${roleGroupKey}`;
132
+ };
133
+ export const getFieldAccessGroupIndexFields = (groupKey, collection, roleGroup) => {
134
+ // eslint-disable-next-line security/detect-object-injection
135
+ if (!collection.fieldAccessGroups?.[groupKey])
136
+ return [];
137
+ const overlayRoles = getFieldAccessGroupRoles(groupKey, collection, roleGroup);
138
+ if (overlayRoles.length === 0)
139
+ return [];
140
+ const indexFields = [];
141
+ const { fields, softDelete, preloadCache, roleSystemFields, queries } = collection;
142
+ const systemFieldsSchema = getSystemFieldsSchema();
143
+ indexFields.push({
144
+ name: "Collection_Path",
145
+ type: "Array",
146
+ required: true,
147
+ });
148
+ indexFields.push({
149
+ name: "id",
150
+ type: "String",
151
+ required: true,
152
+ });
153
+ // eslint-disable-next-line security/detect-object-injection
154
+ const groupFields = getFieldAccessGroupFields(collection)[groupKey] || [];
155
+ indexFields.push(...groupFields);
156
+ if (softDelete) {
157
+ const softDeleteField = getField(fields, softDelete.archivedField);
158
+ if (softDeleteField && !groupFields.includes(softDeleteField)) {
159
+ indexFields.push(softDeleteField);
160
+ }
161
+ }
162
+ if (preloadCache?.range) {
163
+ preloadCache.range.fields.forEach((field) => {
164
+ if (systemFields.includes(field)) {
165
+ indexFields.push(getField(systemFieldsSchema, field));
166
+ }
167
+ else if (overlayRoles.some((role) => fieldRoleProjectionAccess(getField(fields, field), role))) {
168
+ indexFields.push(getField(fields, field));
169
+ }
170
+ });
171
+ }
172
+ roleSystemFields
173
+ ?.filter((systemField) => systemField.field !== "Collection_Path" &&
174
+ overlayRoles.some((role) => !systemField.roles || systemField.roles.includes(role)))
175
+ .forEach((systemField) => {
176
+ indexFields.push(getField(systemFieldsSchema, systemField.field));
177
+ });
178
+ queries?.forEach((query) => {
179
+ const queryField = getField(fields.concat(systemFieldsSchema), query.field);
180
+ if (queryField &&
181
+ overlayRoles.some((role) => (!query.roles || query.roles.includes(role)) && fieldRoleProjectionAccess(queryField, role))) {
182
+ indexFields.push(queryField);
183
+ }
184
+ });
185
+ for (const role of overlayRoles) {
186
+ indexFields.push(...getAccessFields(collection, role));
187
+ }
188
+ return [...new Set(indexFields)];
189
+ };
124
190
  export const getRoleGroups = (collection, schema) => {
125
191
  const { preloadCache } = collection;
126
192
  const roleGroups = new Set();
@@ -194,7 +260,7 @@ export const getAllRoleGroups = (schema, permissions, collections) => {
194
260
  roleGroupsArray.map((roleGroup) => {
195
261
  roleGroup.fields.forEach((field) => {
196
262
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
197
- if (permissions && field.access && !field.access?.includes(permissions.Role)) {
263
+ if (permissions && field.access && !fieldRoleProjectionAccess(field, permissions.Role)) {
198
264
  roleGroup.fields = roleGroup.fields.filter((groupField) => groupField.name !== field.name);
199
265
  }
200
266
  });