@openinc/parse-server-opendash 4.21.0 → 4.22.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,33 @@
1
+ /**
2
+ * The users in the given roles, nested roles included — for the ticket form's
3
+ * "assignable: members of these roles" (`FieldConfigEntry.allowedRoles` in
4
+ * `@opendash/plugin-openservice-ticket`).
5
+ *
6
+ * Params: `{ roleIds: string[] }`. Returns `{ userIds: string[] }`.
7
+ *
8
+ * **Why a function and not a relation query from the browser.** The client
9
+ * reads `_Role.users` with the caller's own session, and Parse answers a
10
+ * `$relatedTo` on a role the caller may not read with an *empty* list, not an
11
+ * error. Exactly the roles worth assigning by are of that kind: a service
12
+ * provider role is readable by administrators only, a tenant admin role by
13
+ * nobody without the master key. A technician opening the form got an empty
14
+ * picker. Here the membership is read with the master key.
15
+ *
16
+ * **Who may ask about which role.** Membership is not public, so a role is
17
+ * resolved only if the caller
18
+ * - may read the role itself (an administrator configuring the form sees the
19
+ * roles they can pick), or
20
+ * - may read a form config that offers the role for assignment — which is the
21
+ * form they are filling in.
22
+ * Other ids are ignored, not refused: the picker then shows fewer people, and
23
+ * the function does not become a way to probe which roles exist.
24
+ *
25
+ * Parse semantics for nesting: a role's `roles` relation lists roles whose
26
+ * users inherit it, so a role's members are its own users plus the members of
27
+ * those roles (the same direction `openinc-config` walks for the caller's own
28
+ * roles).
29
+ *
30
+ * Only ids come back. The browser offers just the users it can read anyway,
31
+ * so this widens nothing beyond "who is in the team".
32
+ */
33
+ export declare function init(name: string): Promise<void>;
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.init = init;
7
+ const node_1 = __importDefault(require("parse/node"));
8
+ const index_js_1 = require("../types/index.js");
9
+ /** More would not be a picker any more; it caps what one call may resolve. */
10
+ const MAX_ROLES = 50;
11
+ /** Nesting deeper than this is a cycle or a mistake, not an org chart. */
12
+ const MAX_DEPTH = 10;
13
+ /**
14
+ * The users in the given roles, nested roles included — for the ticket form's
15
+ * "assignable: members of these roles" (`FieldConfigEntry.allowedRoles` in
16
+ * `@opendash/plugin-openservice-ticket`).
17
+ *
18
+ * Params: `{ roleIds: string[] }`. Returns `{ userIds: string[] }`.
19
+ *
20
+ * **Why a function and not a relation query from the browser.** The client
21
+ * reads `_Role.users` with the caller's own session, and Parse answers a
22
+ * `$relatedTo` on a role the caller may not read with an *empty* list, not an
23
+ * error. Exactly the roles worth assigning by are of that kind: a service
24
+ * provider role is readable by administrators only, a tenant admin role by
25
+ * nobody without the master key. A technician opening the form got an empty
26
+ * picker. Here the membership is read with the master key.
27
+ *
28
+ * **Who may ask about which role.** Membership is not public, so a role is
29
+ * resolved only if the caller
30
+ * - may read the role itself (an administrator configuring the form sees the
31
+ * roles they can pick), or
32
+ * - may read a form config that offers the role for assignment — which is the
33
+ * form they are filling in.
34
+ * Other ids are ignored, not refused: the picker then shows fewer people, and
35
+ * the function does not become a way to probe which roles exist.
36
+ *
37
+ * Parse semantics for nesting: a role's `roles` relation lists roles whose
38
+ * users inherit it, so a role's members are its own users plus the members of
39
+ * those roles (the same direction `openinc-config` walks for the caller's own
40
+ * roles).
41
+ *
42
+ * Only ids come back. The browser offers just the users it can read anyway,
43
+ * so this widens nothing beyond "who is in the team".
44
+ */
45
+ async function init(name) {
46
+ node_1.default.Cloud.define(name, async (request) => {
47
+ const user = request.user;
48
+ if (!user) {
49
+ throw new node_1.default.Error(node_1.default.Error.INVALID_SESSION_TOKEN, `${name} requires a logged-in user`);
50
+ }
51
+ const requested = request.params?.roleIds;
52
+ if (!Array.isArray(requested) ||
53
+ requested.some((id) => typeof id !== "string")) {
54
+ throw new node_1.default.Error(node_1.default.Error.INVALID_QUERY, `${name}: roleIds must be an array of strings`);
55
+ }
56
+ const roleIds = [...new Set(requested)].slice(0, MAX_ROLES);
57
+ if (roleIds.length === 0)
58
+ return { userIds: [] };
59
+ const sessionToken = user.getSessionToken();
60
+ if (!sessionToken) {
61
+ throw new node_1.default.Error(node_1.default.Error.INVALID_SESSION_TOKEN, `${name} requires a session`);
62
+ }
63
+ const allowed = await authorizedRoleIds(roleIds, sessionToken);
64
+ return { userIds: await membersOf([...allowed]) };
65
+ }, { requireUser: true });
66
+ }
67
+ /** The requested roles the caller may ask about (see `init`). */
68
+ async function authorizedRoleIds(roleIds, sessionToken) {
69
+ const readable = await new node_1.default.Query(node_1.default.Role)
70
+ .containedIn("objectId", roleIds)
71
+ .limit(roleIds.length)
72
+ .find({ sessionToken });
73
+ const allowed = new Set(idsOf(readable));
74
+ const rest = roleIds.filter((id) => !allowed.has(id));
75
+ if (rest.length === 0)
76
+ return allowed;
77
+ const configs = await new node_1.default.Query(index_js_1.Service_FormConfig)
78
+ .select("fields")
79
+ .limit(1000)
80
+ .find({ sessionToken });
81
+ const offered = new Set(configs.flatMap(offeredRoleIds));
82
+ for (const id of rest) {
83
+ if (offered.has(id))
84
+ allowed.add(id);
85
+ }
86
+ return allowed;
87
+ }
88
+ function idsOf(objects) {
89
+ return objects.map((object) => object.id).filter((id) => !!id);
90
+ }
91
+ /** Role ids a form config offers for assignment (`allowedRoles` of its fields). */
92
+ function offeredRoleIds(config) {
93
+ try {
94
+ const entries = JSON.parse(config.fields ?? "[]");
95
+ return entries.flatMap((entry) => Array.isArray(entry?.allowedRoles)
96
+ ? entry.allowedRoles.filter((id) => typeof id === "string")
97
+ : []);
98
+ }
99
+ catch {
100
+ return [];
101
+ }
102
+ }
103
+ /** Ids of every user in the roles, nested roles included (master key). */
104
+ async function membersOf(roleIds) {
105
+ const members = new Set();
106
+ const visited = new Set();
107
+ let pending = roleIds;
108
+ for (let depth = 0; pending.length > 0 && depth < MAX_DEPTH; depth++) {
109
+ const batch = pending.filter((id) => !visited.has(id));
110
+ batch.forEach((id) => visited.add(id));
111
+ pending = [];
112
+ for (const id of batch) {
113
+ const role = node_1.default.Role.createWithoutData(id);
114
+ const [users, children] = await Promise.all([
115
+ role.getUsers().query().limit(10000).find({ useMasterKey: true }),
116
+ role.getRoles().query().limit(1000).find({ useMasterKey: true }),
117
+ ]);
118
+ idsOf(users).forEach((id) => members.add(id));
119
+ pending.push(...idsOf(children));
120
+ }
121
+ }
122
+ return [...members].sort();
123
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.21.0",
3
+ "version": "4.22.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [