Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 2x 2x 2x 38x 38x 38x 38x 2x 2x 4x 4x 4x 4x 2x 2x 2x 8x 8x 8x 8x 8x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 8x 2x 2x 12x 12x 12x 12x 12x 11x 11x 11x 11x 11x 11x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x | import {type IMeta, handler} from '@feasibleone/blong';
// The knex query builder is intentionally untyped here (matches the access realm's db handlers).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type KnexQb = any;
/**
* dbTest seed — the organizational hierarchy the record-level ACL test
* (`test.acl.flow`) asserts against: an organization with a head office and two
* branches, a second organization nobody has a scope on, and the persons placed
* in those units. The ACL grants themselves live in
* `accessAuthorizationMerge.yaml`, which also creates the role they mention.
*
* It also creates the **consent records** of the explicit-mode matrix
* (`test.acl.matrix`), each owned by one of those persons. They are seeded here
* rather than in their own file because the rules that admit them name the
* records by name, and `20-` runs before the authorization merge that resolves
* those names.
*
* Every entity is created through `core_resource.ensure` (idempotent — an
* existing row is returned untouched), and the hierarchy is written as
* `core.triple` edges in one batch with a single `access_pathRefresh()`, so the
* materialized `access.effectiveRole` / `access.effectiveScope` paths are
* current before the tests run.
*
* Wire: `party.hierarchy.merge` — dispatched from
* `meta/dbTest/9-partyHierarchyMerge.yaml` (the numeric prefix orders it after
* the plain party/access seeds, which it depends on).
*
* Lives in `adapter/dbTest/` because it is a TEST-ONLY seed: the db adapter
* imports `.dbTest` handler groups only under `dev`, never in production.
*/
export default handler(({handler: {}}) => ({
async partyHierarchyMerge(
params: {
/** Organization names (short names — `legalName` is mirrored onto the resource). */
organizations?: string[];
units?: Array<{
name: string;
unitType?: string;
/** Owning organization name → `unit --belongsTo--> organization`. */
organization?: string;
/** Parent unit name → `unit --isPartOf--> parentUnit`. */
parentUnit?: string;
}>;
persons?: Array<{
/** Resource name (the display label). */
name: string;
firstName: string;
lastName: string;
/** Membership → `person --belongsTo--> unit`. */
unit?: string;
/**
* The `access.user` this person is the profile of → `user --hasProfile--> person`.
*
* The graph links the account to its human half, and a scope walk starts there, so a
* fixture that wants a caller with a position in the hierarchy has to say which
* account it is (@@D-836).
*/
user?: string;
}>;
consents?: Array<{
/** Resource name (the display label). */
name: string;
consentType: string;
/** Defaults to granted — the matrix reads, it never checks the flag. */
isGranted?: boolean;
/** Owner → `consent --belongsTo--> person`. */
belongsTo?: string;
}>;
},
$meta: IMeta,
): Promise<{success: boolean}> {
const qb: KnexQb = this.config?.context?.queryBuilder;
if (!qb) throw new Error('Database not available');
const triples: Array<{
subjectId: string;
predicateName: string;
objectId: string;
}> = [];
/** resource names are resolved per type so a name may repeat across types. */
const ids = new Map<string, string>();
const key = (typeAlias: string, name: string): string => `${typeAlias}:${name}`;
/** The binary form of a resource id, for the edge cleanup below. */
const bufferOf = (id: string): Buffer => Buffer.from(id.replace(/-/g, ''), 'hex');
/** Ensure a resource + entity row and remember its id for the edges. */
const ensure = async (
typeAlias: string,
table: string,
keyName: string,
name: string,
extraColumns: Record<string, unknown>,
): Promise<string> => {
const {resourceId} = (await super.coreResourceEnsure(
{name, typeAlias, table, extraColumns, keyName},
$meta,
)) as {resourceId: string};
ids.set(key(typeAlias, name), resourceId);
return resourceId;
};
/** The id of an entity ensured earlier in this seed. */
const idOf = (typeAlias: string, name: string): string => {
const id = ids.get(key(typeAlias, name));
if (!id) throw new Error(`ACL seed references an unknown ${typeAlias} "${name}"`);
return id;
};
for (const name of params.organizations ?? []) {
await ensure('party.organization', 'party_organization', 'organizationId', name, {
legalName: name,
});
}
// Pass 1 — every unit, so a child may be declared before its parent.
for (const unit of params.units ?? []) {
await ensure('party.unit', 'party_unit', 'unitId', unit.name, {
unitName: unit.name,
unitType: unit.unitType ?? null,
});
}
// Pass 2 — the unit hierarchy.
for (const unit of params.units ?? []) {
if (unit.organization) {
triples.push({
subjectId: idOf('party.unit', unit.name),
predicateName: 'belongsTo',
objectId: idOf('party.organization', unit.organization),
});
}
if (unit.parentUnit) {
triples.push({
subjectId: idOf('party.unit', unit.name),
predicateName: 'isPartOf',
objectId: idOf('party.unit', unit.parentUnit),
});
}
}
for (const person of params.persons ?? []) {
const personId = await ensure('party.person', 'party_person', 'personId', person.name, {
firstName: person.firstName,
lastName: person.lastName,
});
if (person.unit) {
triples.push({
subjectId: personId,
predicateName: 'belongsTo',
objectId: idOf('party.unit', person.unit),
});
}
if (person.user) {
const user = (await qb('core_resource as r')
.select('r.resourceId')
.join('core_type as t', 't.typeId', 'r.typeId')
.where('t.typeAlias', 'access.user')
.where('r.resourceName', person.user)
.first()) as {resourceId: Buffer} | undefined;
if (!user) throw new Error(`ACL seed references an unknown user "${person.user}"`);
const userId = Buffer.from(user.resourceId).toString('hex');
ids.set(key('access.user', person.user), userId);
triples.push({subjectId: userId, predicateName: 'hasProfile', objectId: personId});
}
}
// Consents last: their `belongsTo` edge names a person ensured above.
for (const consent of params.consents ?? []) {
const consentId = await ensure(
'party.consent',
'party_consent',
'consentId',
consent.name,
{
consentName: consent.name,
consentType: consent.consentType,
isGranted: consent.isGranted ?? true,
},
);
if (consent.belongsTo) {
triples.push({
subjectId: consentId,
predicateName: 'belongsTo',
objectId: idOf('party.person', consent.belongsTo),
});
}
}
// The seed is AUTHORITATIVE for the entities it names. The triple merge
// inserts with `onConflict` ignore, so an edge this fixture no longer
// declares would survive — a person moved to another unit would keep
// belonging to the old one and its scope set would never narrow again.
const subjectIds = [...ids.values()].map(bufferOf);
if (subjectIds.length) {
await qb('core_triple')
.whereIn('subjectId', subjectIds)
.whereIn('predicateName', ['belongsTo', 'isPartOf', 'hasProfile'])
.del();
}
// One path refresh for the whole batch (deferred per merge).
await super.coreTripleMerge({triples, refreshPath: true}, $meta);
return {success: true};
},
}));
|