]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/users.ts
Remove ng2 file upload module
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
1 import 'express-validator'
2 import * as express from 'express'
3 import * as Promise from 'bluebird'
4 import * as validator from 'validator'
5
6 import { database as db } from '../../initializers/database'
7 import { checkErrors } from './utils'
8 import { isSignupAllowed, logger } from '../../helpers'
9 import { UserInstance, VideoInstance } from '../../models'
10
11 function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
12 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
13 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
14 req.checkBody('email', 'Should have a valid email').isEmail()
15 req.checkBody('videoQuota', 'Should have a valid user quota').isUserVideoQuotaValid()
16
17 logger.debug('Checking usersAdd parameters', { parameters: req.body })
18
19 checkErrors(req, res, () => {
20 checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
21 })
22 }
23
24 function usersRegisterValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
25 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
26 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
27 req.checkBody('email', 'Should have a valid email').isEmail()
28
29 logger.debug('Checking usersRegister parameters', { parameters: req.body })
30
31 checkErrors(req, res, () => {
32 checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
33 })
34 }
35
36 function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
37 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
38
39 logger.debug('Checking usersRemove parameters', { parameters: req.params })
40
41 checkErrors(req, res, () => {
42 checkUserExists(req.params.id, res, (err, user) => {
43 if (err) {
44 logger.error('Error in usersRemoveValidator.', err)
45 return res.sendStatus(500)
46 }
47
48 if (user.username === 'root') {
49 return res.status(400)
50 .send({ error: 'Cannot remove the root user' })
51 .end()
52 }
53
54 return next()
55 })
56 })
57 }
58
59 function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
60 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
61 req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
62 req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid()
63
64 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
65
66 checkErrors(req, res, () => {
67 checkUserExists(req.params.id, res, next)
68 })
69 }
70
71 function usersUpdateMeValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
72 // Add old password verification
73 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
74 req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
75 req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
76
77 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
78
79 checkErrors(req, res, next)
80 }
81
82 function usersGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
83 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
84
85 checkErrors(req, res, () => {
86 checkUserExists(req.params.id, res, next)
87 })
88 }
89
90 function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
91 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid()
92
93 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
94
95 checkErrors(req, res, () => {
96 let videoPromise: Promise<VideoInstance>
97
98 if (validator.isUUID(req.params.videoId)) {
99 videoPromise = db.Video.loadByUUID(req.params.videoId)
100 } else {
101 videoPromise = db.Video.load(req.params.videoId)
102 }
103
104 videoPromise
105 .then(video => {
106 if (!video) {
107 return res.status(404)
108 .json({ error: 'Video not found' })
109 .end()
110 }
111
112 return next()
113 })
114 .catch(err => {
115 logger.error('Error in user request validator.', err)
116 return res.sendStatus(500)
117 })
118 })
119 }
120
121 function ensureUserRegistrationAllowed (req: express.Request, res: express.Response, next: express.NextFunction) {
122 isSignupAllowed().then(allowed => {
123 if (allowed === false) {
124 return res.status(403)
125 .send({ error: 'User registration is not enabled or user limit is reached.' })
126 .end()
127 }
128
129 return next()
130 })
131 }
132
133 // ---------------------------------------------------------------------------
134
135 export {
136 usersAddValidator,
137 usersRegisterValidator,
138 usersRemoveValidator,
139 usersUpdateValidator,
140 usersUpdateMeValidator,
141 usersVideoRatingValidator,
142 ensureUserRegistrationAllowed,
143 usersGetValidator
144 }
145
146 // ---------------------------------------------------------------------------
147
148 function checkUserExists (id: number, res: express.Response, callback: (err: Error, user: UserInstance) => void) {
149 db.User.loadById(id)
150 .then(user => {
151 if (!user) {
152 return res.status(404)
153 .send({ error: 'User not found' })
154 .end()
155 }
156
157 res.locals.user = user
158 return callback(null, user)
159 })
160 .catch(err => {
161 logger.error('Error in user request validator.', err)
162 return res.sendStatus(500)
163 })
164 }
165
166 function checkUserDoesNotAlreadyExist (username: string, email: string, res: express.Response, callback: () => void) {
167 db.User.loadByUsernameOrEmail(username, email)
168 .then(user => {
169 if (user) {
170 return res.status(409)
171 .send({ error: 'User already exists.' })
172 .end()
173 }
174
175 return callback()
176 })
177 .catch(err => {
178 logger.error('Error in usersAdd request validator.', err)
179 return res.sendStatus(500)
180 })
181 }