@openinc/parse-server-opendash 4.11.0 → 4.12.0

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.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Extracts the object ids from an array-of-ref field, tolerating undefined and
3
+ * malformed entries.
4
+ *
5
+ * The entity adapter stores array-of-ref fields as plain id **strings**, not as
6
+ * `Parse.Object`s. Reading `.id` off them returned undefined, so an ACL built
7
+ * from such a field granted nobody anything — silently. Every consumer of
8
+ * `assignedUsers`, `assignedRoles`, `users` or `roles` has to go through here.
9
+ */
10
+ export declare function pointerIds(value: unknown): string[];
11
+ /**
12
+ * Role **names** for an array-of-ref field holding role pointers.
13
+ *
14
+ * `Parse.ACL` grants roles by name, never by id, so a role reference has to be
15
+ * resolved before it can be written into an ACL.
16
+ */
17
+ export declare function roleNames(Parse: typeof import("parse/node").default, value: unknown): Promise<string[]>;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pointerIds = pointerIds;
4
+ exports.roleNames = roleNames;
5
+ /**
6
+ * Extracts the object ids from an array-of-ref field, tolerating undefined and
7
+ * malformed entries.
8
+ *
9
+ * The entity adapter stores array-of-ref fields as plain id **strings**, not as
10
+ * `Parse.Object`s. Reading `.id` off them returned undefined, so an ACL built
11
+ * from such a field granted nobody anything — silently. Every consumer of
12
+ * `assignedUsers`, `assignedRoles`, `users` or `roles` has to go through here.
13
+ */
14
+ function pointerIds(value) {
15
+ if (!Array.isArray(value))
16
+ return [];
17
+ return value
18
+ .map((entry) => {
19
+ if (typeof entry === "string")
20
+ return entry;
21
+ if (entry && typeof entry === "object") {
22
+ const o = entry;
23
+ return o.id ?? o.objectId;
24
+ }
25
+ return undefined;
26
+ })
27
+ .filter((id) => typeof id === "string");
28
+ }
29
+ /**
30
+ * Role **names** for an array-of-ref field holding role pointers.
31
+ *
32
+ * `Parse.ACL` grants roles by name, never by id, so a role reference has to be
33
+ * resolved before it can be written into an ACL.
34
+ */
35
+ async function roleNames(Parse, value) {
36
+ const ids = pointerIds(value);
37
+ if (ids.length === 0)
38
+ return [];
39
+ const roles = await new Parse.Query(Parse.Role)
40
+ .containedIn("objectId", ids)
41
+ .find({ useMasterKey: true });
42
+ return roles
43
+ .map((role) => role.getName())
44
+ .filter((name) => !!name);
45
+ }
@@ -1,17 +1,51 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.init = init;
7
+ const node_1 = __importDefault(require("parse/node"));
4
8
  const index_js_1 = require("../features/schema/index.js");
9
+ const pointerIds_js_1 = require("../helper/pointerIds.js");
5
10
  const index_js_2 = require("../types/index.js");
11
+ /**
12
+ * Restricts a form config to its audience.
13
+ *
14
+ * A config names the roles and users it applies to, and until now every member
15
+ * of the tenant could read all of them — so a config written for one department
16
+ * was visible to everyone, and the frontend was the only thing deciding who it
17
+ * actually applied to.
18
+ *
19
+ * A config **without** any roles or users is the tenant's global default: it
20
+ * has to stay readable tenant-wide, because it is what everyone falls back to
21
+ * when nothing more specific matches (see `useTicketFormConfig`). Narrowing
22
+ * that one would leave users with no config at all.
23
+ *
24
+ * Write access is unchanged — `defaultAclHandler` keeps it with `od-admin` and
25
+ * the tenant admins. Being the audience of a config does not mean being allowed
26
+ * to change it.
27
+ */
28
+ async function applyAudienceAcl(object) {
29
+ const userIds = (0, pointerIds_js_1.pointerIds)(object.get("users"));
30
+ const roles = await (0, pointerIds_js_1.roleNames)(node_1.default, object.get("roles"));
31
+ if (userIds.length === 0 && roles.length === 0)
32
+ return; // global default
33
+ const acl = object.getACL() ?? new node_1.default.ACL();
34
+ for (const userId of userIds)
35
+ acl.setReadAccess(userId, true);
36
+ for (const role of roles)
37
+ acl.setRoleReadAccess(role, true);
38
+ object.setACL(acl);
39
+ }
6
40
  async function init() {
7
41
  (0, index_js_1.beforeSaveHook)(index_js_2.Service_FormConfig, async (request) => {
8
- const { object, original, user } = request;
42
+ const { object } = request;
43
+ const hasAudience = (0, pointerIds_js_1.pointerIds)(object.get("users")).length > 0 ||
44
+ (0, pointerIds_js_1.pointerIds)(object.get("roles")).length > 0;
9
45
  await (0, index_js_1.defaultHandler)(request);
10
- await (0, index_js_1.defaultAclHandler)(request);
11
- // TODO
12
- });
13
- (0, index_js_1.afterSaveHook)(index_js_2.Service_FormConfig, async (request) => {
14
- const { object, original, user } = request;
15
- // TODO
46
+ // A config addressed at specific roles or users is not the tenant's
47
+ // business — only its audience (granted below) and the admins may read it.
48
+ await (0, index_js_1.defaultAclHandler)(request, { denyTenantUserRead: hasAudience });
49
+ await applyAudienceAcl(object);
16
50
  });
17
51
  }
