-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecution-context.zod.ts
More file actions
79 lines (65 loc) · 3.06 KB
/
Copy pathexecution-context.zod.ts
File metadata and controls
79 lines (65 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Execution Context Schema
*
* Defines the runtime context that flows from HTTP request → data operations.
* This is the "identity + environment" envelope that every data operation can carry.
*
* Design:
* - All fields are optional for backward compatibility
* - `isSystem` bypasses permission checks (for internal/migration operations)
* - `transaction` carries the database transaction handle for atomicity
* - `traceId` enables distributed tracing across microservices
*
* Usage:
* engine.find('account', { context: { userId: '...', tenantId: '...' } })
*/
import { lazySchema } from '../shared/lazy-schema';
export const ExecutionContextSchema = lazySchema(() => z.object({
/** Current user ID (resolved from session) */
userId: z.string().optional(),
/** Current organization/tenant ID (resolved from session.activeOrganizationId) */
tenantId: z.string().optional(),
/**
* Active reference timezone (IANA name, e.g. `America/New_York`), resolved
* once per request as user-preference → org default → `UTC` (ADR-0053
* Phase 2). When unset, consumers treat it as `UTC` — today's behavior.
*/
timezone: z.string().optional(),
/** User role names (resolved from Member + Role) */
roles: z.array(z.string()).default([]),
/** Aggregated permission names (resolved from PermissionSet) */
permissions: z.array(z.string()).default([]),
/**
* Aggregated system permissions (union of `PermissionSet.systemPermissions`
* across the user's resolved permission sets). Used to gate app
* entry (`AppSchema.requiredPermissions`) and system-level capabilities
* like `manage_users`, `studio.access`, `setup.access`.
*/
systemPermissions: z.array(z.string()).optional(),
/**
* Aggregated tab/app visibility overrides (merged most-permissive across
* the user's resolved permission sets: visible > default_on > default_off > hidden).
* Keyed by app name. A `hidden` value forces an app off the user's
* authorized list even if `requiredPermissions` would otherwise pass.
*/
tabPermissions: z.record(z.string(), z.enum(['visible', 'hidden', 'default_on', 'default_off'])).optional(),
/**
* IDs of all users in the active organization. Pre-resolved so RLS
* expressions can scope visibility of identity tables (`sys_user`)
* via `IN (current_user.org_user_ids)` without needing subquery
* support in the RLS compiler. Populated by the runtime's
* resolveExecutionContext from `sys_member`.
*/
org_user_ids: z.array(z.string()).optional(),
/** Whether this is a system-level operation (bypasses permission checks) */
isSystem: z.boolean().default(false),
/** Raw access token (for external API call pass-through) */
accessToken: z.string().optional(),
/** Database transaction handle */
transaction: z.unknown().optional(),
/** Request trace ID (for distributed tracing) */
traceId: z.string().optional(),
}));
export type ExecutionContext = z.infer<typeof ExecutionContextSchema>;