1 import 'express-validator'
2 import * as express from 'express'
3 import * as Promise from 'bluebird'
4 import * as validator from 'validator'
6 import { database as db } from '../../initializers/database'
7 import { checkErrors } from './utils'
8 import { isSignupAllowed, logger } from '../../helpers'
9 import { VideoInstance } from '../../models'
11 function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
12 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
13 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
14 req.checkBody('email', 'Should have a valid email').isEmail()
16 logger.debug('Checking usersAdd parameters', { parameters: req.body })
18 checkErrors(req, res, () => {
19 db.User.loadByUsernameOrEmail(req.body.username, req.body.email)
21 if (user) return res.status(409).send('User already exists.')
26 logger.error('Error in usersAdd request validator.', err)
27 return res.sendStatus(500)
32 function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
33 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
35 logger.debug('Checking usersRemove parameters', { parameters: req.params })
37 checkErrors(req, res, () => {
38 db.User.loadById(req.params.id)
40 if (!user) return res.status(404).send('User not found')
42 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
47 logger.error('Error in usersRemove request validator.', err)
48 return res.sendStatus(500)
53 function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
54 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
55 // Add old password verification
56 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
57 req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
59 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
61 checkErrors(req, res, next)
64 function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
65 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid()
67 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
69 checkErrors(req, res, () => {
70 let videoPromise: Promise<VideoInstance>
72 if (validator.isUUID(req.params.videoId)) {
73 videoPromise = db.Video.loadByUUID(req.params.videoId)
75 videoPromise = db.Video.load(req.params.videoId)
80 if (!video) return res.status(404).send('Video not found')
85 logger.error('Error in user request validator.', err)
86 return res.sendStatus(500)
91 function ensureUserRegistrationAllowed (req: express.Request, res: express.Response, next: express.NextFunction) {
92 isSignupAllowed().then(allowed => {
93 if (allowed === false) {
94 return res.status(403).send('User registration is not enabled or user limit is reached.')
101 // ---------------------------------------------------------------------------
105 usersRemoveValidator,
106 usersUpdateValidator,
107 usersVideoRatingValidator,
108 ensureUserRegistrationAllowed