]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/users.ts
b7b9ef370079197fc390a78e1cf78bf4db013b7f
[github/Chocobozzz/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, function (err, user) {
17 if (err) {
18 logger.error('Error in usersAdd request validator.', { error: err })
19 return res.sendStatus(500)
20 }
21
22 if (user) return res.status(409).send('User already exists.')
23
24 next()
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, function (err, user) {
36 if (err) {
37 logger.error('Error in usersRemove request validator.', { error: err })
38 return res.sendStatus(500)
39 }
40
41 if (!user) return res.status(404).send('User not found')
42
43 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
44
45 next()
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, function (err, video) {
68 if (err) {
69 logger.error('Error in user request validator.', { error: err })
70 return res.sendStatus(500)
71 }
72
73 if (!video) return res.status(404).send('Video not found')
74
75 next()
76 })
77 })
78 }
79
80 // ---------------------------------------------------------------------------
81
82 export {
83 usersAddValidator,
84 usersRemoveValidator,
85 usersUpdateValidator,
86 usersVideoRatingValidator
87 }