|
| 1 | +--- |
| 2 | +title: Context Tokens |
| 3 | +description: Context Tokens protocol schemas |
| 4 | +--- |
| 5 | + |
| 6 | +{/* ⚠️ AUTO-GENERATED — DO NOT EDIT. Run build-docs.ts to regenerate. Hand-written docs live in the module folders under content/docs/. */} |
| 7 | + |
| 8 | +Context Tokens — the declarative placeholders that resolve against the |
| 9 | + |
| 10 | +**caller's session** (who am I, which org am I in) rather than the clock. |
| 11 | + |
| 12 | +# Why this lives in `spec` |
| 13 | + |
| 14 | +These are the sibling vocabulary to `\{date-macros\}`. Filter values in |
| 15 | + |
| 16 | +dashboards, views, reports and pages travel as JSON, so a user-scoped |
| 17 | + |
| 18 | +slice cannot call `currentUser().id` inline — it writes a placeholder: |
| 19 | + |
| 20 | +\{ owner_id: '\{current_user_id\}' \} |
| 21 | + |
| 22 | +[\{ field: 'owner', operator: 'equals', value: '\{current_user_id\}' \}] |
| 23 | + |
| 24 | +Like date macros, the placeholders are expanded **client-side** by |
| 25 | + |
| 26 | +`resolveContextTokens()` in `@object-ui/core` immediately before the |
| 27 | + |
| 28 | +filter is handed to the data source. The data engine only ever sees |
| 29 | + |
| 30 | +concrete ids, never `\{tokens\}`. |
| 31 | + |
| 32 | +# Presentation scope, NOT a security boundary |
| 33 | + |
| 34 | +This is the single most important thing to understand about these |
| 35 | + |
| 36 | +tokens. `\{current_user_id\}` scopes what a surface *shows*; it does not |
| 37 | + |
| 38 | +decide what a caller is *allowed* to read. Enforcement is RLS, which |
| 39 | + |
| 40 | +uses a different and genuinely server-side vocabulary rooted at |
| 41 | + |
| 42 | +`current_user` (`owner_id = current_user.id`, compiled by |
| 43 | + |
| 44 | +`@objectstack/plugin-security`'s RLS compiler). |
| 45 | + |
| 46 | +The two look alike and are easy to confuse, so keep them straight: |
| 47 | + |
| 48 | +| | `\{current_user_id\}` | `current_user.id` | |
| 49 | + |
| 50 | +|---|---|---| |
| 51 | + |
| 52 | +| Where | filter values (JSON) | RLS `using` expressions | |
| 53 | + |
| 54 | +| Resolved | client-side, before the query | server-side, during the query | |
| 55 | + |
| 56 | +| Purpose | presentation scope | access enforcement | |
| 57 | + |
| 58 | +| Bypassable | yes — it's just a filter | no | |
| 59 | + |
| 60 | +Never reach for a context token to keep a user away from data. Removing |
| 61 | + |
| 62 | +a `\{current_user_id\}` filter widens a *view*; it must never widen |
| 63 | + |
| 64 | +*access*. |
| 65 | + |
| 66 | +# Where the tokens are honoured |
| 67 | + |
| 68 | +Filter values on every surface that resolves placeholders — object list |
| 69 | + |
| 70 | +views, dashboard widgets, reports, SDUI page components. Navigation |
| 71 | + |
| 72 | +(`recordId` / `params`) additionally resolves `AppContextSelector` ids |
| 73 | + |
| 74 | +such as `\{active_package\}`; those are nav-only and are NOT valid inside |
| 75 | + |
| 76 | +filter values, because filters are not evaluated with the sidebar's |
| 77 | + |
| 78 | +selector state. |
| 79 | + |
| 80 | +# Out of scope |
| 81 | + |
| 82 | +- `current_user.*` RLS expressions — see `@objectstack/plugin-security`. |
| 83 | + |
| 84 | +- `\{date-macros\}` — the clock-based sibling; see `./date-macros.zod.ts`. |
| 85 | + |
| 86 | +- `titleFormat` field interpolation (`\{user_id\}` etc.) — that substitutes |
| 87 | + |
| 88 | +*record fields*, an unrelated mechanism that happens to share braces. |
| 89 | + |
| 90 | +<Callout type="info"> |
| 91 | +**Source:** `packages/spec/src/data/context-tokens.zod.ts` |
| 92 | +</Callout> |
| 93 | + |
| 94 | +## TypeScript Usage |
| 95 | + |
| 96 | +```typescript |
| 97 | +import { ContextToken, ContextTokenPlaceholder } from '@objectstack/spec/data'; |
| 98 | +import type { ContextToken, ContextTokenPlaceholder } from '@objectstack/spec/data'; |
| 99 | + |
| 100 | +// Validate data |
| 101 | +const result = ContextToken.parse(data); |
| 102 | +``` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | + |
| 110 | +--- |
| 111 | + |
0 commit comments