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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 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 3x 3x 1x 2x 2x 1x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 12x 33x 33x 33x 33x 33x 33x 33x 33x 12x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 12x 44x 44x 44x 44x 12x 11x 11x 1x 1x 1x | import {
capitalize,
checkSubject,
tripleName,
type PrimitiveContext,
type PrimitiveDescriptor,
type PrimitiveFile,
} from '../engine.ts';
import {appendCommaListValue, ensureCommaListValue, readPlainSource} from '../merge.ts';
import {seedRowName} from './shared.ts';
/** `seed` — production and test seed data, including the RBAC grant. */
/**
* One entity's seed file.
*
* `key:` names the identity column; the rows are deliberately stable so a
* browse screenshot taken against them is reproducible.
*/
function seedSource(ctx: PrimitiveContext): PrimitiveFile {
const {subject, object} = ctx;
const kindLabel = ctx.kind === 'test' ? 'Test' : 'Production';
const dir = ctx.kind === 'test' ? 'meta/dbTest' : 'meta/db';
return {
path: `${dir}/${subject}${capitalize(object)}Merge.yaml`,
content: `# ${kindLabel} seed for ${subject}.${object}.
# The dispatched method is derived from the file name (the '<name>Merge' suffix).
key: ${object}Id
${object}:
- ${object}Id: 101
${object}Name: ${seedRowName(object)}
${object}Status: draft
- ${object}Id: 102
${object}Name: Sample ${capitalize(object)} Two
${object}Status: sent
`,
};
}
/**
* The realm's RBAC seed — a **production** seed.
*
* A new entity is unreachable through the gateway until the realm's `Manage`
* capability lists its actions, so granting them is part of scaffolding the
* entity rather than an afterthought. The grant belongs to the production seed
* because the roles (`Admin`, `Manager`, `Customer`, `Guest`) and the
* capabilities are production provisioning; only the accounts that hold those
* roles in a test run live in `meta/dbTest` (F-613).
*
* The file belongs to the realm template (it dispatches blong-access's shared
* `access.authorization.merge`), so it is composed into rather than generated.
*/
const RBAC_SEED = 'meta/db/accessAuthorizationMerge.yaml';
const seed: PrimitiveDescriptor = {
id: 'seed',
title: 'Seed data',
skill: 'blong-schema',
summary:
'Production seeds (meta/db) and test seeds (meta/dbTest) as <name>Merge.yaml. Each seed ' +
"also grants the entity's actions to the realm's production capabilities.",
kinds: ['prod', 'test'],
defaultKind: 'prod',
roots: ['meta/db', 'meta/dbTest'],
check(ctx) {
return checkSubject(ctx.subject);
},
files(ctx) {
return [seedSource(ctx)];
},
compose({host, root, context}) {
const files = [seedSource(context)];
/** The realm name as a person writes it: `test` → `Test`, for role names. */
const Subject = capitalize(context.subject);
const rbac = readPlainSource(host, root, RBAC_SEED);
if (!rbac) {
// A realm that ships no RBAC seed must hear that its entity is
// unreachable, not be scaffolded in silence (@@F-635).
return [
{
...files[0],
notices: [
`no ${RBAC_SEED} in this realm, so the ${context.object} actions were not ` +
'granted; add them to the capabilities that hold the rest of the realm ' +
'or every gateway call will be denied.',
],
},
];
}
/**
* The realm's production seed declares the three capabilities the
* Storybook toolbar's Role item demonstrates — full access, read + edit,
* read-only — so a new entity joins all three, or switching to Manager or
* Guest in a story would show an empty (403) page instead of a narrower
* one. Written for either kind of seed: the rows a seed carries are not
* what decides whether the entity may be called.
*/
const grants: [string, string[]][] = [
[`${context.subject}Manage`, ['add', 'find', 'get', 'edit', 'remove']],
[`${context.subject}Maintain`, ['add', 'find', 'get', 'edit']],
[`${context.subject}View`, ['find', 'get']],
];
let merged = rbac;
const missing: string[] = [];
for (const [capability, actions] of grants) {
const next = appendCommaListValue(
merged,
capability,
actions.map(action => tripleName(context.subject, context.object, action)),
);
if (next === undefined) missing.push(capability);
else merged = next;
}
if (missing.length) {
return [
{
...files[0],
notices: [
`${RBAC_SEED} does not declare ${missing
.map(capability => `'${capability}'`)
.join(' or ')}; add the ${context.object} actions to them by hand ` +
'or every call will be denied through the gateway.',
],
},
];
}
/**
* The realm-scoped roles (`Test Admin`, `Test Manager`, …).
*
* One deployment loads several realms, so the shared `Admin` role is not
* enough to hand somebody "the sport pages": these roles grant this
* realm's capabilities and nothing from a neighbour. `loginCapability`
* rides along because the login gate is realm-independent — without it
* the role could not open the portal at all.
*
* A realm scaffolded before the roles existed gains them here, which is
* why the entries are *created* rather than appended to.
*/
const realmRoles: [string, string[]][] = [
[`${Subject} Admin`, [`${context.subject}Manage`, 'loginCapability']],
[`${Subject} Manager`, [`${context.subject}Maintain`, 'loginCapability']],
[`${Subject} Customer`, [`${context.subject}View`, 'loginCapability']],
[`${Subject} Guest`, [`${context.subject}View`, 'loginCapability']],
];
const missingRoles: string[] = [];
for (const [role, capabilities] of realmRoles) {
const next = ensureCommaListValue(merged, 'role', role, capabilities);
if (next === undefined) missingRoles.push(role);
else merged = next;
}
if (missingRoles.length) {
return [
{
...files[0],
notices: [
`${RBAC_SEED} does not declare ${missingRoles
.map(entry => `'${entry}'`)
.join(' or ')}; add it by hand, or the realm will have no role ` +
'scoped to its own pages.',
],
},
];
}
return [...files, {path: RBAC_SEED, content: merged}];
},
};
export default seed;
|