]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/users.js
3 const checkErrors
= require('./utils').checkErrors
4 const db
= require('../../initializers/database')
5 const logger
= require('../../helpers/logger')
7 const validatorsUsers
= {
13 function usersAdd (req
, res
, next
) {
14 req
.checkBody('username', 'Should have a valid username').isUserUsernameValid()
15 req
.checkBody('password', 'Should have a valid password').isUserPasswordValid()
16 req
.checkBody('email', 'Should have a valid email').isEmail()
18 logger
.debug('Checking usersAdd parameters', { parameters: req
.body
})
20 checkErrors(req
, res
, function () {
21 db
.User
.loadByUsernameOrEmail(req
.body
.username
, req
.body
.email
, function (err
, user
) {
23 logger
.error('Error in usersAdd request validator.', { error: err
})
24 return res
.sendStatus(500)
27 if (user
) return res
.status(409).send('User already exists.')
34 function usersRemove (req
, res
, next
) {
35 req
.checkParams('id', 'Should have a valid id').notEmpty().isInt()
37 logger
.debug('Checking usersRemove parameters', { parameters: req
.params
})
39 checkErrors(req
, res
, function () {
40 db
.User
.loadById(req
.params
.id
, function (err
, user
) {
42 logger
.error('Error in usersRemove request validator.', { error: err
})
43 return res
.sendStatus(500)
46 if (!user
) return res
.status(404).send('User not found')
48 if (user
.username
=== 'root') return res
.status(400).send('Cannot remove the root user')
55 function usersUpdate (req
, res
, next
) {
56 req
.checkParams('id', 'Should have a valid id').notEmpty().isInt()
57 // Add old password verification
58 req
.checkBody('password', 'Should have a valid password').isUserPasswordValid()
60 logger
.debug('Checking usersUpdate parameters', { parameters: req
.body
})
62 checkErrors(req
, res
, next
)
65 // ---------------------------------------------------------------------------
67 module
.exports
= validatorsUsers