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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 10x 1x | import {type IMeta} from '@feasibleone/blong';
import {type AclMatrixRow} from './accessModel.ts';
import {handler} from './handlerGroup.ts';
type KnexQb = any;
/**
* `access.user.edit` — update a user's columns plus its credential rows and
* granted roles.
*
* The standard `access_user` update runs through the automatic knex CRUD
* (`super.exec`). Credential rows are then synced (only when the form actually
* submitted a `credential` array — otherwise they are left untouched), the
* `hasRole` edges are brought in line with the submitted `role` array, and the
* `matrix` array updates the user's own scope-level `access_acl` rules.
*/
export default handler(
({
lib: {
binHex,
credentialPolicyParams,
crockfordDecode,
hashPassword,
resolveRoleIds,
syncAclMatrix,
syncCredentials,
syncEdges,
ulid,
},
}) => ({
async accessUserEdit(
params: {
user?: {userId?: string; emailAddress?: string; isActive?: boolean};
credential?: Array<Record<string, unknown>>;
role?: Array<{roleId?: string; roleName?: string; granted?: boolean}>;
matrix?: AclMatrixRow[];
},
$meta: IMeta,
): Promise<unknown> {
const qb: KnexQb = this.config?.context?.queryBuilder;
if (!qb) throw new Error('Database not available');
const result = await super.exec(params, $meta);
const hex = binHex(params.user?.userId);
if (!hex) throw new Error('Invalid user id');
// The entity row is the framework's own edit case, which is already one
// transaction; everything this handler writes after it is the second, and
// it has to be one: the credentials (several rows), the granted roles
// (with the rebuild inside `syncEdges`) and the scope ACL matrix used to
// commit one at a time, so a failure in the middle left an edit the caller
// was told had failed with half of its assignments already stored.
await qb.transaction(async (trx: KnexQb) => {
if (Array.isArray(params.credential)) {
await syncCredentials(
trx,
{hashPassword, credentialPolicyParams},
hex,
params.credential,
);
}
if (Array.isArray(params.role)) {
const roleIds = await resolveRoleIds(trx, params.role ?? []);
await syncEdges(trx, hex, 'hasRole', roleIds);
}
if (Array.isArray(params.matrix)) {
await syncAclMatrix(
trx,
{
// `core.resource.ensure` lives in `core`, a library realm
// this process carries itself, so `super` reaches it in
// the same process on the same connection — and it takes
// the transaction, so an ACL rule and the resource it
// points at are one write. The helper has no `super` of
// its own, so the binding is closed over here.
coreResourceEnsure: (p: any, m: IMeta) =>
super.coreResourceEnsure(p, m, trx),
newAclId: () => Buffer.from(crockfordDecode(ulid())),
},
hex,
params.matrix,
$meta,
);
}
});
return result;
},
}),
);
|