]>
Commit | Line | Data |
---|---|---|
69818c93 C |
1 | import 'express-validator' |
2 | import * as express from 'express' | |
0a6658fd C |
3 | import * as Promise from 'bluebird' |
4 | import * as validator from 'validator' | |
69818c93 | 5 | |
e02643f3 | 6 | import { database as db } from '../../initializers/database' |
65fcc311 | 7 | import { checkErrors } from './utils' |
291e8d3e | 8 | import { isSignupAllowed, logger } from '../../helpers' |
0a6658fd | 9 | import { VideoInstance } from '../../models' |
9bd26629 | 10 | |
69818c93 | 11 | function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
9bd26629 C |
12 | req.checkBody('username', 'Should have a valid username').isUserUsernameValid() |
13 | req.checkBody('password', 'Should have a valid password').isUserPasswordValid() | |
ad4a8a1c | 14 | req.checkBody('email', 'Should have a valid email').isEmail() |
9bd26629 | 15 | |
9bd26629 C |
16 | logger.debug('Checking usersAdd parameters', { parameters: req.body }) |
17 | ||
075f16ca | 18 | checkErrors(req, res, () => { |
6fcd19ba C |
19 | db.User.loadByUsernameOrEmail(req.body.username, req.body.email) |
20 | .then(user => { | |
21 | if (user) return res.status(409).send('User already exists.') | |
22 | ||
23 | next() | |
24 | }) | |
25 | .catch(err => { | |
ad0997ad | 26 | logger.error('Error in usersAdd request validator.', err) |
bf68dd75 | 27 | return res.sendStatus(500) |
6fcd19ba | 28 | }) |
bf68dd75 | 29 | }) |
9bd26629 C |
30 | } |
31 | ||
69818c93 | 32 | function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
feb4bdfd | 33 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() |
9bd26629 C |
34 | |
35 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) | |
36 | ||
075f16ca | 37 | checkErrors(req, res, () => { |
6fcd19ba C |
38 | db.User.loadById(req.params.id) |
39 | .then(user => { | |
40 | if (!user) return res.status(404).send('User not found') | |
9bd26629 | 41 | |
6fcd19ba | 42 | if (user.username === 'root') return res.status(400).send('Cannot remove the root user') |
af1068ce | 43 | |
6fcd19ba C |
44 | next() |
45 | }) | |
46 | .catch(err => { | |
ad0997ad | 47 | logger.error('Error in usersRemove request validator.', err) |
6fcd19ba C |
48 | return res.sendStatus(500) |
49 | }) | |
9bd26629 C |
50 | }) |
51 | } | |
52 | ||
69818c93 | 53 | function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
feb4bdfd | 54 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() |
9bd26629 | 55 | // Add old password verification |
1d49e1e2 C |
56 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() |
57 | req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid() | |
9bd26629 C |
58 | |
59 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) | |
60 | ||
61 | checkErrors(req, res, next) | |
62 | } | |
63 | ||
69818c93 | 64 | function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
0a6658fd | 65 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid() |
d38b8281 C |
66 | |
67 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) | |
68 | ||
075f16ca | 69 | checkErrors(req, res, () => { |
0a6658fd C |
70 | let videoPromise: Promise<VideoInstance> |
71 | ||
72 | if (validator.isUUID(req.params.videoId)) { | |
73 | videoPromise = db.Video.loadByUUID(req.params.videoId) | |
74 | } else { | |
75 | videoPromise = db.Video.load(req.params.videoId) | |
76 | } | |
77 | ||
78 | videoPromise | |
6fcd19ba C |
79 | .then(video => { |
80 | if (!video) return res.status(404).send('Video not found') | |
81 | ||
82 | next() | |
83 | }) | |
84 | .catch(err => { | |
ad0997ad | 85 | logger.error('Error in user request validator.', err) |
d38b8281 | 86 | return res.sendStatus(500) |
6fcd19ba | 87 | }) |
d38b8281 C |
88 | }) |
89 | } | |
90 | ||
291e8d3e C |
91 | function ensureUserRegistrationAllowed (req: express.Request, res: express.Response, next: express.NextFunction) { |
92 | isSignupAllowed().then(allowed => { | |
93 | if (allowed === false) { | |
94 | return res.status(403).send('User registration is not enabled or user limit is reached.') | |
95 | } | |
96 | ||
97 | return next() | |
98 | }) | |
99 | } | |
100 | ||
9bd26629 C |
101 | // --------------------------------------------------------------------------- |
102 | ||
65fcc311 C |
103 | export { |
104 | usersAddValidator, | |
105 | usersRemoveValidator, | |
106 | usersUpdateValidator, | |
291e8d3e C |
107 | usersVideoRatingValidator, |
108 | ensureUserRegistrationAllowed | |
65fcc311 | 109 | } |