diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
commit | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch) | |
tree | 03204a408d56311692c3528bedcf95d2455e94f2 /server/middlewares/validators/users.js | |
parent | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff) | |
parent | c4403b29ad4db097af528a7f04eea07e0ed320d0 (diff) | |
download | PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip |
Merge branch 'master' into webseed-merged
Diffstat (limited to 'server/middlewares/validators/users.js')
-rw-r--r-- | server/middlewares/validators/users.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/server/middlewares/validators/users.js b/server/middlewares/validators/users.js new file mode 100644 index 000000000..d541e9124 --- /dev/null +++ b/server/middlewares/validators/users.js | |||
@@ -0,0 +1,67 @@ | |||
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, | ||
12 | usersRemove, | ||
13 | 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 | logger.debug('Checking usersAdd parameters', { parameters: req.body }) | ||
21 | |||
22 | checkErrors(req, res, function () { | ||
23 | User.loadByUsername(req.body.username, function (err, user) { | ||
24 | if (err) { | ||
25 | logger.error('Error in usersAdd request validator.', { error: err }) | ||
26 | return res.sendStatus(500) | ||
27 | } | ||
28 | |||
29 | if (user) return res.status(409).send('User already exists.') | ||
30 | |||
31 | next() | ||
32 | }) | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | function usersRemove (req, res, next) { | ||
37 | req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() | ||
38 | |||
39 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) | ||
40 | |||
41 | checkErrors(req, res, function () { | ||
42 | User.loadById(req.params.id, function (err, user) { | ||
43 | if (err) { | ||
44 | logger.error('Error in usersRemove request validator.', { error: err }) | ||
45 | return res.sendStatus(500) | ||
46 | } | ||
47 | |||
48 | if (!user) return res.status(404).send('User not found') | ||
49 | |||
50 | next() | ||
51 | }) | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | function usersUpdate (req, res, next) { | ||
56 | req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() | ||
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 | ||