From d348e58ecdf1eb49203640771b52bee342ecd111 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 7 Dec 2019 11:51:38 +0100 Subject: [PATCH] Can safely get the ID of a user in a request --- src/entities/RequestHandler.ts | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 3277e0a..b297eab 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -6,6 +6,7 @@ import { AccountHelper } from "../helpers/AccountHelper"; import { UploadedFile } from "express-fileupload"; import { prepareFileCreation, generateNewUserDataFileName, pathUserData } from "../utils/UserDataUtils"; import * as sharp from 'sharp'; +import { UserHelper } from "../helpers/UserHelper"; /** * Response to a request @@ -162,16 +163,39 @@ export class RequestHandler { * Get a boolean included in the request * * @param name The name of the POST field + * @param fallback Fallback value to use if the value is not + * found in the request */ - public postBool(name: string) : boolean { + public postBool(name: string, fallback ?: boolean) : boolean { const param = this.getPostParam(name); - if(param == undefined) + if(param == undefined) { + if(fallback != undefined) + return fallback; + this.error(400, "Missing boolean '" + name + "' in the request!"); + } return param === "true" || param === true; } + /** + * Get the ID of a user specified in a POST request + * + * @param name Name of the POST field + */ + public async postUserId(name: string) : Promise { + const userID = this.postInt(name); + + if(userID < 1) + this.error(400, "Invalid user ID specified in '" + name +"'!"); + + if(!await UserHelper.Exists(userID)) + this.error(404, "User with ID " + userID + " not found!"); + + return userID; + } + /** * Get information about an uploaded file *