1 import 'express-validator'
2 import * as express from 'express'
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { logger } from '../../helpers'
8 function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
9 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
10 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
11 req.checkBody('email', 'Should have a valid email').isEmail()
13 logger.debug('Checking usersAdd parameters', { parameters: req.body })
15 checkErrors(req, res, function () {
16 db.User.loadByUsernameOrEmail(req.body.username, req.body.email)
18 if (user) return res.status(409).send('User already exists.')
23 logger.error('Error in usersAdd request validator.', { error: err })
24 return res.sendStatus(500)
29 function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
30 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
32 logger.debug('Checking usersRemove parameters', { parameters: req.params })
34 checkErrors(req, res, function () {
35 db.User.loadById(req.params.id)
37 if (!user) return res.status(404).send('User not found')
39 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
44 logger.error('Error in usersRemove request validator.', { error: err })
45 return res.sendStatus(500)
50 function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
51 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
52 // Add old password verification
53 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
54 req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
56 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
58 checkErrors(req, res, next)
61 function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
62 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4)
64 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
66 checkErrors(req, res, function () {
67 db.Video.load(req.params.videoId)
69 if (!video) return res.status(404).send('Video not found')
74 logger.error('Error in user request validator.', { error: err })
75 return res.sendStatus(500)
80 // ---------------------------------------------------------------------------
86 usersVideoRatingValidator