@openinc/parse-server-opendash 4.16.0 → 4.17.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.
@@ -39,7 +39,8 @@ export declare namespace Permissions {
39
39
  chatadmin = "chat:can-see-admin",
40
40
  changelog = "changelog:can-see-app",
41
41
  changelog_restore = "changelog:can-restore-deleted",
42
- tenant_admin = "opendash:can-admin-tenants"
42
+ tenant_admin = "opendash:can-admin-tenants",
43
+ multi_tenant = "opendash:can-work-multitenant"
43
44
  }
44
45
  enum GTFS {
45
46
  global = "gtfs:can-access-global"
@@ -84,12 +84,19 @@ 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.
87
+ // Cross-tenant administration: the tenant admin pages, and `defaultHandler`
88
+ // honours a submitted `tenant` for a holder. Registered so od-admin holds
89
+ // it without manual setup — and *only* od-admin: the `Permission`
90
+ // beforeSave hook keeps the tenant roles off it, which every other
91
+ // registered permission is granted to.
92
92
  CORE["tenant_admin"] = "opendash:can-admin-tenants";
93
+ // Working across tenants without administering them — someone who handles
94
+ // tickets for several tenants. `defaultHandler` honours a submitted
95
+ // `tenant` for a holder, and the plugins offer the tenant field, the active
96
+ // tenant notices and tenant-aware views to them. Kept apart from
97
+ // `tenant_admin` so it does not come with the tenant administration.
98
+ // Same handling: od-admin only by default, granted with the master key.
99
+ CORE["multi_tenant"] = "opendash:can-work-multitenant";
93
100
  })(CORE = Permissions.CORE || (Permissions.CORE = {}));
94
101
  let GTFS;
95
102
  (function (GTFS) {
@@ -2,7 +2,7 @@ import { SessionTokenType } from "../index.js";
2
2
  /**
3
3
  * Handles the default beforeSave() logic for a Parse.Object.
4
4
  *
5
- * This function will check if a user or tenant field is present in the schema and set the value to the current user and/or the tenant of the current user if not provided.
5
+ * This function will check if a user or tenant field is present in the schema and set the value to the current user and/or the tenant of the current user. A tenant submitted on create is kept for a holder of `opendash:can-work-multitenant` or `opendash:can-admin-tenants`.
6
6
  * This will also mark the user and tenant fields as immutable, preventing them from being edited in the future.
7
7
  *
8
8
  * @param request - The Parse.Cloud.BeforeSaveRequest object.
@@ -13,7 +13,7 @@ const migrationMode_js_1 = require("./migrationMode.js");
13
13
  /**
14
14
  * Handles the default beforeSave() logic for a Parse.Object.
15
15
  *
16
- * This function will check if a user or tenant field is present in the schema and set the value to the current user and/or the tenant of the current user if not provided.
16
+ * This function will check if a user or tenant field is present in the schema and set the value to the current user and/or the tenant of the current user. A tenant submitted on create is kept for a holder of `opendash:can-work-multitenant` or `opendash:can-admin-tenants`.
17
17
  * This will also mark the user and tenant fields as immutable, preventing them from being edited in the future.
18
18
  *
19
19
  * @param request - The Parse.Cloud.BeforeSaveRequest object.
@@ -57,18 +57,35 @@ async function defaultHandler(request) {
57
57
  const user = (await request.user?.fetch({
58
58
  useMasterKey: true,
59
59
  }));
60
- try {
61
- await (0, index_js_1.requirePermission)(request, "opendash:can-admin-tenants", "User is not allowed to use tag which ignores tenant prefix");
62
- if (!request.object.get("tenant")) {
63
- request.object.set("tenant", user.get("tenant"));
64
- }
65
- }
66
- catch (error) {
60
+ if (!request.object.get("tenant") ||
61
+ !(await mayChooseTenant(request.sessionToken))) {
67
62
  request.object.set("tenant", user.get("tenant"));
68
63
  }
69
64
  }
70
65
  }
71
66
  }
67
+ /**
68
+ * The keys that let a writer choose the tenant of a new object instead of
69
+ * getting their own: `opendash:can-work-multitenant` for someone who works in
70
+ * several tenants (the frontend submits the *active* one — the tenant of the
71
+ * selected header source, whose statuses, projects and equipment its pickers
72
+ * offered), and `opendash:can-admin-tenants` for the platform administration.
73
+ * Separate keys, so working across tenants does not come with administering
74
+ * them.
75
+ */
76
+ const TENANT_CHOICE_PERMISSIONS = [
77
+ "opendash:can-work-multitenant",
78
+ "opendash:can-admin-tenants",
79
+ ];
80
+ async function mayChooseTenant(sessionToken) {
81
+ if (!sessionToken)
82
+ return false;
83
+ for (const key of TENANT_CHOICE_PERMISSIONS) {
84
+ if (await (0, index_js_1.hasPermission)(sessionToken, key))
85
+ return true;
86
+ }
87
+ return false;
88
+ }
72
89
  /**
73
90
  * Handles the default Parse.ACL beforeSave() logic for a Parse.Object.
74
91
  *
@@ -50,8 +50,8 @@ function allowSubjectUserWrite(object) {
50
50
  * `afterSave` below then registered the context under A as well.
51
51
  *
52
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.
53
+ * with the writer's own for any caller without `opendash:can-work-multitenant`
54
+ * or `opendash:can-admin-tenants`, so correcting it beforehand would be undone.
55
55
  *
56
56
  * Only on create, or on a master-key write — `immutableField` refuses a tenant
57
57
  * change for everyone else, and a master-key re-save is therefore also the
@@ -9,6 +9,7 @@ const index_js_1 = require("../features/schema/index.js");
9
9
  const index_js_2 = require("../types/index.js");
10
10
  const masterKeyPermission = [
11
11
  "opendash:can-admin-tenants",
12
+ "opendash:can-work-multitenant",
12
13
  "opendash:tenant:change-tag-prefix",
13
14
  "opendash:source:write-tag-ignore-tenant-prefix",
14
15
  "opendash:source:write-tag-group",
@@ -21,12 +22,15 @@ const masterKeyPermission = [
21
22
  * `defaultAclHandler` grants `od-tenant-admin-<tenant>` read and write on every
22
23
  * object with a tenant — on purpose for ordinary permissions, which a tenant
23
24
  * 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.
25
+ * that default is an escalation: these are registered for every tenant on
26
+ * startup, so they would make every tenant's admin a cross-tenant one.
27
+ * Granting them to anyone beyond od-admin stays possible, with the master key,
28
+ * through a user or a role of its own in the ACL.
28
29
  */
29
- const crossTenantPermission = ["opendash:can-admin-tenants"];
30
+ const crossTenantPermission = [
31
+ "opendash:can-admin-tenants",
32
+ "opendash:can-work-multitenant",
33
+ ];
30
34
  /**
31
35
  * Take back the read/write `defaultAclHandler` gave the object's tenant roles.
32
36
  * Runs on every save, master key included, so re-saving an object written
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.16.0",
3
+ "version": "4.17.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [