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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x | import {type IMeta} from '@feasibleone/blong';
import {handler} from './handlerGroup.ts';
type KnexQb = any;
/**
* `access.capability.add` — create a capability (resource + `access_capability`
* row) plus its assigned actions.
*
* Reuses `core.resource.ensure` (the generic knex `add` cannot create the
* resource row for a `uidNotNull` PK) and writes the `hasAction` edges with their
* rebuild in `syncEdges`. Ticked CRUD columns on the `action` pivot row
* ensure the `accessCapability<predicate>` action resources and include them;
* `granted` `otherAction` rows are included as-is.
*/
export default handler(({lib: {binHex, bufToBase64, crudPivotActionIds, syncEdges, uuidBuf}}) => ({
async accessCapabilityAdd(
params: {
capability?: {capabilityName?: string; description?: string};
action?: Array<{
entityName?: string;
find?: boolean;
get?: boolean;
add?: boolean;
edit?: boolean;
remove?: boolean;
}>;
otherAction?: Array<{actionId?: string; granted?: boolean}>;
},
$meta: IMeta,
): Promise<Record<string, unknown>> {
const qb: KnexQb = this.config?.context?.queryBuilder;
if (!qb) throw new Error('Database not available');
const capability = params.capability ?? {};
const name = capability.capabilityName || `capability-${Date.now()}`;
// `core.resource.ensure` comes from `core`, a library realm this process carries
// itself, so `super` reaches it by prototype-chain delegation — same process, same
// connection (core is an early child of every suite that has it).
const {resourceId} = (await super.coreResourceEnsure(
{
name,
typeAlias: 'access.capability',
table: 'access_capability',
extraColumns: {description: capability.description ?? null},
keyName: 'capabilityId',
},
$meta,
)) as {resourceId: string};
const capabilityIdHex = binHex(resourceId);
if (!capabilityIdHex) throw new Error('Could not resolve capability resource id');
const crudIds = await crudPivotActionIds(
qb,
// The helper has no `super` of its own, so the binding is closed over here.
(p: any, m: IMeta) => super.coreResourceEnsure(p, m),
params.action ?? [],
$meta,
);
const otherIds = (params.otherAction ?? [])
.filter(r => r.granted)
.map(r => binHex(r.actionId))
.filter((x): x is string => !!x);
const all = [...crudIds, ...otherIds];
if (all.length) {
await syncEdges(qb, capabilityIdHex, 'hasAction', all);
}
return {
capability: {
capabilityId: bufToBase64(uuidBuf(resourceId)),
capabilityName: name,
description: capability.description ?? null,
},
};
},
}));
|