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()
15 req.checkBody('videoQuota', 'Should have a valid user quota').isUserVideoQuotaValid()
17 logger.debug('Checking usersAdd parameters', { parameters: req.body })
19 checkErrors(req, res, () => {
20 db.User.loadByUsernameOrEmail(req.body.username, req.body.email)
22 if (user) return res.status(409).send('User already exists.')
27 logger.error('Error in usersAdd request validator.', err)
28 return res.sendStatus(500)
33 function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
34 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
36 logger.debug('Checking usersRemove parameters', { parameters: req.params })
38 checkErrors(req, res, () => {
39 db.User.loadById(req.params.id)
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')
48 logger.error('Error in usersRemove request validator.', err)
49 return res.sendStatus(500)
54 function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
55 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
56 req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
57 req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid()
59 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
61 checkErrors(req, res, () => {
62 checkUserExists(req.params.id, res, next)
66 function usersUpdateMeValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
67 // Add old password verification
68 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
69 req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
70 req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
72 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
74 checkErrors(req, res, next)
77 function usersGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
78 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
80 checkErrors(req, res, () => {
81 checkUserExists(req.params.id, res, next)
85 function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
86 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid()
88 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
90 checkErrors(req, res, () => {
91 let videoPromise: Promise<VideoInstance>
93 if (validator.isUUID(req.params.videoId)) {
94 videoPromise = db.Video.loadByUUID(req.params.videoId)
96 videoPromise = db.Video.load(req.params.videoId)
101 if (!video) return res.status(404).send('Video not found')
106 logger.error('Error in user request validator.', err)
107 return res.sendStatus(500)
112 function ensureUserRegistrationAllowed (req: express.Request, res: express.Response, next: express.NextFunction) {
113 isSignupAllowed().then(allowed => {
114 if (allowed === false) {
115 return res.status(403).send('User registration is not enabled or user limit is reached.')
122 // ---------------------------------------------------------------------------
126 usersRemoveValidator,
127 usersUpdateValidator,
128 usersUpdateMeValidator,
129 usersVideoRatingValidator,
130 ensureUserRegistrationAllowed,
134 // ---------------------------------------------------------------------------
136 function checkUserExists (id: number, res: express.Response, callback: () => void) {
139 if (!user) return res.status(404).send('User not found')
141 res.locals.user = user
145 logger.error('Error in user request validator.', err)
146 return res.sendStatus(500)