@htilabs/platform-common-models 0.54.0 → 0.56.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 (41) hide show
  1. package/README.md +90 -0
  2. package/dist/authz/index.cjs +353 -2
  3. package/dist/authz/index.cjs.map +1 -1
  4. package/dist/authz/index.d.cts +330 -9
  5. package/dist/authz/index.d.ts +330 -9
  6. package/dist/authz/index.js +339 -1
  7. package/dist/authz/index.js.map +1 -1
  8. package/dist/{index-XWSYon40.d.cts → index-Bj-s0nZy.d.cts} +1 -1
  9. package/dist/{index-Bh1zO_Vu.d.ts → index-DvAypMJV.d.ts} +1 -1
  10. package/dist/index.cjs +503 -2
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +4 -4
  13. package/dist/index.d.ts +4 -4
  14. package/dist/index.js +483 -2
  15. package/dist/index.js.map +1 -1
  16. package/dist/models/index.d.cts +2 -2
  17. package/dist/models/index.d.ts +2 -2
  18. package/dist/schemas/index.d.cts +25 -25
  19. package/dist/schemas/index.d.ts +25 -25
  20. package/dist/sharing-ceiling/index.cjs +152 -2
  21. package/dist/sharing-ceiling/index.cjs.map +1 -1
  22. package/dist/sharing-ceiling/index.d.cts +214 -2
  23. package/dist/sharing-ceiling/index.d.ts +214 -2
  24. package/dist/sharing-ceiling/index.js +145 -2
  25. package/dist/sharing-ceiling/index.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/__tests__/authz-resolve.test.ts +5 -0
  28. package/src/__tests__/authz-viewing-policy.test.ts +685 -0
  29. package/src/__tests__/authz-viewing-scope.test.ts +1037 -0
  30. package/src/__tests__/core-function-default-bounds.test.ts +2 -2
  31. package/src/authz/functionDoor.ts +4 -4
  32. package/src/authz/index.ts +2 -0
  33. package/src/authz/resolveEffectiveFunctions.ts +6 -4
  34. package/src/authz/viewingPolicy.ts +525 -0
  35. package/src/authz/viewingScope.ts +359 -0
  36. package/src/sharingCeiling/__tests__/fireShareEvent.test.ts +437 -0
  37. package/src/sharingCeiling/fireShareEvent.ts +352 -0
  38. package/src/sharingCeiling/index.ts +4 -0
  39. package/src/sharingCeiling/resolve.ts +31 -5
  40. package/dist/{cvcEntry-D787utvP.d.cts → cvcEntry-BLeRivoF.d.cts} +8 -8
  41. package/dist/{cvcEntry-D787utvP.d.ts → cvcEntry-BLeRivoF.d.ts} +8 -8
@@ -981,6 +981,331 @@ function functionDoor(input) {
981
981
  reason: sentence(refusal, labelFor(functionKey, input.label), roleWords)
982
982
  };
983
983
  }
