@svgrid/enterprise 2.3.0 → 2.3.1

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.
Files changed (49) hide show
  1. package/dist/cdn/svgrid-enterprise.svelte-external.js +6885 -3504
  2. package/dist/designer/assets/GridMenus-BtWVk9Ab.js +7 -0
  3. package/dist/designer/assets/SvGridChartPanel-DWlBO2SD.js +10 -0
  4. package/dist/designer/assets/SvGridChartView-8c8Mb4j8.js +1 -0
  5. package/dist/designer/assets/SvGridChartView-DX9HfkBR.css +1 -0
  6. package/dist/designer/assets/index-DsDgp9Xq.js +78758 -0
  7. package/dist/designer/assets/index-tTY_Dx4P.css +1 -0
  8. package/dist/designer/assets/jszip.min-fkJdmAmj.js +2 -0
  9. package/dist/designer/assets/pdfmake-DeCsnyl9.js +242 -0
  10. package/dist/designer/assets/smart.export-BZlSCE8T.js +35 -0
  11. package/dist/designer/assets/vfs_fonts-eX2NpmfX.js +1 -0
  12. package/dist/designer/index.html +13 -0
  13. package/dist/node/studio.js +5660 -1747
  14. package/package.json +8 -5
  15. package/src/SvGridBoard.svelte +4 -1
  16. package/src/SvGridScheduler.svelte +114 -90
  17. package/src/ai-export-pdf.dom.test.ts +77 -77
  18. package/src/ai-export-xlsx.dom.test.ts +90 -90
  19. package/src/ai-export.dom.test.ts +114 -114
  20. package/src/export-ooxml.ts +4 -0
  21. package/src/export-xls.ts +3 -0
  22. package/src/expressions/evaluate.ts +9 -0
  23. package/src/import.test.ts +1 -1
  24. package/src/import.ts +1 -1
  25. package/src/index.ts +12 -0
  26. package/src/pivot.test.ts +0 -1
  27. package/src/schema-designer.ts +1 -1
  28. package/src/studio/emit-project.test.ts +298 -7
  29. package/src/studio/emit-project.ts +483 -29
  30. package/src/studio/emit-schema.ts +20 -10
  31. package/src/studio/index.ts +34 -0
  32. package/src/studio/init-flow.test.ts +239 -0
  33. package/src/studio/init-flow.ts +358 -0
  34. package/src/studio/project.test.ts +0 -1
  35. package/src/studio/project.ts +109 -2
  36. package/src/studio/samples/datasets.test.ts +84 -0
  37. package/src/studio/samples/datasets.ts +340 -0
  38. package/src/studio/samples/live-data.test.ts +1 -1
  39. package/src/studio/samples/samples.test.ts +268 -268
  40. package/src/studio/samples/shared.ts +7 -150
  41. package/src/studio/screen-suites.test.ts +226 -0
  42. package/src/studio/screen-suites.ts +445 -0
  43. package/src/studio/ui-components-surface.test.ts +2 -2
  44. package/src/studio/ui-components.generated.ts +1643 -106
  45. package/src/studio/ui-components.ts +742 -641
  46. package/src/sveltekit/index.ts +1 -0
  47. package/src/sveltekit/sql-source.ts +1 -1
  48. package/src/sveltekit/transport-scope.test.ts +125 -0
  49. package/src/sveltekit/transport.ts +76 -2
@@ -11,6 +11,7 @@ export {
11
11
  createKitHandlers,
12
12
  type KitDataSourceOptions,
13
13
  type KitHandlerOptions,
14
+ type KitScope,
14
15
  type KitHandlers,
15
16
  type KitMessage,
16
17
  } from './transport'
