Move to promises
[oweals/peertube.git] / server / middlewares / validators / users.ts
1 import 'express-validator'
2 import * as express from 'express'
3
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { logger } from '../../helpers'
7
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()
12
13   logger.debug('Checking usersAdd parameters', { parameters: req.body })
14
15   checkErrors(req, res, function () {
16     db.User.loadByUsernameOrEmail(req.body.username, req.body.email)
17       .then(user => {
18         if (user) return res.status(409).send('User already exists.')
19
20         next()
21       })
22       .catch(err => {
23         logger.error('Error in usersAdd request validator.', { error: err })
24         return res.sendStatus(500)
25       })
26   })
27 }
28
29 function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
30   req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
31
32   logger.debug('Checking usersRemove parameters', { parameters: req.params })
33
34   checkErrors(req, res, function () {
35     db.User.loadById(req.params.id)
36       .then(user => {
37         if (!user) return res.status(404).send('User not found')
38
39         if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
40
41         next()
42       })
43       .catch(err => {
44         logger.error('Error in usersRemove request validator.', { error: err })
45         return res.sendStatus(500)
46       })
47   })
48 }
49
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()
55
56   logger.debug('Checking usersUpdate parameters', { parameters: req.body })
57
58   checkErrors(req, res, next)
59 }
60
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)
63
64   logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
65
66   checkErrors(req, res, function () {
67     db.Video.load(req.params.videoId)
68       .then(video => {
69         if (!video) return res.status(404).send('Video not found')
70
71         next()
72       })
73       .catch(err => {
74         logger.error('Error in user request validator.', { error: err })
75         return res.sendStatus(500)
76       })
77   })
78 }
79
80 // ---------------------------------------------------------------------------
81
82 export {
83   usersAddValidator,
84   usersRemoveValidator,
85   usersUpdateValidator,
86   usersVideoRatingValidator
87 }