diff options
Diffstat (limited to 'server/middlewares/validators/users.js')
-rw-r--r-- | server/middlewares/validators/users.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/server/middlewares/validators/users.js b/server/middlewares/validators/users.js new file mode 100644 index 000000000..175d90bcb --- /dev/null +++ b/server/middlewares/validators/users.js | |||
@@ -0,0 +1,57 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const mongoose = require('mongoose') | ||
4 | |||
5 | const checkErrors = require('./utils').checkErrors | ||
6 | const logger = require('../../helpers/logger') | ||
7 | |||
8 | const User = mongoose.model('User') | ||
9 | |||
10 | const validatorsUsers = { | ||
11 | usersAdd: usersAdd, | ||
12 | usersRemove: usersRemove, | ||
13 | usersUpdate: usersUpdate | ||
14 | } | ||
15 | |||
16 | function 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 | |||
27 | function usersRemove (req, res, next) { | ||
28 | req.checkParams('username', 'Should have a valid username').isUserUsernameValid() | ||
29 | |||
30 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) | ||
31 | |||
32 | checkErrors(req, res, function () { | ||
33 | User.loadByUsername(req.params.username, function (err, user) { | ||
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 | |||
46 | function usersUpdate (req, res, next) { | ||
47 | // Add old password verification | ||
48 | req.checkBody('password', 'Should have a valid password').isUserPasswordValid() | ||
49 | |||
50 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) | ||
51 | |||
52 | checkErrors(req, res, next) | ||
53 | } | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | module.exports = validatorsUsers | ||