@@ -41,6 +41,7 @@ const node_1 = __importStar(require("parse/node"));
41
41
  const i18next_1 = __importDefault(require("i18next"));
42
42
  const index_js_1 = require("../features/schema/index.js");
43
43
  const appLinks_js_1 = require("../helper/appLinks.js");
44
+ const pointerIds_js_1 = require("../helper/pointerIds.js");
44
45
  const getUserLanguage_js_1 = require("../helper/getUserLanguage.js");
45
46
  const index_js_2 = require("../types/index.js");
46
47
  const ruleset_core_1 = require("@opendash/ruleset-core");
@@ -65,6 +66,7 @@ async function init() {
65
66
  // showing every ticket.
66
67
  await (0, index_js_1.defaultAclHandler)(request, { allowCustomACL: true });
67
68
  await setTicketACL(object);
69
+ await assertSameTenantReferences(object);
68
70
  // Assign a sequential ticket number on creation. Done here (backend) rather
69
71
  // than in the frontend because every write path — the create form, the
70
72
  // cross-plugin service, and the generic entity admin — funnels through this
@@ -200,6 +202,77 @@ async function nextTenantTicketNumber(tenant) {
200
202
  await counter.save(null, { useMasterKey: true });
201
203
  return 1;
202
204
  }
205
+ /** Pointer fields on a ticket that must belong to the ticket's own tenant. */
206
+ const TENANT_SCOPED_POINTERS = ["status", "project", "equipment"];
207
+ /** Pointer-array fields whose entries must belong to the ticket's own tenant. */
208
+ const TENANT_SCOPED_POINTER_ARRAYS = ["tags", "assignedUsers"];
209
+ /**
210
+ * Rejects a ticket that references master data from another tenant.
211
+ *
212
+ * ACLs do not stop an `od-admin` at a tenant boundary, so every picker in the
213
+ * UI used to offer every tenant's statuses, projects, equipment and tags. The
214
+ * frontend now scopes those queries, but the UI is not the only write path —
215
+ * the generic entity admin and the REST API go straight through here — and a
216
+ * ticket pointing at a foreign status is silently broken: it is counted in
217
+ * neither tenant's board, and it blocks deletion of that status
218
+ * (`Service_Status`'s in-use guard does not filter by tenant either).
219
+ *
220
+ * `assignedRoles` is deliberately not checked: `_Role` has no tenant column at
221
+ * all — a role's tenant lives in its name — so there is nothing to compare.
222
+ *
223
+ * Only dirty fields are checked, so a ticket that already carries a bad
224
+ * reference can still be saved to repair it, and the common save does at most
225
+ * one extra query.
226
+ */
227
+ async function assertSameTenantReferences(object) {
228
+ const tenantId = object.get("tenant")?.id;
229
+ if (!tenantId)
230
+ return; // nothing to compare against
231
+ const suspects = [];
232
+ for (const field of TENANT_SCOPED_POINTERS) {
233
+ if (!object.dirty(field))
234
+ continue;
235
+ const value = object.get(field);
236
+ if (value)
237
+ suspects.push(value);
238
+ }
239
+ for (const field of TENANT_SCOPED_POINTER_ARRAYS) {
240
+ if (!object.dirty(field))
241
+ continue;
242
+ const value = object.get(field);
243
+ if (Array.isArray(value))
244
+ suspects.push(...value.filter(Boolean));
245
+ }
246
+ if (suspects.length === 0)
247
+ return;
248
+ // Pointers arrive unfetched, so the tenant has to be read from the database —
249
+ // one query per referenced class rather than `fetchAll` over a mixed list,
250
+ // which is both explicit and the only form that reads `tenant` back reliably.
251
+ const byClass = new Map();
252
+ for (const suspect of suspects) {
253
+ // A pointer that has not been saved yet has no id — nothing to look up.
254
+ if (!suspect.id)
255
+ continue;
256
+ const ids = byClass.get(suspect.className) ?? new Set();
257
+ ids.add(suspect.id);
258
+ byClass.set(suspect.className, ids);
259
+ }
260
+ for (const [className, ids] of byClass) {
261
+ const rows = await new node_1.default.Query(className)
262
+ .containedIn("objectId", [...ids])
263
+ .select("tenant")
264
+ .limit(ids.size)
265
+ .find({ useMasterKey: true });
266
+ for (const row of rows) {
267
+ const refTenantId = row.get("tenant")?.id;
268
+ // No tenant on the referenced row = shared/global master data, allowed.
269
+ if (!refTenantId || refTenantId === tenantId)
270
+ continue;
271
+ throw new node_1.default.Error(node_1.default.Error.VALIDATION_ERROR, `Cannot reference ${className} ${row.id} from tenant ${refTenantId} ` +
272
+ `on a ticket of tenant ${tenantId}.`);
273
+ }
274
+ }
275
+ }
203
276
  /**
204
277
  * Sets the ticket ACL from `createdBy`, `assignedUsers` and `assignedRoles` —
205
278
  * each gets read/write. `assignedUsers`/`assignedRoles` are arrays of Parse
@@ -232,12 +305,12 @@ async function setTicketACL(object) {
232
305
  acl.setWriteAccess(creatorId, true);
233
306
  }
234
307
  // assigned users read/write
235
- for (const userId of pointerIds(object.get("assignedUsers"))) {
308
+ for (const userId of (0, pointerIds_js_1.pointerIds)(object.get("assignedUsers"))) {
236
309
  acl.setReadAccess(userId, true);
237
310
  acl.setWriteAccess(userId, true);
238
311
  }
239
312
  // assigned roles read/write (resolve pointer ids → role names)
240
- const roleIds = pointerIds(object.get("assignedRoles"));
313
+ const roleIds = (0, pointerIds_js_1.pointerIds)(object.get("assignedRoles"));
241
314
  if (roleIds.length > 0) {
242
315
  const roles = await new node_1.default.Query(node_1.default.Role)
243
316
  .containedIn("objectId", roleIds)
@@ -404,29 +477,8 @@ async function sendAssignmentNotification(user, ticket, options) {
404
477
  }
405
478
  /** Ids present in `current` (array of pointers) but not in `previous`. */
406
479
  function addedIds(current, previous) {
407
- const previousIds = new Set(pointerIds(previous));
408
- return pointerIds(current).filter((id) => !previousIds.has(id));
409
- }
410
- /** Extracts the object ids from an array of Parse pointers, tolerating
411
- * undefined / malformed entries. */
412
- function pointerIds(value) {
413
- if (!Array.isArray(value))
414
- return [];
415
- return value
416
- .map((entry) => {
417
- // The entity adapter stores array-of-ref fields as plain id
418
- // STRINGS, not Parse.Objects. Reading `.id` off them returned
419
- // undefined, so setTicketACL granted assignees nothing and
420
- // notifyNewAssignees found nobody — silently, for every ticket.
421
- if (typeof entry === "string")
422
- return entry;
423
- if (entry && typeof entry === "object") {
424
- const o = entry;
425
- return o.id ?? o.objectId;
426
- }
427
- return undefined;
428
- })
429
- .filter((id) => typeof id === "string");
480
+ const previousIds = new Set((0, pointerIds_js_1.pointerIds)(previous));
481
+ return (0, pointerIds_js_1.pointerIds)(current).filter((id) => !previousIds.has(id));
430
482
  }
431
483
  function getUsername(user) {
432
484
  if (!user)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.11.0",
3
+ "version": "4.12.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [
@@ -67,10 +67,6 @@
67
67
  "type": "Pointer",
68
68
  "targetClass": "{{PREFIX}}BDE_WorkOrder",
69
69
  "required": false
70
- },
71
- "reportedAt": {
72
- "type": "Date",
73
- "required": false
74
70
  }
75
71
  },
76
72
  "classLevelPermissions": {