@cogover/sdk 0.9.0 → 0.10.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.
package/README.md CHANGED
@@ -76,6 +76,18 @@ invocations and to `null` for system invocations. Only the documented profile,
76
76
  membership, locale, and workspace fields are included. Raw authentication data
77
77
  and Cogover credentials are never exposed.
78
78
 
79
+ `invocation.user.membership.isSuperAdmin` tells whether the user holds the
80
+ workspace's Super Admin role, and `invocation.user.membership.roles` lists the
81
+ user's roles in the current workspace as `{ id, name }` entries. Compare roles by
82
+ `id`; role permissions are not exposed, and Cogover still enforces record and
83
+ field access for the user, so use these fields for business rules only:
84
+
85
+ ```typescript
86
+ const user = invocation.user;
87
+ const canApprove = user !== null && (user.membership.isSuperAdmin
88
+ || user.membership.roles.some((role) => role.id === APPROVER_ROLE_ID));
89
+ ```
90
+
79
91
  ## Full HTTP routes and responses
80
92
 
81
93
  ```typescript
package/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ export type { JobSchedule, JobConfig, JobManifest, JobDefinition, JobInfo, Enque
15
15
  export type { SecretsApi } from "./secrets.js";
16
16
  export { crypto } from "./crypto.js";
17
17
  export type { CryptoApi, BinaryEncoding, DecryptOutput, SecretKeyReference, AesKey, AsymmetricKey, AesMode, AesEncryptOptions, AesEncryptResult, AesDecryptOptions, RsaPadding, RsaEncryptOptions, RsaDecryptOptions, SignatureAlgorithm, SignatureOptions, } from "./crypto.js";
18
- export type { CurrentWorkspace, CurrentWorkspaceMembership, CurrentUser, UserInvocationContext, SystemInvocationContext, InboundInvocationContext, InvocationContext, } from "./invocation.js";
18
+ export type { CurrentWorkspace, CurrentWorkspaceMembership, CurrentWorkspaceRole, CurrentUser, UserInvocationContext, SystemInvocationContext, InboundInvocationContext, InvocationContext, } from "./invocation.js";
19
19
  export { createRouter } from "./router.js";
20
20
  export type { ScriptRouter } from "./router.js";
21
21
  export type { RequestMethod, ScriptRequest, ScriptQuery, ScriptHeaders, ScriptPathParams, } from "./request.js";
@@ -32,6 +32,8 @@ function userSnapshot(raw) {
32
32
  ...optionalStringFields(membershipRaw, ["personnelId", "language", "timezone"]),
33
33
  ...(membershipRaw.personnelId === undefined && typeof raw.personnelId === "string" && raw.personnelId !== ""
34
34
  ? { personnelId: raw.personnelId } : {}),
35
+ isSuperAdmin: superAdminFlag(membershipRaw.isSuperAdmin),
36
+ roles: roleSnapshots(membershipRaw.roles),
35
37
  });
36
38
  return Object.freeze({
37
39
  accountId: requiredString(raw.accountId, "account id"),
@@ -39,6 +41,28 @@ function userSnapshot(raw) {
39
41
  membership,
40
42
  });
41
43
  }
44
+ // A Runtime or development host that predates these fields omits them; the tolerant
45
+ // defaults keep project code working against it.
46
+ function superAdminFlag(value) {
47
+ if (value === undefined)
48
+ return false;
49
+ if (typeof value !== "boolean")
50
+ throw new ValidationError("Invalid invocation isSuperAdmin");
51
+ return value;
52
+ }
53
+ function roleSnapshots(value) {
54
+ if (value === undefined)
55
+ return Object.freeze([]);
56
+ if (!Array.isArray(value))
57
+ throw new ValidationError("Invalid invocation roles");
58
+ return Object.freeze(value.map((role) => {
59
+ if (!isRecord(role) || typeof role.id !== "string" || role.id === "" || typeof role.name !== "string") {
60
+ throw new ValidationError("Invalid invocation role");
61
+ }
62
+ // Only the documented keys are copied so that no other role metadata reaches project code.
63
+ return Object.freeze({ id: role.id, name: role.name });
64
+ }));
65
+ }
42
66
  function inboundSnapshot(raw) {
43
67
  if (!isRecord(raw))
44
68
  throw new ValidationError("Invalid invocation identity metadata");
@@ -9,11 +9,20 @@ export interface CurrentWorkspace {
9
9
  readonly timeFormat?: string;
10
10
  readonly numberFormat?: string;
11
11
  }
12
+ /** A role assigned to the current user in the current workspace. */
13
+ export interface CurrentWorkspaceRole {
14
+ readonly id: string;
15
+ readonly name: string;
16
+ }
12
17
  /** Public membership metadata for the current user in the current workspace. */
13
18
  export interface CurrentWorkspaceMembership {
14
19
  readonly personnelId?: string;
15
20
  readonly language?: string;
16
21
  readonly timezone?: string;
22
+ /** True when one of the user's roles is the workspace's highest administrator (Super Admin) role. */
23
+ readonly isSuperAdmin: boolean;
24
+ /** Roles assigned to the user in this workspace, sorted by name. Compare roles by `id`. */
25
+ readonly roles: readonly CurrentWorkspaceRole[];
17
26
  }
18
27
  /** Public account metadata for the authenticated user who initiated the invocation. */
19
28
  export interface CurrentUser {
@@ -107,10 +107,17 @@ interface CurrentWorkspace {
107
107
  readonly numberFormat?: string;
108
108
  }
109
109
 
110
+ interface CurrentWorkspaceRole {
111
+ readonly id: string;
112
+ readonly name: string;
113
+ }
114
+
110
115
  interface CurrentWorkspaceMembership {
111
116
  readonly personnelId?: string;
112
117
  readonly language?: string;
113
118
  readonly timezone?: string;
119
+ readonly isSuperAdmin: boolean; // Holds the workspace's Super Admin role
120
+ readonly roles: readonly CurrentWorkspaceRole[]; // Sorted by name
114
121
  }
115
122
 
116
123
  interface CurrentUser {
@@ -178,6 +185,38 @@ the authenticated request context. Raw authentication data, tokens, inbound keys
178
185
  and credentials are not included. A handler called outside Cogover has no
179
186
  authenticated invocation snapshot.
180
187
 
188
+ ### Workspace roles of the current user
189
+
190
+ `user.membership` describes the caller's membership in the current workspace:
191
+
192
+ - `isSuperAdmin` is `true` when one of the user's roles is the workspace's
193
+ highest administrator (Super Admin) role, otherwise `false`.
194
+ - `roles` lists the roles assigned to the user in the current workspace, sorted
195
+ by name. Each `CurrentWorkspaceRole` has only an `id` and a `name`; the list is
196
+ empty when the user has no role. Role permissions are not exposed.
197
+
198
+ Compare roles by `id`: a role can be renamed, and the same role name has a
199
+ different `id` in each workspace. Record and field access is still enforced by
200
+ Cogover under the user's identity, so use these fields for business rules, such
201
+ as who may approve an order, not as a replacement for security rules.
202
+
203
+ ```typescript
204
+ const APPROVER_ROLE_ID = "RO6P2N1K9X4A";
205
+
206
+ export default defineScript(({ invocation }) => {
207
+ const user = invocation.user;
208
+ const canApprove = user !== null && (user.membership.isSuperAdmin
209
+ || user.membership.roles.some((role) => role.id === APPROVER_ROLE_ID));
210
+ return {canApprove};
211
+ });
212
+ ```
213
+
214
+ HTTP invocations reflect the user's roles at the time of the request. Record
215
+ triggers and background jobs may see a role change up to about one minute late.
216
+ In local development with the Cogover Dev CLI, the values are captured when the
217
+ development session starts; restart `cogover-dev` to pick up role changes. A
218
+ `system` or `inbound` invocation has `user: null` and therefore no membership.
219
+
181
220
  ### `createRouter<TSchema = EffectiveWorkspaceObjects>(): ScriptRouter<TSchema>`
182
221
 
183
222
  Creates an independent router. `TSchema extends object` defaults to the effective
@@ -92,6 +92,38 @@ Use the Record API only when you need richer, current Personnel business fields
92
92
  such as department, position, manager, or custom fields. The snapshot intentionally
93
93
  contains only documented fields and omits credentials and raw authentication data.
94
94
 
95
+ ### Allow an action by workspace role
96
+
97
+ `invocation.user.membership` also tells whether the caller is a Super Admin of the
98
+ current workspace and which roles the caller holds there. For example, allow an
99
+ approval only for Super Admins or users holding the approver role:
100
+
101
+ ```typescript
102
+ const APPROVER_ROLE_ID = "RO6P2N1K9X4A";
103
+
104
+ export default defineScript(({ invocation }) => {
105
+ const user = invocation.user;
106
+ const canApprove = user !== null && (user.membership.isSuperAdmin
107
+ || user.membership.roles.some((role) => role.id === APPROVER_ROLE_ID));
108
+ if (!canApprove) {
109
+ return {approved: false, reason: "Only approvers can approve this order"};
110
+ }
111
+ return {approved: true};
112
+ });
113
+ ```
114
+
115
+ - The values describe the caller's roles in the current workspace. Each role has
116
+ only an `id` and a `name`; role permissions are not exposed.
117
+ - Compare roles by `id`, not by name: a role can be renamed, and role IDs differ
118
+ between workspaces.
119
+ - Cogover still enforces record and field access under the user's identity. Use
120
+ these fields for business rules, not as a replacement for security rules.
121
+ - HTTP invocations reflect the roles at the time of the request. Record triggers
122
+ and background jobs may see a role change up to about one minute late.
123
+ - In local development with the Cogover Dev CLI, the values are captured when the
124
+ development session starts. Restart `cogover-dev` to pick up role changes.
125
+ - `system` and `inbound` invocations have `user: null`, so there is no membership.
126
+
95
127
  ## Filter and sort records
96
128
 
97
129
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cogover/sdk",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "TypeScript SDK for developing Custom Backend Modules on Cogover",
5
5
  "type": "module",
6
6
  "sideEffects": false,