diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/middlewares/validators/users.js | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/middlewares/validators/users.js')
-rw-r--r-- | server/middlewares/validators/users.js | 88 |
1 files changed, 0 insertions, 88 deletions
diff --git a/server/middlewares/validators/users.js b/server/middlewares/validators/users.js deleted file mode 100644 index 1e7a64793..000000000 --- a/server/middlewares/validators/users.js +++ /dev/null | |||
@@ -1,88 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const checkErrors = require('./utils').checkErrors | ||
4 | const db = require('../../initializers/database') | ||
5 | const logger = require('../../helpers/logger') | ||
6 | |||
7 | const validatorsUsers = { | ||
8 | usersAdd, | ||
9 | usersRemove, | ||
10 | usersUpdate, | ||
11 | usersVideoRating | ||
12 | } | ||
13 | |||
14 | function usersAdd (req, res, next) { | ||
15 | req.checkBody('username', 'Should have a valid username').isUserUsernameValid() | ||
16 | req.checkBody('password', 'Should have a valid password').isUserPasswordValid() | ||
17 | req.checkBody('email', 'Should have a valid email').isEmail() | ||
18 | |||
19 | logger.debug('Checking usersAdd parameters', { parameters: req.body }) | ||
20 | |||
21 | checkErrors(req, res, function () { | ||
22 | db.User.loadByUsernameOrEmail(req.body.username, req.body.email, function (err, user) { | ||
23 | if (err) { | ||
24 | logger.error('Error in usersAdd request validator.', { error: err }) | ||
25 | return res.sendStatus(500) | ||
26 | } | ||
27 | |||
28 | if (user) return res.status(409).send('User already exists.') | ||
29 | |||
30 | next() | ||
31 | }) | ||
32 | }) | ||
33 | } | ||
34 | |||
35 | function usersRemove (req, res, next) { | ||
36 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() | ||
37 | |||
38 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) | ||
39 | |||
40 | checkErrors(req, res, function () { | ||
41 | db.User.loadById(req.params.id, function (err, user) { | ||
42 | if (err) { | ||
43 | logger.error('Error in usersRemove request validator.', { error: err }) | ||
44 | return res.sendStatus(500) | ||
45 | } | ||
46 | |||
47 | if (!user) return res.status(404).send('User not found') | ||
48 | |||
49 | if (user.username === 'root') return res.status(400).send('Cannot remove the root user') | ||
50 | |||
51 | next() | ||
52 | }) | ||
53 | }) | ||
54 | } | ||
55 | |||
56 | function usersUpdate (req, res, next) { | ||
57 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() | ||
58 | // Add old password verification | ||
59 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() | ||
60 | req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid() | ||
61 | |||
62 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) | ||
63 | |||
64 | checkErrors(req, res, next) | ||
65 | } | ||
66 | |||
67 | function usersVideoRating (req, res, next) { | ||
68 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) | ||
69 | |||
70 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) | ||
71 | |||
72 | checkErrors(req, res, function () { | ||
73 | db.Video.load(req.params.videoId, function (err, video) { | ||
74 | if (err) { | ||
75 | logger.error('Error in user request validator.', { error: err }) | ||
76 | return res.sendStatus(500) | ||
77 | } | ||
78 | |||
79 | if (!video) return res.status(404).send('Video not found') | ||
80 | |||
81 | next() | ||
82 | }) | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | // --------------------------------------------------------------------------- | ||
87 | |||
88 | module.exports = validatorsUsers | ||