984
+
985
+ // src/authz/viewingPolicy.ts
986
+ var VIEWING_POLICY_SPECIES = "viewing";
987
+ var VIEWING_POLICY_VOCABULARIES = Object.freeze({
988
+ 3: Object.freeze({ scopeVersion: 3, oneHomeCarry: true, identifyingAlias: true }),
989
+ 4: Object.freeze({ scopeVersion: 3, oneHomeCarry: false, identifyingAlias: true }),
990
+ 5: Object.freeze({ scopeVersion: 4, oneHomeCarry: false, identifyingAlias: false })
991
+ });
992
+ function viewingPolicyVocabulary(scopeVocab) {
993
+ return scopeVocab === 3 || scopeVocab === 4 || scopeVocab === 5 ? VIEWING_POLICY_VOCABULARIES[scopeVocab] : null;
994
+ }
995
+ function unavailableViewingPolicy(reason, policyId) {
996
+ return {
997
+ source: "unavailable",
998
+ level: 0,
999
+ visibleGroups: [],
1000
+ scopeVocab: null,
1001
+ policy: null,
1002
+ reason,
1003
+ policyId: policyId ?? null,
1004
+ rulePath: `viewing policy unavailable: ${reason}` + (policyId ? ` (${policyId})` : "") + " \u2014 no view of other teams' cases"
1005
+ };
1006
+ }
1007
+ var HEX_OBJECT_ID = /^[0-9a-fA-F]{24}$/;
1008
+ function objectIdHex(v) {
1009
+ if (typeof v === "object" && v !== null && "toHexString" in v) {
1010
+ const toHex = v.toHexString;
1011
+ if (typeof toHex === "function") {
1012
+ const hex = toHex.call(v);
1013
+ if (typeof hex === "string") return hex.toLowerCase();
1014
+ }
1015
+ }
1016
+ return null;
1017
+ }
1018
+ function idText(v) {
1019
+ if (typeof v === "string") return v === "" ? null : v;
1020
+ return objectIdHex(v);
1021
+ }
1022
+ function normalizedId(id) {
1023
+ return HEX_OBJECT_ID.test(id) ? id.toLowerCase() : id;
1024
+ }
1025
+ function policyIdOf(d) {
1026
+ return idText(d.entity_id) ?? idText(d.id) ?? idText(d._id) ?? "(no id)";
1027
+ }
1028
+ function placementOf(d) {
1029
+ if (!Array.isArray(d.assignments) || d.assignments.length !== 1) {
1030
+ return { kind: "unreadable" };
1031
+ }
1032
+ const [a] = d.assignments;
1033
+ if (typeof a !== "object" || a === null) return { kind: "unreadable" };
1034
+ const { scope, teamId } = a;
1035
+ if (scope === "account") return { kind: "account" };
1036
+ const team = idText(teamId);
1037
+ if (scope === "team" && team !== null) {
1038
+ return { kind: "team", teamId: normalizedId(team) };
1039
+ }
1040
+ return { kind: "unreadable" };
1041
+ }
1042
+ function positiveIntOrNull(v) {
1043
+ if (v === void 0 || v === null) return null;
1044
+ return typeof v === "number" && Number.isInteger(v) && v >= 1 ? v : void 0;
1045
+ }
1046
+ function governingRowOf(rows) {
1047
+ const [only] = rows;
1048
+ if (rows.length === 1 && only !== void 0) return { row: only };
1049
+ const family = idText(rows[0]?.familyId);
1050
+ const versions = rows.map((r) => positiveIntOrNull(r.version));
1051
+ const ordered = family !== null && rows.every((r) => idText(r.familyId) === family) && versions.every((v) => typeof v === "number") && new Set(versions).size === versions.length;
1052
+ if (!ordered) {
1053
+ return unavailableViewingPolicy("ambiguous_rows", policyIdOf(rows[0] ?? {}));
1054
+ }
1055
+ let best = rows[0];
1056
+ for (const r of rows) {
1057
+ if (r.version > best.version) best = r;
1058
+ }
1059
+ return { row: best };
1060
+ }
1061
+ function whitelistProblem(visibleGroups, vocab, scope) {
1062
+ const reading = scope.readSelection(visibleGroups, vocab.scopeVersion, {
1063
+ subject: "policy"
1064
+ });
1065
+ if (reading.unsupportedVersion) return "unsupported_scope_vocab";
1066
+ if (reading.unknown.length > 0 || reading.retired.length > 0) {
1067
+ return "unknown_visible_group";
1068
+ }
1069
+ return null;
1070
+ }
1071
+ function withoutRestRows(visibleGroups, scope) {
1072
+ return visibleGroups.filter((code) => scope.row(code)?.rowType !== "category_rest");
1073
+ }
1074
+ function ruleText(source, ref) {
1075
+ const v = ref.version === null ? "" : ` v${ref.version}`;
1076
+ return source === "team" ? `${ref.id}${v} (the team's own row \u2014 it governs this team in place of the account-wide rule)` : `${ref.id}${v} (the account-wide viewing rule \u2014 no row for this team)`;
1077
+ }
1078
+ function readRow(d, source, accountId, scope) {
1079
+ const policyId = policyIdOf(d);
1080
+ const level = d.level;
1081
+ if (level !== 0 && level !== 1 && level !== 2 && level !== 3) {
1082
+ return unavailableViewingPolicy("malformed_level", policyId);
1083
+ }
1084
+ let visibleGroups = [];
1085
+ if (d.visibleGroups !== void 0 && d.visibleGroups !== null) {
1086
+ if (!Array.isArray(d.visibleGroups) || !d.visibleGroups.every((g) => typeof g === "string")) {
1087
+ return unavailableViewingPolicy("malformed_visible_groups", policyId);
1088
+ }
1089
+ visibleGroups = [...d.visibleGroups];
1090
+ }
1091
+ const scopeVocab = positiveIntOrNull(d.scopeVocab);
1092
+ if (scopeVocab === void 0) {
1093
+ return unavailableViewingPolicy("malformed_scope_vocab", policyId);
1094
+ }
1095
+ const vocab = viewingPolicyVocabulary(scopeVocab);
1096
+ if (scopeVocab === null || vocab === null) {
1097
+ return unavailableViewingPolicy("unsupported_scope_vocab", policyId);
1098
+ }
1099
+ const problem = whitelistProblem(visibleGroups, vocab, scope);
1100
+ if (problem !== null) return unavailableViewingPolicy(problem, policyId);
1101
+ const policy = {
1102
+ id: policyId,
1103
+ familyId: idText(d.familyId),
1104
+ version: positiveIntOrNull(d.version) ?? null
1105
+ };
1106
+ return {
1107
+ source,
1108
+ level,
1109
+ visibleGroups: withoutRestRows(visibleGroups, scope),
1110
+ scopeVocab,
1111
+ policy,
1112
+ accountId,
1113
+ rulePath: ruleText(source, policy)
1114
+ };
1115
+ }
1116
+ function resolveViewingPolicy(docs, query, scope) {
1117
+ const accountRaw = typeof query.accountId === "string" ? query.accountId.trim() : "";
1118
+ if (accountRaw === "") return unavailableViewingPolicy("account_unresolved");
1119
+ if (!Array.isArray(docs)) return unavailableViewingPolicy("read_error");
1120
+ const accountId = normalizedId(accountRaw);
1121
+ const teamRaw = typeof query.teamId === "string" ? query.teamId.trim() : "";
1122
+ const actingTeamId = teamRaw !== "" ? normalizedId(teamRaw) : null;
1123
+ const rows = [];
1124
+ for (const raw of docs) {
1125
+ if (typeof raw !== "object" || raw === null) {
1126
+ return unavailableViewingPolicy("read_error");
1127
+ }
1128
+ const d = raw;
1129
+ if (d.species !== VIEWING_POLICY_SPECIES) continue;
1130
+ const owner = idText(d.account_id);
1131
+ if (owner === null || normalizedId(owner) !== accountId) continue;
1132
+ rows.push(d);
1133
+ }
1134
+ const teamRows = [];
1135
+ const accountRows = [];
1136
+ for (const d of rows) {
1137
+ if (d.active === false) continue;
1138
+ const placement = placementOf(d);
1139
+ if (placement.kind === "unreadable") {
1140
+ return unavailableViewingPolicy("unreadable_assignment", policyIdOf(d));
1141
+ }
1142
+ const governsHere = placement.kind === "account" || actingTeamId !== null && placement.teamId === actingTeamId;
1143
+ if (!governsHere) continue;
1144
+ if (d.active !== true) {
1145
+ return unavailableViewingPolicy("malformed_active", policyIdOf(d));
1146
+ }
1147
+ if (placement.kind === "account") accountRows.push(d);
1148
+ else teamRows.push(d);
1149
+ }
1150
+ const [source, candidates] = teamRows.length > 0 ? ["team", teamRows] : ["account", accountRows];
1151
+ if (candidates.length === 0) {
1152
+ const retiredRowsOnly = rows.length > 0 && rows.every((d) => d.active === false);
1153
+ return {
1154
+ source: "none",
1155
+ level: null,
1156
+ visibleGroups: null,
1157
+ scopeVocab: null,
1158
+ policy: null,
1159
+ accountId,
1160
+ retiredRowsOnly,
1161
+ rulePath: retiredRowsOnly ? "no viewing policy on file (every viewing row is retired) \u2014 the role level alone decides" : "no viewing policy on file \u2014 the role level alone decides"
1162
+ };
1163
+ }
1164
+ const governing = governingRowOf(candidates);
1165
+ if (!("row" in governing)) return governing;
1166
+ return readRow(governing.row, source, accountId, scope);
1167
+ }
1168
+ function teamViewingLevelFromPolicy(policy) {
1169
+ switch (policy.source) {
1170
+ case "team":
1171
+ case "account":
1172
+ return policy.level;
1173
+ case "none":
1174
+ return null;
1175
+ default:
1176
+ return 0;
1177
+ }
1178
+ }
1179
+
1180
+ // src/authz/viewingScope.ts
1181
+ var CURRENT_VIEWING_POLICY_VOCAB = 5;
1182
+ var RELATIONSHIPS_HOUSEHOLD = "relationships_household";
1183
+ var RELATIONSHIP_DYNAMICS = "relationship_dynamics";
1184
+ function viewingWhitelistFromPolicy(policy) {
1185
+ switch (policy.source) {
1186
+ case "none":
1187
+ return null;
1188
+ case "team":
1189
+ case "account":
1190
+ return { informationScope: [...policy.visibleGroups], scopeVocab: policy.scopeVocab };
1191
+ default:
1192
+ return { informationScope: [], scopeVocab: null };
1193
+ }
1194
+ }
1195
+ function caseViewingAccess(ctx, scope) {
1196
+ if (ctx.canSeeCase !== true) {
1197
+ return { mode: "none", reason: "cannot_see_case" };
1198
+ }
1199
+ if (ctx.ownedByViewer === true) return { mode: "full", reason: "owned" };
1200
+ if (ctx.level === 1 || ctx.level === 2) {
1201
+ return { mode: "stub", level: ctx.level };
1202
+ }
1203
+ if (ctx.level !== 3) return { mode: "none", reason: "level_0" };
1204
+ if (ctx.whitelist === null) {
1205
+ return { mode: "full", reason: "no_viewing_policy" };
1206
+ }
1207
+ const stored = ctx.whitelist ?? {
1208
+ informationScope: [],
1209
+ scopeVocab: CURRENT_VIEWING_POLICY_VOCAB
1210
+ };
1211
+ const stamp = typeof stored.scopeVocab === "number" ? stored.scopeVocab : null;
1212
+ const vocab = viewingPolicyVocabulary(stamp);
1213
+ const reading = scope.readSelection(
1214
+ stored.informationScope,
1215
+ vocab !== null ? vocab.scopeVersion : stamp,
1216
+ { subject: "policy" }
1217
+ );
1218
+ const identifyingAlias = vocab !== null && vocab.identifyingAlias && reading.informationScope.includes("identifying");
1219
+ return {
1220
+ mode: "whitelist",
1221
+ reading,
1222
+ visibleScope: identifyingAlias ? [...reading.informationScope, RELATIONSHIPS_HOUSEHOLD] : reading.informationScope,
1223
+ oneHomeCarry: vocab !== null && vocab.oneHomeCarry,
1224
+ relationshipDynamicsCarry: identifyingAlias
1225
+ };
1226
+ }
1227
+ function caseSurfacesOn(access, surface) {
1228
+ switch (access.mode) {
1229
+ case "none":
1230
+ return false;
1231
+ case "full":
1232
+ case "whitelist":
1233
+ return true;
1234
+ case "stub":
1235
+ if (surface === "search") return true;
1236
+ if (surface === "autocomplete") return access.level === 2;
1237
+ return false;
1238
+ }
1239
+ }
1240
+ function caseSurfacesAsStub(access) {
1241
+ return access.mode === "stub";
1242
+ }
1243
+ var ONE_HOME_CARRY = [
1244
+ { homeRow: "summary_ida", domains: /* @__PURE__ */ new Set(["ida_determination"]), unlockedBy: "risk" },
1245
+ {
1246
+ homeRow: "summary_needs_assessment",
1247
+ domains: /* @__PURE__ */ new Set(["needs_assessment", "recommended_followup"]),
1248
+ unlockedBy: "services"
1249
+ }
1250
+ ];
1251
+ function isClientStubRow(scope, code) {
1252
+ const row = scope.row(code);
1253
+ return row !== void 0 && row.levelFloor === 1 && row.rowType !== "branch" && (row.kindBranch === "client" || row.kindBranch === null);
1254
+ }
1255
+ function recordViewing(access, record, viewerUserId, scope) {
1256
+ if (access.mode === "none" && access.reason === "cannot_see_case") {
1257
+ return { visible: false, reason: "cannot_see_case" };
1258
+ }
1259
+ const author = record.authorUserId;
1260
+ if (typeof author === "string" && author.trim().length > 0 && author === viewerUserId) {
1261
+ return { visible: true, reason: "author" };
1262
+ }
1263
+ switch (access.mode) {
1264
+ case "none":
1265
+ return { visible: false, reason: access.reason };
1266
+ case "full":
1267
+ return { visible: true, reason: "full" };
1268
+ case "stub":
1269
+ return { visible: false, reason: "not_opened" };
1270
+ case "whitelist":
1271
+ return whitelistDecision(access, record, scope);
1272
+ }
1273
+ }
1274
+ function whitelistDecision(access, record, scope) {
1275
+ const home = scope.row(record.homeRow);
1276
+ if (home === void 0 || home.kindRow !== void 0) {
1277
+ return { visible: false, reason: "unknown_row" };
1278
+ }
1279
+ if ((record.subjectKind === "client" || home.kindBranch !== "client") && isClientStubRow(scope, record.homeRow)) {
1280
+ return { visible: true, reason: "stub_row" };
1281
+ }
1282
+ const list = access.visibleScope;
1283
+ if (scope.isCovered(list, record.homeRow)) {
1284
+ return { visible: true, reason: "covered" };
1285
+ }
1286
+ if (access.relationshipDynamicsCarry && record.domain === RELATIONSHIP_DYNAMICS) {
1287
+ return { visible: true, reason: "identifying_alias_carry" };
1288
+ }
1289
+ if (access.oneHomeCarry && !scope.isCovered(list, "instruments") && typeof record.domain === "string") {
1290
+ for (const carry of ONE_HOME_CARRY) {
1291
+ if (record.homeRow === carry.homeRow && carry.domains.has(record.domain) && scope.isCovered(list, carry.unlockedBy)) {
1292
+ return { visible: true, reason: "one_home_carry" };
1293
+ }
1294
+ }
1295
+ }
1296
+ return { visible: false, reason: "not_covered" };
1297
+ }
1298
+ function viewingRowVisible(access, homeRow, scope) {
1299
+ return recordViewing(access, { homeRow, subjectKind: "client" }, null, scope).visible;
1300
+ }
1301
+ function viewingComponentOpen(access, homeRow, scope) {
1302
+ if (access.mode === "full") return true;
1303
+ if (access.mode !== "whitelist") return false;
1304
+ const row = scope.row(homeRow);
1305
+ if (row === void 0 || row.kindRow !== void 0) return false;
1306
+ if (row.status === "retired") return false;
1307
+ return scope.isCovered(access.visibleScope, homeRow);
1308
+ }
984
1309
  export {
985
1310
  CORE_FUNCTION_DEFAULT_BOUNDS,
986
1311
  CORE_FUNCTION_DEFAULT_BOUNDS_SOT_VERSION,
@@ -989,14 +1314,27 @@ export {
989
1314
  DEFAULT_BOUND_STATE_LABELS,
990
1315
  DEFAULT_BOUND_STATE_VALUES,
991
1316
  VIEWING_LEVEL_FUNCTIONS,
1317
+ VIEWING_POLICY_SPECIES,
1318
+ VIEWING_POLICY_VOCABULARIES,
992
1319
  canonicalRoleCode,
1320
+ caseSurfacesAsStub,
1321
+ caseSurfacesOn,
1322
+ caseViewingAccess,
993
1323
  codebookDefaultBound,
994
1324
  explainFunctionRefusal,
995
1325
  functionDoor,
996
1326
  isCanonEvaluableFunction,
1327
+ recordViewing,
997
1328
  resolveEffectiveFunctions,
998
1329
  resolveEffectiveViewingLevel,
1330
+ resolveViewingPolicy,
999
1331
  selectActingTeamId,
1000
- viewingLevelFromFunctions
1332
+ teamViewingLevelFromPolicy,
1333
+ unavailableViewingPolicy,
1334
+ viewingComponentOpen,
1335
+ viewingLevelFromFunctions,
1336
+ viewingPolicyVocabulary,
1337
+ viewingRowVisible,
1338
+ viewingWhitelistFromPolicy
1001
1339
  };
1002
1340
  //# sourceMappingURL=index.js.map