diff options
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r-- | server/middlewares/validators/index.js | 2 | ||||
-rw-r--r-- | server/middlewares/validators/users.js | 57 | ||||
-rw-r--r-- | server/middlewares/validators/videos.js | 1 |
3 files changed, 60 insertions, 0 deletions
diff --git a/server/middlewares/validators/index.js b/server/middlewares/validators/index.js index 0471b3f92..6c3a9c2b4 100644 --- a/server/middlewares/validators/index.js +++ b/server/middlewares/validators/index.js | |||
@@ -4,6 +4,7 @@ const paginationValidators = require('./pagination') | |||
4 | const podsValidators = require('./pods') | 4 | const podsValidators = require('./pods') |
5 | const remoteValidators = require('./remote') | 5 | const remoteValidators = require('./remote') |
6 | const sortValidators = require('./sort') | 6 | const sortValidators = require('./sort') |
7 | const usersValidators = require('./users') | ||
7 | const videosValidators = require('./videos') | 8 | const videosValidators = require('./videos') |
8 | 9 | ||
9 | const validators = { | 10 | const validators = { |
@@ -11,6 +12,7 @@ const validators = { | |||
11 | pods: podsValidators, | 12 | pods: podsValidators, |
12 | remote: remoteValidators, | 13 | remote: remoteValidators, |
13 | sort: sortValidators, | 14 | sort: sortValidators, |
15 | users: usersValidators, | ||
14 | videos: videosValidators | 16 | videos: videosValidators |
15 | } | 17 | } |
16 | 18 | ||
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 | ||
diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js index 422f3642f..9d21ee16f 100644 --- a/server/middlewares/validators/videos.js +++ b/server/middlewares/validators/videos.js | |||
@@ -18,6 +18,7 @@ const validatorsVideos = { | |||
18 | 18 | ||
19 | function videosAdd (req, res, next) { | 19 | function videosAdd (req, res, next) { |
20 | req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() | 20 | req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() |
21 | // TODO: move to constants and function | ||
21 | req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) | 22 | req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) |
22 | req.checkBody('name', 'Should have a valid name').isVideoNameValid() | 23 | req.checkBody('name', 'Should have a valid name').isVideoNameValid() |
23 | req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid() | 24 | req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid() |