All files / realm/blong-access/adapter/db accessUserRemove.ts

100% Statements 67/67
72.72% Branches 8/11
100% Functions 1/1
100% Lines 67/67

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 681x 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 10x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 10x  
import {type IMeta} from '@feasibleone/blong';
 
import {handler} from './handlerGroup.ts';
 
type KnexQb = any;
 
/**
 * `access.user.remove` — delete a user and everything that references it.
 *
 * The user is named either way a caller may have it: `userId` when the id was read from
 * a form or a token, `userName` — the login, which is what a list of accounts shows and
 * what a person removing a handful of them knows. The name is resolved to its
 * `core_resource` row inside the transaction, so a concurrent rename cannot slip between
 * the lookup and the delete, and a name nobody holds is refused rather than answered
 * `success` (`@@D-649`).
 *
 * Everything that points at the user goes with it: the graph edges in both directions
 * (the user's `hasRole`, and membership edges that name the user as their object), its
 * record-level ACL rules, the session rows, the credential rows, the `access_user` row and
 * finally the `core_resource` row. The paths are rebuilt once, before the resource row is
 * deleted, because `core_path` holds a foreign key to it. Deleting the rows and leaving an
 * edge or an ACL rule behind would leave a principal id no reader can name.
 *
 * The sessions go before the credentials, not after: `access_session.credentialId` is a
 * foreign key into `access_credential`, so a user who is signed in — the one an operator
 * removes last, having just used it — refuses the delete if the credentials are removed
 * first (`@@F-713`).
 */
export default handler(({lib: {binHex, resourceIdByName}, errors: {errorUserNotFound}}) => ({
    async accessUserRemove(
        params: {userId?: string; userName?: string},
        $meta: IMeta,
    ): Promise<{success: boolean; userId: string; userName?: string}> {
        const qb: KnexQb = this.config?.context?.queryBuilder;
        if (!qb) throw new Error('Database not available');
        const hex = binHex(params.userId);
        const name = params.userName?.trim();
        // A missing id used to answer `success` while removing nothing — a caller
        // that passed the id under another key (this handler takes a flat
        // `{userId}`, not the `{user: {userId}}` of add/edit) saw a clean result
        // and the user stayed. @@D-649
        if (!hex && !name) throw new Error('access.user.remove requires a userId or a userName');
 
        const removed = await qb.transaction(async (trx: KnexQb) => {
            const resolved = hex ?? binHex(await resourceIdByName(trx, name!, 'access.user'));
            if (!resolved) throw errorUserNotFound({params: {userName: name}}, $meta);
            const buf = Buffer.from(resolved, 'hex');
 
            const edges =
                (await trx('core_triple').where('subjectId', buf).del()) +
                (await trx('core_triple').where('objectId', buf).del());
            await trx('access_acl').where('principalId', buf).del();
            await trx('access_session').where('userId', buf).del();
            await trx('access_credential').where('userId', buf).del();
            await trx('access_user').where('userId', buf).del();
            if (edges > 0) await trx.raw('CALL access_pathRefresh()');
            await trx('core_resource').where('resourceId', buf).del();
            return resolved;
        });
 
        return {
            success: true,
            userId: Buffer.from(removed, 'hex').toString('base64'),
            ...(name ? {userName: name} : {}),
        };
    },
}));