diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-04 22:32:36 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-04 22:33:38 +0200 |
commit | 9bd2662976a75d3b03364cdbe6419e57c80f99a6 (patch) | |
tree | 0b5289660f843a8ba7f13aa79d458f53c94b36d9 /server/middlewares/validators/users.js | |
parent | e4c556196d7b31111f17596840d2e1d60caa7dcb (diff) | |
download | PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.tar.gz PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.tar.zst PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.zip |
Implement user API (create, update, remove, list)
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 | ||