1 const db = require('../../initializers/database')
2 import { checkErrors } from './utils'
3 import { logger } from '../../helpers'
5 function usersAddValidator (req, res, next) {
6 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
7 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
8 req.checkBody('email', 'Should have a valid email').isEmail()
10 logger.debug('Checking usersAdd parameters', { parameters: req.body })
12 checkErrors(req, res, function () {
13 db.User.loadByUsernameOrEmail(req.body.username, req.body.email, function (err, user) {
15 logger.error('Error in usersAdd request validator.', { error: err })
16 return res.sendStatus(500)
19 if (user) return res.status(409).send('User already exists.')
26 function usersRemoveValidator (req, res, next) {
27 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
29 logger.debug('Checking usersRemove parameters', { parameters: req.params })
31 checkErrors(req, res, function () {
32 db.User.loadById(req.params.id, function (err, user) {
34 logger.error('Error in usersRemove request validator.', { error: err })
35 return res.sendStatus(500)
38 if (!user) return res.status(404).send('User not found')
40 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
47 function usersUpdateValidator (req, res, next) {
48 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
49 // Add old password verification
50 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
51 req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
53 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
55 checkErrors(req, res, next)
58 function usersVideoRatingValidator (req, res, next) {
59 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4)
61 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
63 checkErrors(req, res, function () {
64 db.Video.load(req.params.videoId, function (err, video) {
66 logger.error('Error in user request validator.', { error: err })
67 return res.sendStatus(500)
70 if (!video) return res.status(404).send('Video not found')
77 // ---------------------------------------------------------------------------
83 usersVideoRatingValidator