X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fusers.ts;h=1a33cfd8ceb6571aac43e20d324e5ef9c6b40b09;hb=d412e80e5f748f92118541a5334c14ebd4a90881;hp=c06735047eca51b70898d40f0bf0ff4cf87b4db8;hpb=6fcd19ba737f1f5614a56c6925adb882dea43b8d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index c06735047..1a33cfd8c 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,87 +1,206 @@ +import { body, param } from 'express-validator/check' import 'express-validator' import * as express from 'express' +import * as Promise from 'bluebird' +import * as validator from 'validator' import { database as db } from '../../initializers/database' import { checkErrors } from './utils' -import { logger } from '../../helpers' - -function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { - req.checkBody('username', 'Should have a valid username').isUserUsernameValid() - req.checkBody('password', 'Should have a valid password').isUserPasswordValid() - req.checkBody('email', 'Should have a valid email').isEmail() - - logger.debug('Checking usersAdd parameters', { parameters: req.body }) - - checkErrors(req, res, function () { - db.User.loadByUsernameOrEmail(req.body.username, req.body.email) - .then(user => { - if (user) return res.status(409).send('User already exists.') - - next() +import { + isSignupAllowed, + logger, + isUserUsernameValid, + isUserPasswordValid, + isUserVideoQuotaValid, + isUserDisplayNSFWValid, + isIdOrUUIDValid +} from '../../helpers' +import { UserInstance, VideoInstance } from '../../models' + +const usersAddValidator = [ + body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), + body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), + body('email').isEmail().withMessage('Should have a valid email'), + body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking usersAdd parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next) + }) + } +] + +const usersRegisterValidator = [ + body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), + body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), + body('email').isEmail().withMessage('Should have a valid email'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking usersRegister parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next) + }) + } +] + +const usersRemoveValidator = [ + param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking usersRemove parameters', { parameters: req.params }) + + checkErrors(req, res, () => { + checkUserExists(req.params.id, res, (err, user) => { + if (err) { + logger.error('Error in usersRemoveValidator.', err) + return res.sendStatus(500) + } + + if (user.username === 'root') { + return res.status(400) + .send({ error: 'Cannot remove the root user' }) + .end() + } + + return next() }) - .catch(err => { - logger.error('Error in usersAdd request validator.', { error: err }) - return res.sendStatus(500) - }) - }) -} + }) + } +] + +const usersUpdateValidator = [ + param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), + body('email').optional().isEmail().withMessage('Should have a valid email attribute'), + body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking usersUpdate parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + checkUserExists(req.params.id, res, next) + }) + } +] + +const usersUpdateMeValidator = [ + body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), + body('email').optional().isEmail().withMessage('Should have a valid email attribute'), + body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + // TODO: Add old password verification + logger.debug('Checking usersUpdateMe parameters', { parameters: req.body }) + + checkErrors(req, res, next) + } +] + +const usersGetValidator = [ + param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + checkErrors(req, res, () => { + checkUserExists(req.params.id, res, next) + }) + } +] + +const usersVideoRatingValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) + + checkErrors(req, res, () => { + let videoPromise: Promise + + if (validator.isUUID(req.params.videoId)) { + videoPromise = db.Video.loadByUUID(req.params.videoId) + } else { + videoPromise = db.Video.load(req.params.videoId) + } + + videoPromise + .then(video => { + if (!video) { + return res.status(404) + .json({ error: 'Video not found' }) + .end() + } + + return next() + }) + .catch(err => { + logger.error('Error in user request validator.', err) + return res.sendStatus(500) + }) + }) + } +] + +const ensureUserRegistrationAllowed = [ + (req: express.Request, res: express.Response, next: express.NextFunction) => { + isSignupAllowed().then(allowed => { + if (allowed === false) { + return res.status(403) + .send({ error: 'User registration is not enabled or user limit is reached.' }) + .end() + } + + return next() + }) + } +] -function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) { - req.checkParams('id', 'Should have a valid id').notEmpty().isInt() - - logger.debug('Checking usersRemove parameters', { parameters: req.params }) - - checkErrors(req, res, function () { - db.User.loadById(req.params.id) - .then(user => { - if (!user) return res.status(404).send('User not found') - - if (user.username === 'root') return res.status(400).send('Cannot remove the root user') +// --------------------------------------------------------------------------- - next() - }) - .catch(err => { - logger.error('Error in usersRemove request validator.', { error: err }) - return res.sendStatus(500) - }) - }) +export { + usersAddValidator, + usersRegisterValidator, + usersRemoveValidator, + usersUpdateValidator, + usersUpdateMeValidator, + usersVideoRatingValidator, + ensureUserRegistrationAllowed, + usersGetValidator } -function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { - req.checkParams('id', 'Should have a valid id').notEmpty().isInt() - // Add old password verification - req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() - req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid() - - logger.debug('Checking usersUpdate parameters', { parameters: req.body }) +// --------------------------------------------------------------------------- - checkErrors(req, res, next) +function checkUserExists (id: number, res: express.Response, callback: (err: Error, user: UserInstance) => void) { + db.User.loadById(id) + .then(user => { + if (!user) { + return res.status(404) + .send({ error: 'User not found' }) + .end() + } + + res.locals.user = user + return callback(null, user) + }) + .catch(err => { + logger.error('Error in user request validator.', err) + return res.sendStatus(500) + }) } -function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) { - req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) - - logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) - - checkErrors(req, res, function () { - db.Video.load(req.params.videoId) - .then(video => { - if (!video) return res.status(404).send('Video not found') +function checkUserDoesNotAlreadyExist (username: string, email: string, res: express.Response, callback: () => void) { + db.User.loadByUsernameOrEmail(username, email) + .then(user => { + if (user) { + return res.status(409) + .send({ error: 'User already exists.' }) + .end() + } - next() + return callback() }) .catch(err => { - logger.error('Error in user request validator.', { error: err }) + logger.error('Error in usersAdd request validator.', err) return res.sendStatus(500) }) - }) -} - -// --------------------------------------------------------------------------- - -export { - usersAddValidator, - usersRemoveValidator, - usersUpdateValidator, - usersVideoRatingValidator }