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, function (err, user) {
18 logger.error('Error in usersAdd request validator.', { error: err })
19 return res.sendStatus(500)
22 if (user) return res.status(409).send('User already exists.')
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, function (err, user) {
37 logger.error('Error in usersRemove request validator.', { error: err })
38 return res.sendStatus(500)
41 if (!user) return res.status(404).send('User not found')
43 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
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, function (err, video) {
69 logger.error('Error in user request validator.', { error: err })
70 return res.sendStatus(500)
73 if (!video) return res.status(404).send('Video not found')
80 // ---------------------------------------------------------------------------
86 usersVideoRatingValidator