X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fusers.ts;h=ebb3435355a50ef8632ca45d42c86112b7413abd;hb=8094a8980265a0a28e508dbd7cf7c7029e6d98b6;hp=eeb0e3557571744acae76b70dd489ef91c8b809e;hpb=980246ea8f1c51a137eaf0c441ef7e3b6fb88810;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index eeb0e3557..ebb343535 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -53,16 +53,35 @@ function usersRemoveValidator (req: express.Request, res: express.Response, next function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isInt() + req.checkBody('email', 'Should have a valid email attribute').optional().isEmail() + req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid() + + logger.debug('Checking usersUpdate parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + checkUserExists(req.params.id, res, next) + }) +} + +function usersUpdateMeValidator (req: express.Request, res: express.Response, next: express.NextFunction) { // Add old password verification req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() + req.checkBody('email', 'Should have a valid email attribute').optional().isEmail() req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid() - req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid() logger.debug('Checking usersUpdate parameters', { parameters: req.body }) checkErrors(req, res, next) } +function usersGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) { + req.checkParams('id', 'Should have a valid id').notEmpty().isInt() + + checkErrors(req, res, () => { + checkUserExists(req.params.id, res, next) + }) +} + function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid() @@ -106,6 +125,24 @@ export { usersAddValidator, usersRemoveValidator, usersUpdateValidator, + usersUpdateMeValidator, usersVideoRatingValidator, - ensureUserRegistrationAllowed + ensureUserRegistrationAllowed, + usersGetValidator +} + +// --------------------------------------------------------------------------- + +function checkUserExists (id: number, res: express.Response, callback: () => void) { + db.User.loadById(id) + .then(user => { + if (!user) return res.status(404).send('User not found') + + res.locals.user = user + callback() + }) + .catch(err => { + logger.error('Error in user request validator.', err) + return res.sendStatus(500) + }) }