]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/users.js
Server: add unique to unique indexes
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.js
CommitLineData
9bd26629
C
1'use strict'
2
9bd26629 3const checkErrors = require('./utils').checkErrors
feb4bdfd 4const db = require('../../initializers/database')
9bd26629
C
5const logger = require('../../helpers/logger')
6
9bd26629 7const validatorsUsers = {
c4403b29
C
8 usersAdd,
9 usersRemove,
10 usersUpdate
9bd26629
C
11}
12
13function 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
9bd26629
C
17 logger.debug('Checking usersAdd parameters', { parameters: req.body })
18
bf68dd75 19 checkErrors(req, res, function () {
feb4bdfd 20 db.User.loadByUsername(req.body.username, function (err, user) {
bf68dd75
C
21 if (err) {
22 logger.error('Error in usersAdd request validator.', { error: err })
23 return res.sendStatus(500)
24 }
25
26 if (user) return res.status(409).send('User already exists.')
27
28 next()
29 })
30 })
9bd26629
C
31}
32
33function usersRemove (req, res, next) {
feb4bdfd 34 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
9bd26629
C
35
36 logger.debug('Checking usersRemove parameters', { parameters: req.params })
37
38 checkErrors(req, res, function () {
feb4bdfd 39 db.User.loadById(req.params.id, function (err, user) {
9bd26629
C
40 if (err) {
41 logger.error('Error in usersRemove request validator.', { error: err })
42 return res.sendStatus(500)
43 }
44
45 if (!user) return res.status(404).send('User not found')
46
af1068ce
C
47 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
48
9bd26629
C
49 next()
50 })
51 })
52}
53
54function usersUpdate (req, res, next) {
feb4bdfd 55 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
9bd26629
C
56 // Add old password verification
57 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
58
59 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
60
61 checkErrors(req, res, next)
62}
63
64// ---------------------------------------------------------------------------
65
66module.exports = validatorsUsers