/* * Copyright (c) 2022. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ import { removeRobotCredentialsFromVault } from '@authup/server-common'; import { BadRequestError, ForbiddenError, NotFoundError } from '@ebec/http'; import { PermissionName, REALM_MASTER_NAME, ROBOT_SYSTEM_NAME, } from '@authup/common'; import type { Request, Response } from 'routup'; import { sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { RobotEntity, resolveRealm } from '@authup/server-database'; import { useRequestEnv } from '../../../utils'; export async function deleteRobotRouteHandler(req: Request, res: Response) : Promise { const id = useRequestParam(req, 'id'); const dataSource = await useDataSource(); const repository = dataSource.getRepository(RobotEntity); const entity = await repository.findOneBy({ id }); if (!entity) { throw new NotFoundError(); } const ability = useRequestEnv(req, 'ability'); if (!ability.has(PermissionName.ROBOT_DROP)) { if (!entity.user_id) { throw new ForbiddenError(); } if ( entity.user_id && entity.user_id !== useRequestEnv(req, 'userId') ) { throw new ForbiddenError(); } } if (entity.name === ROBOT_SYSTEM_NAME) { const realm = await resolveRealm(entity.realm_id); if (realm.name === REALM_MASTER_NAME) { throw new BadRequestError('The system robot can not be deleted.'); } } // ---------------------------------------------- const { id: entityId } = entity; await repository.remove(entity); entity.id = entityId; // ---------------------------------------------- // todo: this should be executed through a message broker await removeRobotCredentialsFromVault(entity); // ---------------------------------------------- return sendAccepted(res, entity); }