@aglyn/plugins-data 1.12.0 → 1.14.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.
Files changed (43) hide show
  1. package/package.json +10 -10
  2. package/src/lib/artifact/dataset-schema-artifact.server.js +6 -3
  3. package/src/lib/artifact/dataset-schema-artifact.server.js.map +1 -1
  4. package/src/lib/components/dataset-schema-dialog.component.d.ts +14 -0
  5. package/src/lib/components/dataset-schema-dialog.component.js +317 -239
  6. package/src/lib/components/dataset-schema-dialog.component.js.map +1 -1
  7. package/src/lib/components/host-datasets-card.component.js +71 -34
  8. package/src/lib/components/host-datasets-card.component.js.map +1 -1
  9. package/src/lib/components/use-dataset-writes-blocked.d.ts +50 -12
  10. package/src/lib/components/use-dataset-writes-blocked.js +86 -19
  11. package/src/lib/components/use-dataset-writes-blocked.js.map +1 -1
  12. package/src/lib/declarations.d.ts +25 -0
  13. package/src/lib/declarations.js +37 -0
  14. package/src/lib/declarations.js.map +1 -0
  15. package/src/lib/model/data-permissions.d.ts +34 -0
  16. package/src/lib/model/data-permissions.js +108 -0
  17. package/src/lib/model/data-permissions.js.map +1 -0
  18. package/src/lib/model/dataset-access.d.ts +22 -0
  19. package/src/lib/model/dataset-access.js +30 -0
  20. package/src/lib/model/dataset-access.js.map +1 -0
  21. package/src/lib/model/dataset-record.js +8 -3
  22. package/src/lib/model/dataset-record.js.map +1 -1
  23. package/src/lib/plugin.js +4 -1
  24. package/src/lib/plugin.js.map +1 -1
  25. package/src/lib/server/dataset-access.server.d.ts +41 -0
  26. package/src/lib/server/dataset-access.server.js +49 -0
  27. package/src/lib/server/dataset-access.server.js.map +1 -0
  28. package/src/lib/server/dataset-figures.js +12 -0
  29. package/src/lib/server/dataset-figures.js.map +1 -1
  30. package/src/lib/server/dataset-site-erasure.d.ts +2 -2
  31. package/src/lib/server/dataset-site-erasure.js +4 -26
  32. package/src/lib/server/dataset-site-erasure.js.map +1 -1
  33. package/src/lib/server/datasets-route.d.ts +2 -0
  34. package/src/lib/server/datasets-route.js +178 -27
  35. package/src/lib/server/datasets-route.js.map +1 -1
  36. package/src/lib/site-bundle/datasets-site-bundle.server.d.ts +4 -2
  37. package/src/lib/site-bundle/datasets-site-bundle.server.js +8 -4
  38. package/src/lib/site-bundle/datasets-site-bundle.server.js.map +1 -1
  39. package/src/lib/transfer/dataset-transfer-access.server.d.ts +23 -0
  40. package/src/lib/transfer/dataset-transfer-access.server.js +45 -0
  41. package/src/lib/transfer/dataset-transfer-access.server.js.map +1 -0
  42. package/src/lib/transfer/dataset-transfer-resource.js +2 -1
  43. package/src/lib/transfer/dataset-transfer-resource.js.map +1 -1
@@ -14,13 +14,17 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
14
14
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
- */ import { defaultScopeForNewResource, newResourceScopeFields, pluginRequestFromWeb } from "@aglyn/aglyn/server";
17
+ */ import { isOrgWideMember } from "@aglyn/aglyn/app-utils/organizations";
18
+ import { sharingFromChoice } from "@aglyn/aglyn/app-utils/scope-tokens";
19
+ import { defaultScopeForNewResource, newResourceScopeFields, pluginRequestFromWeb } from "@aglyn/aglyn/server";
18
20
  import { checkDatasetQuota, checkEntitlement, checkQuota, createResourceUid, ensureDeclaredCustomFieldTypes, memberCanSee } from "@aglyn/aglyn/server";
19
21
  import { emailUnverifiedResponse, firebaseAdmin, dataStorageRefusal, isImpersonationSession, isServerReleaseFlagOnForOrg, lockdownRefusal, memberHasOrgPermission, resolveOrgIdForHost, resolveOrgMembership } from "@aglyn/tenant-data-admin";
