1
0
Fork 0
peertube/server/middlewares/validators/users.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

'use strict'
const checkErrors = require('./utils').checkErrors
2016-12-11 15:50:51 -05:00
const db = require('../../initializers/database')
const logger = require('../../helpers/logger')
const validatorsUsers = {
usersAdd,
usersRemove,
2017-03-08 15:35:43 -05:00
usersUpdate,
usersVideoRating
}
function usersAdd (req, res, next) {
req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
2017-02-18 03:29:59 -05:00
req.checkBody('email', 'Should have a valid email').isEmail()
logger.debug('Checking usersAdd parameters', { parameters: req.body })
checkErrors(req, res, function () {
2017-02-18 03:29:59 -05:00
db.User.loadByUsernameOrEmail(req.body.username, req.body.email, function (err, user) {
if (err) {
logger.error('Error in usersAdd request validator.', { error: err })
return res.sendStatus(500)
}
if (user) return res.status(409).send('User already exists.')
next()
})
})
}
function usersRemove (req, res, next) {
2016-12-11 15:50:51 -05:00
req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
logger.debug('Checking usersRemove parameters', { parameters: req.params })
checkErrors(req, res, function () {
2016-12-11 15:50:51 -05:00
db.User.loadById(req.params.id, function (err, user) {
if (err) {
logger.error('Error in usersRemove request validator.', { error: err })
return res.sendStatus(500)
}
if (!user) return res.status(404).send('User not found')
2016-10-07 09:32:09 -04:00
if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
next()
})
})
}
function usersUpdate (req, res, next) {
2016-12-11 15:50:51 -05:00
req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
// Add old password verification
2017-04-03 15:24:36 -04:00
req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
logger.debug('Checking usersUpdate parameters', { parameters: req.body })
checkErrors(req, res, next)
}
2017-03-08 15:35:43 -05:00
function usersVideoRating (req, res, next) {
req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4)
logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
checkErrors(req, res, function () {
db.Video.load(req.params.videoId, function (err, video) {
if (err) {
logger.error('Error in user request validator.', { error: err })
return res.sendStatus(500)
}
if (!video) return res.status(404).send('Video not found')
next()
})
})
}
// ---------------------------------------------------------------------------
module.exports = validatorsUsers