@@ -74,7 +74,7 @@ export function createSqlDataSource<TData extends RowData>(
74
74
  config: SqlDataSourceConfig<TData>,
75
75
  ): WritableDataSource<TData> & AggregateSource {
76
76
  nudgeEnterprise('Studio') // soft-gate; never blocks, safe on the server
77
- const { schema, table, execute } = config
77
+ const { schema, execute } = config
78
78
  const dialect = config.dialect ?? {}
79
79
  const returning = config.returning ?? true
80
80
  const id = quoter(dialect)
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Row scoping (`scope`) - the mechanism behind multi-tenancy.
3
+ *
4
+ * Scoping reads is the easy half and the useless half on its own: if create can
5
+ * plant a row in another tenant, or update/delete can reach one by guessing an
6
+ * id, the isolation is decorative. These tests cover all four paths plus the
7
+ * ways a client might try to talk its way out of the scope.
8
+ */
9
+ import { describe, expect, it } from 'vitest'
10
+ import type { ServerRequest } from '@svgrid/grid'
11
+ import type { EntitySchema } from '../schema'
12
+ import { createInMemoryDataSource } from './in-memory'
13
+ import { createKitDataSource, createKitHandlers } from './transport'
14
+
15
+ type Row = { id: string; name: string; tenantId: string }
16
+
17
+ const schema: EntitySchema<Row> = {
18
+ name: 'notes',
19
+ fields: [
20
+ { field: 'id', type: 'text', primaryKey: true },
21
+ { field: 'name', type: 'text' },
22
+ { field: 'tenantId', type: 'text' },
23
+ ],
24
+ }
25
+
26
+ const seed: Row[] = [
27
+ { id: '1', name: 'Acme note', tenantId: 'acme' },
28
+ { id: '2', name: 'Acme second', tenantId: 'acme' },
29
+ { id: '3', name: 'Globex secret', tenantId: 'globex' },
30
+ ]
31
+
32
+ /** Wire a client to handlers scoped to `tenant` (null = unscoped/super-admin). */
33
+ function wire(tenant: string | null, opts: { throwOnResolve?: boolean } = {}) {
34
+ const backend = createInMemoryDataSource(seed.map((r) => ({ ...r })), schema)
35
+ const handlers = createKitHandlers({
36
+ schema,
37
+ source: backend,
38
+ scope: () => {
39
+ if (opts.throwOnResolve) throw new Error('no tenant on session')
40
+ return tenant ? { field: 'tenantId', value: tenant } : null
41
+ },
42
+ })
43
+ const fakeFetch = (url: string, init?: RequestInit) =>
44
+ handlers.handle(new Request(`http://localhost${url}`, init))
45
+ const client = createKitDataSource<Row>({ endpoint: '/api/notes', fetch: fakeFetch })
46
+ return { client, backend, handlers }
47
+ }
48
+
49
+ const req = (partial: Partial<ServerRequest> = {}): ServerRequest => ({
50
+ startRow: 0, endRow: 50, pageIndex: 0, pageSize: 50, sortModel: [], filterModel: {}, ...partial,
51
+ })
52
+
53
+ describe('row scoping: reads', () => {
54
+ it('returns only the caller tenant\'s rows', async () => {
55
+ const { client } = wire('acme')
56
+ const { rows } = await client.getRows(req())
57
+ expect(rows.map((r) => r.id)).toEqual(['1', '2'])
58
+ })
59
+
60
+ it('is unscoped when the resolver returns null', async () => {
61
+ const { client } = wire(null)
62
+ const { rows } = await client.getRows(req())
63
+ expect(rows).toHaveLength(3)
64
+ })
65
+
66
+ it('a client-supplied tenant filter cannot widen the scope', async () => {
67
+ const { client } = wire('acme')
68
+ // Ask for globex explicitly - the server's predicate is written last.
69
+ const { rows } = await client.getRows(
70
+ req({ filterModel: { columns: { tenantId: { operator: 'equals', value: 'globex' } } } }),
71
+ )
72
+ expect(rows.every((r) => r.tenantId === 'acme')).toBe(true)
73
+ })
74
+ })
75
+
76
+ describe('row scoping: writes', () => {
77
+ it('stamps the tenant on create, overriding what the client sent', async () => {
78
+ const { client, backend } = wire('acme')
79
+ const created = await client.createRow!({ id: '9', name: 'New', tenantId: 'globex' } as Partial<Row>)
80
+ expect(created.tenantId).toBe('acme')
81
+ const { rows } = await backend.getRows(req())
82
+ expect(rows.find((r) => r.id === '9')!.tenantId).toBe('acme')
83
+ })
84
+
85
+ it('rejects updating a row in another tenant', async () => {
86
+ const { client } = wire('acme')
87
+ await expect(client.updateRow!('3', { name: 'hacked' })).rejects.toThrow()
88
+ })
89
+
90
+ it('rejects deleting a row in another tenant', async () => {
91
+ const { client } = wire('acme')
92
+ await expect(client.deleteRow!('3')).rejects.toThrow()
93
+ })
94
+
95
+ it('leaves the other tenant\'s data untouched after a rejected write', async () => {
96
+ const { client, backend } = wire('acme')
97
+ await client.updateRow!('3', { name: 'hacked' }).catch(() => {})
98
+ await client.deleteRow!('3').catch(() => {})
99
+ const { rows } = await backend.getRows(req())
100
+ expect(rows.find((r) => r.id === '3')).toEqual({ id: '3', name: 'Globex secret', tenantId: 'globex' })
101
+ })
102
+
103
+ it('allows updating and deleting the caller\'s own rows', async () => {
104
+ const { client } = wire('acme')
105
+ const updated = await client.updateRow!('1', { name: 'Renamed' })
106
+ expect(updated.name).toBe('Renamed')
107
+ await expect(client.deleteRow!('2')).resolves.not.toThrow()
108
+ })
109
+
110
+ it('a patch cannot move a row into another tenant', async () => {
111
+ const { client, backend } = wire('acme')
112
+ await client.updateRow!('1', { tenantId: 'globex' } as Partial<Row>)
113
+ const { rows } = await backend.getRows(req())
114
+ expect(rows.find((r) => r.id === '1')!.tenantId).toBe('acme')
115
+ })
116
+ })
117
+
118
+ describe('row scoping: resolver failure', () => {
119
+ it('rejects rather than falling through to an unscoped query', async () => {
120
+ // The dangerous failure mode: "I cannot tell which tenant you are" must not
121
+ // mean "show everything".
122
+ const { client } = wire('acme', { throwOnResolve: true })
123
+ await expect(client.getRows(req())).rejects.toThrow()
124
+ })
125
+ })
@@ -109,9 +109,29 @@ export type KitValidateContext<TData extends RowData> = {
109
109
  event: { request: Request; locals?: Record<string, unknown> }
110
110
  }
111
111
 
112
+ /** A row-scoping rule: every row this caller may touch has `field === value`. */
113
+ export type KitScope = { field: string; value: unknown }
114
+
112
115
  export type KitHandlerOptions<TData extends RowData> = {
113
116
  schema: EntitySchema<TData>
114
117
  source: ServerDataSource<TData>
118
+ /**
119
+ * Restrict every operation to rows matching one column - the mechanism behind
120
+ * multi-tenancy (`field: 'tenantId'`), and usable for any "you only see your
121
+ * own rows" rule.
122
+ *
123
+ * Enforced on all four paths, because scoping reads alone is not isolation:
124
+ * - **read**: the predicate is merged into the query's filter model;
125
+ * - **create**: the value is stamped onto the row, overriding whatever the
126
+ * client sent;
127
+ * - **update / delete**: the target row is re-read under the scope first, and
128
+ * the write is rejected with `403` if it isn't the caller's - otherwise
129
+ * guessing an id would reach across tenants.
130
+ *
131
+ * Return `null` to apply no scope (e.g. a super-admin). Throwing rejects the
132
+ * request, which is the safer default when a tenant cannot be resolved.
133
+ */
134
+ scope?: (ctx: { event: { request: Request; locals?: Record<string, unknown> } }) => KitScope | null | Promise<KitScope | null>
115
135
  /**
116
136
  * Optional server-side guard run BEFORE every op. Return `false` (or throw) to
117
137
  * reject the request with `403`. Receives the SvelteKit event, so you can read
@@ -188,7 +208,7 @@ const actionOf = <TData extends RowData>(msg: KitMessage<TData>): KitAction =>
188
208
  export function createKitHandlers<TData extends RowData>(
189
209
  options: KitHandlerOptions<TData>,
190
210
  ): KitHandlers {
191
- const { source, authorize, validate, audit, hooks } = options
211
+ const { source, authorize, validate, audit, hooks, scope } = options
192
212
  const idField = options.schema.idField ?? options.schema.fields.find((f) => f.primaryKey)?.field ?? 'id'
193
213
  const rowId = (row: unknown): string | null => {
194
214
  const v = (row as Record<string, unknown> | null)?.[idField]
@@ -237,13 +257,62 @@ export function createKitHandlers<TData extends RowData>(
237
257
  }
238
258
  }
239
259
 
260
+ // Resolve the row scope once per request. A thrown resolver is a rejection,
261
+ // not a 500: "I can't tell which tenant you are" must not fall through to
262
+ // an unscoped query.
263
+ let activeScope: KitScope | null = null
264
+ if (scope) {
265
+ try {
266
+ activeScope = (await scope({ event: { request, locals: event?.locals } })) ?? null
267
+ } catch (err) {
268
+ return jsonResponse({ error: err instanceof Error ? err.message : 'forbidden' }, 403)
269
+ }
270
+ }
271
+ /** True when `id` exists AND belongs to the caller's scope. */
272
+ const ownsRow = async (id: string): Promise<boolean> => {
273
+ if (!activeScope) return true
274
+ const idField = options.schema.idField ?? options.schema.fields.find((f) => f.primaryKey)?.field ?? 'id'
275
+ const { rows } = await source.getRows({
276
+ startRow: 0,
277
+ endRow: 1,
278
+ pageIndex: 0,
279
+ pageSize: 1,
280
+ sortModel: [],
281
+ filterModel: {
282
+ columns: {
283
+ [idField]: { operator: 'equals' as const, value: id },
284
+ [activeScope.field]: { operator: 'equals' as const, value: String(activeScope.value) },
285
+ },
286
+ },
287
+ })
288
+ return rows.length > 0
289
+ }
290
+
240
291
  try {
241
292
  if (msg.kind === 'query') {
242
- const result = await source.getRows(msg.request)
293
+ // Merge the scope into the requested filter. Written last so a client
294
+ // that sends its own `tenantId` filter cannot widen the scope.
295
+ const request = activeScope
296
+ ? {
297
+ ...msg.request,
298
+ filterModel: {
299
+ ...msg.request.filterModel,
300
+ columns: {
301
+ ...(msg.request.filterModel?.columns ?? {}),
302
+ [activeScope.field]: { operator: 'equals' as const, value: String(activeScope.value) },
303
+ },
304
+ },
305
+ }
306
+ : msg.request
307
+ const result = await source.getRows(request)
243
308
  return jsonResponse(result)
244
309
  }
245
310
  if (msg.kind === 'mutate') {
246
311
  const evt = { request, locals: event?.locals }
312
+ // Cross-tenant writes: reject before the source sees the id.
313
+ if (activeScope && (msg.op === 'update' || msg.op === 'delete')) {
314
+ if (!(await ownsRow(msg.id))) return jsonResponse({ error: 'forbidden' }, 403)
315
+ }
247
316
  // A `before*` hook throwing is a business-rule rejection -> 422 (not a 500).
248
317
  const runBefore = async <R>(fn: () => Promise<R>): Promise<R | Response> => {
249
318
  try { return await fn() } catch (err) { return jsonResponse({ error: err instanceof Error ? err.message : 'rejected' }, 422) }
@@ -256,6 +325,9 @@ export function createKitHandlers<TData extends RowData>(
256
325
  if (r instanceof Response) return r
257
326
  if (r) input = r as Partial<TData>
258
327
  }
328
+ // Stamp the scope LAST - after the before-hook - so neither the client
329
+ // nor a business rule can write a row into another tenant.
330
+ if (activeScope) input = { ...input, [activeScope.field]: activeScope.value } as Partial<TData>
259
331
  const row = await source.createRow(input)
260
332
  if (hooks?.afterCreate) await hooks.afterCreate({ row, event: evt })
261
333
  await fireAudit({ action: 'create', id: rowId(row), values: input, result: row, event: evt })
@@ -269,6 +341,8 @@ export function createKitHandlers<TData extends RowData>(
269
341
  if (r instanceof Response) return r
270
342
  if (r) patch = r as Partial<TData>
271
343
  }
344
+ // A patch must not be able to hand the row to another tenant.
345
+ if (activeScope) patch = { ...patch, [activeScope.field]: activeScope.value } as Partial<TData>
272
346
  const row = await source.updateRow(msg.id, patch)
273
347
  if (hooks?.afterUpdate) await hooks.afterUpdate({ id: msg.id, row, event: evt })
274
348
  await fireAudit({ action: 'update', id: msg.id, values: patch, result: row, event: evt })