20
22
  import { isRefusedIdToken } from "@aglyn/tenant-data-admin/server/id-token-refusal";
21
- import { permissionRefusalText } from "@aglyn/aglyn/app-utils/org-permissions";
23
+ import { fieldsToWrite, orgHostIdsFromDoc } from "@aglyn/tenant-data-admin/server/scope-sharing-sweep";
22
24
  import { FieldPath, FieldValue, Timestamp } from "firebase-admin/firestore";
25
+ import { dataPermissionRefusal } from "../model/dataset-access.js";
23
26
  import { announceDatasetRecords as announceDatasetChange } from "./announce-dataset-records.js";
27
+ import { datasetRefusalPermission, memberMayActOnDataset } from "./dataset-access.server.js";
24
28
  import { coerceDocumentValues, datasetIntegrityFields, datasetIntegrityUpdate, effectiveDatasetModel, validateDocument } from "../model/dataset-models.js";
25
29
  import { defaultDatasetFieldId } from "../model/datasets.js";
26
30
  import { planReferenceFixups, referencingFieldIds, restrictedDeleteMessage } from "../model/dataset-record-delete-plan.js";
@@ -45,15 +49,50 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
45
49
  * The key every action here asks (AGL-4047): "Manage data", whose default is
46
50
  * the owner, admin and editor, as the rules' `canWriteOrgDatasets()` asks it.
47
51
  */ const DATASETS_PERMISSION = 'data.manage';
48
- /** The 403 for a member without it, naming the permission, never a role. */ function datasetsPermissionRefusal() {
52
+ /** The 403 for a member without it, naming the permission, never a role. */ function datasetsPermissionRefusal(permission = DATASETS_PERMISSION) {
49
53
  return Response.json({
50
- error: permissionRefusalText(DATASETS_PERMISSION),
54
+ error: dataPermissionRefusal(permission),
51
55
  reason: 'permission',
52
- permission: DATASETS_PERMISSION
56
+ permission
53
57
  }, {
54
58
  status: 403
55
59
  });
56
60
  }
61
+ /**
62
+ * The actions on one existing dataset, whose answer depends on where it is
63
+ * placed (AGL-4047): `memberMayActOnDataset`, asked once the dataset is read.
64
+ * Every other action asks "Manage data" up front.
65
+ */ const DATASET_ACTIONS = new Set([
66
+ 'create-record',
67
+ 'import-records',
68
+ 'announce-records',
69
+ 'add-address-field',
70
+ 'update-record',
71
+ 'delete-record'
72
+ ]);
73
+ /** The most site ids one sharing choice may name: far past any workspace. */ const SHARING_CHOICE_MAX_IDS = 1000;
74
+ /** A sharing choice off the wire, or null for anything else. Ids are checked against the workspace after. */ function sharingChoiceFrom(value) {
75
+ const choice = value != null ? value : {};
76
+ const ids = (list)=>Array.isArray(list) && list.length <= SHARING_CHOICE_MAX_IDS && list.every((id)=>typeof id === 'string' && /^[^:/]+$/.test(id)) ? list : null;
77
+ if (choice.mode === 'all') return {
78
+ mode: 'all'
79
+ };
80
+ if (choice.mode === 'selected') {
81
+ const hostIds = ids(choice.hostIds);
82
+ return hostIds && {
83
+ mode: 'selected',
84
+ hostIds
85
+ };
86
+ }
87
+ if (choice.mode === 'allExcept') {
88
+ const exceptHostIds = ids(choice.exceptHostIds);
89
+ return exceptHostIds && {
90
+ mode: 'allExcept',
91
+ exceptHostIds
92
+ };
93
+ }
94
+ return null;
95
+ }
57
96
  /**
58
97
  * Rows written per import transaction.
59
98
  *
@@ -84,6 +123,8 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
84
123
  * - `update-record` / `delete-record`: the Data card's record edit and
85
124
  * delete (with its reference integrity) for the native apps, which have no
86
125
  * browser to write from (AGL-3668).
126
+ * - `set-sharing`: who a dataset is shared with: all sites, the sites chosen, or
127
+ * all sites except some (AGL-4047).
87
128
  *
88
129
  * Since AGL-2163 both record actions ALSO enforce `checkDataStorageQuota`.
89
130
  * See {@link refuseIfDataStorageBlocked}: that check's `allowed` field had no
@@ -139,7 +180,10 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
139
180
  * route as it opens the rules' `canWriteOrgDatasets()`, and a refusal
140
181
  * names the permission. Staff keep their bypass, as the rules' `isStaff()`
141
182
  * does, and still need a membership.
142
- *=========================================*/ if (decoded['staff'] !== true && !await memberHasOrgPermission(orgId, member, DATASETS_PERMISSION)) {
183
+ *
184
+ * The actions on one dataset ask below, once it is read (AGL-4047): a
185
+ * dataset placed on one site answers to that site as well.
186
+ *=========================================*/ if (decoded['staff'] !== true && !DATASET_ACTIONS.has(action) && !await memberHasOrgPermission(orgId, member, DATASETS_PERMISSION)) {
143
187
  return datasetsPermissionRefusal();
144
188
  }
145
189
  // Release gate (AGL-1653). `<FeatureGate flag="release_data_store">` on
@@ -310,10 +354,87 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
310
354
  status: 200
311
355
  });
