@openinc/parse-server-opendash 4.15.0 → 4.16.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.
@@ -38,7 +38,8 @@ export declare namespace Permissions {
38
38
  metaadmin = "meta:can-see-admin",
39
39
  chatadmin = "chat:can-see-admin",
40
40
  changelog = "changelog:can-see-app",
41
- changelog_restore = "changelog:can-restore-deleted"
41
+ changelog_restore = "changelog:can-restore-deleted",
42
+ tenant_admin = "opendash:can-admin-tenants"
42
43
  }
43
44
  enum GTFS {
44
45
  global = "gtfs:can-access-global"
@@ -84,6 +84,12 @@ var Permissions;
84
84
  // Restoring a deleted object writes with the master key, so it is kept
85
85
  // separate from merely reading the changelog.
86
86
  CORE["changelog_restore"] = "changelog:can-restore-deleted";
87
+ // Cross-tenant administration: `defaultHandler` honours a submitted
88
+ // `tenant` only for a holder, and the frontend offers the tenant field to
89
+ // them alone. Registered so od-admin holds it without manual setup — and
90
+ // *only* od-admin: the `Permission` beforeSave hook keeps the tenant roles
91
+ // off it, which every other registered permission is granted to.
92
+ CORE["tenant_admin"] = "opendash:can-admin-tenants";
87
93
  })(CORE = Permissions.CORE || (Permissions.CORE = {}));
88
94
  let GTFS;
89
95
  (function (GTFS) {
@@ -37,9 +37,57 @@ function allowSubjectUserWrite(object) {
37
37
  acl.setWriteAccess(subjectId, true);
38
38
  object.setACL(acl);
39
39
  }
40
+ /**
41
+ * File an entry under the tenant of the object it is *about*, not under the
42
+ * tenant of whoever wrote it.
43
+ *
44
+ * `defaultHandler` can only answer "whose is this?" with the writer's own
45
+ * tenant — it knows nothing about `entryClassName` / `entryObjectId`. That was
46
+ * wrong for anyone who edits across tenants: an `od-admin` of tenant A adding
47
+ * meta to tenant B's equipment produced an entry owned by A, and since
48
+ * `defaultAclHandler` keys its grants on the entry's tenant, **B could not read
49
+ * the meta data on its own record** — only od-admin and tenant A could. The
50
+ * `afterSave` below then registered the context under A as well.
51
+ *
52
+ * Runs after `defaultHandler` on purpose: that function overwrites `tenant`
53
+ * with the writer's own for any caller without `opendash:can-admin-tenants`,
54
+ * so correcting it beforehand would be undone.
55
+ *
56
+ * Only on create, or on a master-key write — `immutableField` refuses a tenant
57
+ * change for everyone else, and a master-key re-save is therefore also the
58
+ * repair path for entries written before this existed.
59
+ *
60
+ * Every failure leaves `defaultHandler`'s answer in place: a linked object that
61
+ * does not exist, a class without a tenant column, an entry that names no
62
+ * object at all. The tenant is never cleared, only corrected.
63
+ */
64
+ async function applyLinkedObjectTenant(request) {
65
+ if (request.original && !request.master)
66
+ return;
67
+ const className = request.object.entryClassName;
68
+ const objectId = request.object.entryObjectId;
69
+ if (!className || !objectId)
70
+ return;
71
+ try {
72
+ const linked = await new node_1.default.Query(className)
73
+ .equalTo("objectId", objectId)
74
+ .first({ useMasterKey: true });
75
+ const tenant = linked?.get("tenant");
76
+ if (!tenant)
77
+ return;
78
+ if (request.object.tenant?.id === tenant.id)
79
+ return;
80
+ request.object.tenant = tenant;
81
+ }
82
+ catch (error) {
83
+ console.error("[@openinc/parse-server-opendash][Meta_Entry] Failed to resolve the linked object's tenant", className, objectId, error);
84
+ }
85
+ }
40
86
  async function init() {
41
87
  (0, index_js_2.beforeSaveHook)(index_js_3.Meta_Entry, async (request) => {
42
88
  await (0, index_js_2.defaultHandler)(request);
89
+ // Before the ACL handler: it derives every grant from `tenant`.
90
+ await applyLinkedObjectTenant(request);
43
91
  await (0, index_js_2.defaultAclHandler)(request, {
44
92
  // Meta entries are collaborative header data: the tenant's users are the
45
93
  // ones filling them in from the ticket/equipment UI, so they need write
@@ -13,10 +13,45 @@ const masterKeyPermission = [
13
13
  "opendash:source:write-tag-ignore-tenant-prefix",
14
14
  "opendash:source:write-tag-group",
15
15
  ];
16
+ /**
17
+ * Permissions no tenant role may hold, whichever tenant the object is filed
18
+ * under.
19
+ *
20
+ * A permission is held by whoever can *read* its object (`hasPermission`), and
21
+ * `defaultAclHandler` grants `od-tenant-admin-<tenant>` read and write on every
22
+ * object with a tenant — on purpose for ordinary permissions, which a tenant
23
+ * admin holds and hands on within their tenant. For a cross-tenant permission
24
+ * that default is an escalation: `opendash:can-admin-tenants` is registered for
25
+ * every tenant on startup, so it would make every tenant's admin a cross-tenant
26
+ * one. Granting it to anyone beyond od-admin stays possible, with the master
27
+ * key, through a user or a role of its own in the ACL.
28
+ */
29
+ const crossTenantPermission = ["opendash:can-admin-tenants"];
30
+ /**
31
+ * Take back the read/write `defaultAclHandler` gave the object's tenant roles.
32
+ * Runs on every save, master key included, so re-saving an object written
33
+ * before this existed is enough to repair it.
34
+ */
35
+ function revokeTenantRoles(object) {
36
+ const tenantId = object.get("tenant")?.id;
37
+ const acl = object.getACL();
38
+ if (!tenantId || !acl)
39
+ return;
40
+ for (const role of [
41
+ `od-tenant-admin-${tenantId}`,
42
+ `od-tenant-user-${tenantId}`,
43
+ ]) {
44
+ acl.setRoleReadAccess(role, false);
45
+ acl.setRoleWriteAccess(role, false);
46
+ }
47
+ object.setACL(acl);
48
+ }
16
49
  async function init() {
17
50
  (0, index_js_1.beforeSaveHook)(index_js_2.Permission, async (request) => {
18
51
  const { object, original, user } = request;
19
- if ((masterKeyPermission.includes(object.get("key")) || (object.get("key") && object.get("key").startsWith("master:"))) && !request.master) {
52
+ if ((masterKeyPermission.includes(object.get("key")) ||
53
+ (object.get("key") && object.get("key").startsWith("master:"))) &&
54
+ !request.master) {
20
55
  throw new node_1.default.Error(node_1.default.Error.OPERATION_FORBIDDEN, `User is not allowed to create or update permission with key '${object.get("key")}' without master key`);
21
56
  }
22
57
  await (0, index_js_1.defaultHandler)(request);
@@ -24,6 +59,9 @@ async function init() {
24
59
  allowCustomACL: true,
25
60
  denyTenantUserRead: true,
26
61
  });
62
+ if (crossTenantPermission.includes(object.get("key"))) {
63
+ revokeTenantRoles(object);
64
+ }
27
65
  });
28
66
  (0, index_js_1.afterSaveHook)(index_js_2.Permission, async ({ object, original, user }) => {
29
67
  // TODO
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.15.0",
3
+ "version": "4.16.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [