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