]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/users.ts
Remove ng2-completer
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
CommitLineData
69818c93
C
1import 'express-validator'
2import * as express from 'express'
3
e02643f3 4import { database as db } from '../../initializers/database'
65fcc311
C
5import { checkErrors } from './utils'
6import { logger } from '../../helpers'
9bd26629 7
69818c93 8function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
9bd26629
C
9 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
10 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
ad4a8a1c 11 req.checkBody('email', 'Should have a valid email').isEmail()
9bd26629 12
9bd26629
C
13 logger.debug('Checking usersAdd parameters', { parameters: req.body })
14
bf68dd75 15 checkErrors(req, res, function () {
ad4a8a1c 16 db.User.loadByUsernameOrEmail(req.body.username, req.body.email, function (err, user) {
bf68dd75
C
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 })
9bd26629
C
27}
28
69818c93 29function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
feb4bdfd 30 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
9bd26629
C
31
32 logger.debug('Checking usersRemove parameters', { parameters: req.params })
33
34 checkErrors(req, res, function () {
feb4bdfd 35 db.User.loadById(req.params.id, function (err, user) {
9bd26629
C
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
af1068ce
C
43 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
44
9bd26629
C
45 next()
46 })
47 })
48}
49
69818c93 50function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
feb4bdfd 51 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
9bd26629 52 // Add old password verification
1d49e1e2
C
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()
9bd26629
C
55
56 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
57
58 checkErrors(req, res, next)
59}
60
69818c93 61function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
d38b8281
C
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
9bd26629
C
80// ---------------------------------------------------------------------------
81
65fcc311
C
82export {
83 usersAddValidator,
84 usersRemoveValidator,
85 usersUpdateValidator,
86 usersVideoRatingValidator
87}