312
356
  }
313
- if (action === 'create-record' || action === 'import-records' || action === 'announce-records' || action === 'add-address-field' || action === 'update-record' || action === 'delete-record') {
357
+ /**
358
+ * Who a dataset is shared with (AGL-4047).
359
+ *
360
+ * Who may do it is what the rule on a client's `visibleTo` write asks: "Manage
361
+ * data", asked above, with every site in reach. A site's verdict writes a
362
+ * dataset and never moves it.
363
+ *
364
+ * The `scopeMode` and `scopeExcept` of a dataset shared with all sites except
365
+ * some are the server's alone (the rules' `scopeMarkerUnchanged`), so this is
366
+ * the one door that sets or removes them, and removing them is a field delete.
367
+ *
368
+ * The workspace's sites are read in the transaction: a site being created
369
+ * writes the same document, so it either lands first and is on this list, or
370
+ * lands after and its sweep finds this dataset. A sweep can't miss it.
371
+ *
372
+ * A site this workspace doesn't hold is dropped from a choice, never refused
373
+ * and never stored: it may be one that has just gone, and a token no site
374
+ * holds would make a dataset nobody could reach.
375
+ */ if (action === 'set-sharing') {
314
376
  var _ref4;
315
- var _datasetSnapshot_data;
377
+ if (decoded['staff'] !== true && !isOrgWideMember(member)) {
378
+ return Response.json({
379
+ error: 'Changing who a dataset is shared with takes access to every site in the workspace.',
380
+ reason: 'reach'
381
+ }, {
382
+ status: 403
383
+ });
384
+ }
316
385
  const datasetId = String((_ref4 = body == null ? void 0 : body.datasetId) != null ? _ref4 : '');
386
+ const choice = sharingChoiceFrom(body == null ? void 0 : body.choice);
387
+ if (!datasetId || datasetId.includes('/') || !choice) {
388
+ return Response.json({
389
+ error: 'Missing datasetId or sharing choice'
390
+ }, {
391
+ status: 400
392
+ });
393
+ }
394
+ const datasetRef = orgRef.collection('datasets').doc(datasetId);
395
+ const answer = await firestore.runTransaction(async (tx)=>{
396
+ const [liveOrg, dataset] = await tx.getAll(orgRef, datasetRef);
397
+ if (!dataset.exists) return {
398
+ status: 404,
399
+ error: 'Unknown dataset'
400
+ };
401
+ const orgHostIds = orgHostIdsFromDoc(liveOrg.data());
402
+ const shared = sharingFromChoice(choice.mode === 'selected' ? {
403
+ mode: 'selected',
404
+ hostIds: choice.hostIds.filter((id)=>orgHostIds.includes(id))
405
+ } : choice, orgHostIds);
406
+ if (!shared.fields) return {
407
+ status: 400,
408
+ error: shared.problem
409
+ };
410
+ tx.update(datasetRef, _extends({}, fieldsToWrite(shared.fields), {
411
+ updatedAt: Timestamp.now()
412
+ }));
413
+ return {
414
+ status: 200,
415
+ visibleTo: shared.fields.visibleTo,
416
+ scopeExcept: shared.fields.scopeExcept
417
+ };
418
+ });
419
+ if (answer.status !== 200) {
420
+ return Response.json({
421
+ error: answer.error
422
+ }, {
423
+ status: answer.status
424
+ });
425
+ }
426
+ return Response.json({
427
+ ok: true,
428
+ visibleTo: answer.visibleTo,
429
+ scopeExcept: answer.scopeExcept
430
+ }, {
431
+ status: 200
432
+ });
433
+ }
434
+ if (DATASET_ACTIONS.has(action)) {
435
+ var _ref5;
436
+ var _datasetSnapshot_data;
437
+ const datasetId = String((_ref5 = body == null ? void 0 : body.datasetId) != null ? _ref5 : '');
317
438
  if (!datasetId) {
318
439
  return Response.json({
319
440
  error: 'Missing datasetId'
@@ -333,8 +454,8 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
333
454
  /**
334
455
  * The caller has to be able to SEE the dataset they add rows to.
335
456
  *
336
- * The `data.manage` gate above is about the org, and a member scoped
337
- * to one site can pass it. The rules hold every record update and
457
+ * "Manage data" is about the org, and a member scoped to one site can
458
+ * hold it. The rules hold every record update and
338
459
  * delete to the parent dataset being visible (`parentDatasetVisible()`),
339
460
  * and refuse client record creates outright, so this route is the one
340
461
  * place a member's create can be held to the same condition. Without it,
@@ -353,6 +474,21 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
353
474
  });
354
475
  }
355
476
  /**
477
+ * Who may write THIS dataset (AGL-4047), the rules' `canWriteDatasetOn`:
478
+ * "Manage data", unless a site narrows the member on the one site it is
479
+ * placed on; or that one site's verdict.
480
+ */ const placedOn = datasetSnapshot.data();
481
+ if (decoded['staff'] !== true && !await memberMayActOnDataset({
482
+ firestore,
483
+ orgId,
484
+ uid: decoded.uid,
485
+ member,
486
+ dataset: placedOn,
487
+ act: 'write'
488
+ })) {
489
+ return datasetsPermissionRefusal(datasetRefusalPermission('write', placedOn));
490
+ }
491
+ /**
356
492
  * A record the BROWSER wrote, announcing itself (AGL-3113).
357
493
  *
358
494
  * Record edits and deletes stay client-direct on purpose — they consume
@@ -391,8 +527,8 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
391
527
  * an address field keeps it: the call fills that field's empty
392
528
  * addresses instead of adding a second one.
393
529
  */ if (action === 'add-address-field') {
394
- var _ref5;
395
- const sourceField = String((_ref5 = body == null ? void 0 : body.sourceField) != null ? _ref5 : '').trim();
530
+ var _ref6;
531
+ const sourceField = String((_ref6 = body == null ? void 0 : body.sourceField) != null ? _ref6 : '').trim();
396
532
  const source = model.fields[sourceField];
397
533
  if (!source || source.type !== 'text' || isRecordAddressField(source)) {
398
534
  return Response.json({
@@ -475,12 +611,12 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
475
611
  * Firestore — it consumes no quota — with the values coerced, filled
476
612
  * and validated against the model and the integrity and filter index
477
613
  * moved with them; the native apps reach the same write here, under
478
- * the same `data.manage` and visibility gates as a create.
614
+ * the same write and visibility gates as a create.
479
615
  * `values` is replaced whole, so a removed field's value strips here,
480
616
  * as it does on the card (the AGL-178 policy).
481
617
  */ if (action === 'update-record') {
482
- var _ref6, _ref7;
483
- const recordId = String((_ref6 = body == null ? void 0 : body.recordId) != null ? _ref6 : '');
618
+ var _ref7, _ref8;
619
+ const recordId = String((_ref7 = body == null ? void 0 : body.recordId) != null ? _ref7 : '');
484
620
  if (!recordId) {
485
621
  return Response.json({
486
622
  error: 'Missing recordId'
@@ -496,7 +632,7 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
496
632
  status: 404
497
633
  });
498
634
  }
499
- const coerced = fillRecordAddresses(model, coerceDocumentValues(model, (_ref7 = body == null ? void 0 : body.values) != null ? _ref7 : {}));
635
+ const coerced = fillRecordAddresses(model, coerceDocumentValues(model, (_ref8 = body == null ? void 0 : body.values) != null ? _ref8 : {}));
500
636
  const errors = validateDocument(model, coerced);
501
637
  if (Object.keys(errors).length) {
502
638
  return Response.json({
@@ -534,8 +670,8 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
534
670
  * datasets consulted are the ones the caller can see, exactly the set
535
671
  * the card reads, so the app is refused exactly when the console is.
536
672
  */ if (action === 'delete-record') {
537
- var _ref8;
538
- const recordId = String((_ref8 = body == null ? void 0 : body.recordId) != null ? _ref8 : '');
673
+ var _ref9;
674
+ const recordId = String((_ref9 = body == null ? void 0 : body.recordId) != null ? _ref9 : '');
539
675
  if (!recordId) {
540
676
  return Response.json({
541
677
  error: 'Missing recordId'
@@ -562,20 +698,35 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
562
698
  values: holder.get('values')
563
699
  })));
564
700
  if (plan.kind === 'restricted') {
565
- var _ref9, _ref10;
701
+ var _ref10, _ref11;
566
702
  var _other_data, _other_data1;
567
- const name = String((_ref9 = (_ref10 = (_other_data = other.data()) == null ? void 0 : _other_data.displayName) != null ? _ref10 : (_other_data1 = other.data()) == null ? void 0 : _other_data1.name) != null ? _ref9 : '').trim();
703
+ const name = String((_ref10 = (_ref11 = (_other_data = other.data()) == null ? void 0 : _other_data.displayName) != null ? _ref11 : (_other_data1 = other.data()) == null ? void 0 : _other_data1.name) != null ? _ref10 : '').trim();
568
704
  return Response.json({
569
705
  error: restrictedDeleteMessage(plan.holders, name)
570
706
  }, {
571
707
  status: 409
572
708
  });
573
709
  }
574
- if (plan.kind === 'strip') fixups.push({
575
- datasetId: other.id,
576
- model: otherModel,
577
- updates: plan.updates
578
- });
710
+ if (plan.kind === 'strip') {
711
+ // Stripping a reference writes THAT dataset, so it asks what writing
712
+ // it asks, as the card's client-direct batch does at the rule (AGL-4047).
713
+ const otherPlacedOn = other.data();
714
+ if (decoded['staff'] !== true && !await memberMayActOnDataset({
715
+ firestore,
716
+ orgId,
717
+ uid: decoded.uid,
718
+ member,
719
+ dataset: otherPlacedOn,
720
+ act: 'write'
721
+ })) {
722
+ return datasetsPermissionRefusal(datasetRefusalPermission('write', otherPlacedOn));
723
+ }
724
+ fixups.push({
725
+ datasetId: other.id,
726
+ model: otherModel,
727
+ updates: plan.updates
728
+ });
729
+ }
579
730
  }
580
731
  for (const fixup of fixups){
581
732
  const holdersRef = orgRef.collection('datasets').doc(fixup.datasetId).collection('records');
@@ -611,8 +762,8 @@ import { RECORD_PAGE_ADDRESS_FIELD_TYPE, fillRecordAddresses, isRecordAddressFie
611
762
  const recordCount = (await recordsRef.count().get()).data().count;
612
763
  const overRecordQuota = (limit)=>`Record limit reached (${limit}) — upgrade in Billing`;
613
764
  if (action === 'create-record') {
614
- var _ref11;
615
- const coerced = fillRecordAddresses(model, coerceDocumentValues(model, (_ref11 = body == null ? void 0 : body.values) != null ? _ref11 : {}));
765
+ var _ref12;
766
+ const coerced = fillRecordAddresses(model, coerceDocumentValues(model, (_ref12 = body == null ? void 0 : body.values) != null ? _ref12 : {}));
616
767
  const errors = validateDocument(model, coerced);
617
768
  if (Object.keys(errors).length) {
618
769
  return Response.json({