]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/users.js
Server: make friends urls come from the request instead of the
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.js
CommitLineData
9bd26629
C
1'use strict'
2
3const mongoose = require('mongoose')
4
5const checkErrors = require('./utils').checkErrors
6const logger = require('../../helpers/logger')
7
8const User = mongoose.model('User')
9
10const validatorsUsers = {
11 usersAdd: usersAdd,
12 usersRemove: usersRemove,
13 usersUpdate: usersUpdate
14}
15
16function usersAdd (req, res, next) {
17 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
18 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
19
20 // TODO: check we don't have already the same username
21
22 logger.debug('Checking usersAdd parameters', { parameters: req.body })
23
24 checkErrors(req, res, next)
25}
26
27function usersRemove (req, res, next) {
68a3b9f2 28 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
9bd26629
C
29
30 logger.debug('Checking usersRemove parameters', { parameters: req.params })
31
32 checkErrors(req, res, function () {
68a3b9f2 33 User.loadById(req.params.id, function (err, user) {
9bd26629
C
34 if (err) {
35 logger.error('Error in usersRemove request validator.', { error: err })
36 return res.sendStatus(500)
37 }
38
39 if (!user) return res.status(404).send('User not found')
40
41 next()
42 })
43 })
44}
45
46function usersUpdate (req, res, next) {
68a3b9f2 47 req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId()
9bd26629
C
48 // Add old password verification
49 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
50
51 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
52
53 checkErrors(req, res, next)
54}
55
56// ---------------------------------------------------------------------------
57
58module.exports = validatorsUsers