]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/users.ts
require -> import
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
CommitLineData
e02643f3 1import { database as db } from '../../initializers/database'
65fcc311
C
2import { checkErrors } from './utils'
3import { logger } from '../../helpers'
9bd26629 4
65fcc311 5function usersAddValidator (req, res, next) {
9bd26629
C
6 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
7 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
ad4a8a1c 8 req.checkBody('email', 'Should have a valid email').isEmail()
9bd26629 9
9bd26629
C
10 logger.debug('Checking usersAdd parameters', { parameters: req.body })
11
bf68dd75 12 checkErrors(req, res, function () {
ad4a8a1c 13 db.User.loadByUsernameOrEmail(req.body.username, req.body.email, function (err, user) {
bf68dd75
C
14 if (err) {
15 logger.error('Error in usersAdd request validator.', { error: err })
16 return res.sendStatus(500)
17 }
18
19 if (user) return res.status(409).send('User already exists.')
20
21 next()
22 })
23 })
9bd26629
C
24}
25
65fcc311 26function usersRemoveValidator (req, res, next) {
feb4bdfd 27 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
9bd26629
C
28
29 logger.debug('Checking usersRemove parameters', { parameters: req.params })
30
31 checkErrors(req, res, function () {
feb4bdfd 32 db.User.loadById(req.params.id, function (err, user) {
9bd26629
C
33 if (err) {
34 logger.error('Error in usersRemove request validator.', { error: err })
35 return res.sendStatus(500)
36 }
37
38 if (!user) return res.status(404).send('User not found')
39
af1068ce
C
40 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
41
9bd26629
C
42 next()
43 })
44 })
45}
46
65fcc311 47function usersUpdateValidator (req, res, next) {
feb4bdfd 48 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
9bd26629 49 // Add old password verification
1d49e1e2
C
50 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
51 req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
9bd26629
C
52
53 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
54
55 checkErrors(req, res, next)
56}
57
65fcc311 58function usersVideoRatingValidator (req, res, next) {
d38b8281
C
59 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4)
60
61 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
62
63 checkErrors(req, res, function () {
64 db.Video.load(req.params.videoId, function (err, video) {
65 if (err) {
66 logger.error('Error in user request validator.', { error: err })
67 return res.sendStatus(500)
68 }
69
70 if (!video) return res.status(404).send('Video not found')
71
72 next()
73 })
74 })
75}
76
9bd26629
C
77// ---------------------------------------------------------------------------
78
65fcc311
C
79export {
80 usersAddValidator,
81 usersRemoveValidator,
82 usersUpdateValidator,
83 usersVideoRatingValidator
84}