]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/users.ts
Add avatar max size limit
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param } from 'express-validator/check'
4 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
5 import {
6 isAvatarFile, isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
7 isUserVideoQuotaValid
8 } from '../../helpers/custom-validators/users'
9 import { isVideoExist } from '../../helpers/custom-validators/videos'
10 import { logger } from '../../helpers/logger'
11 import { isSignupAllowed } from '../../helpers/utils'
12 import { CONSTRAINTS_FIELDS } from '../../initializers'
13 import { UserModel } from '../../models/account/user'
14 import { areValidationErrors } from './utils'
15 import Multer = require('multer')
16
17 const usersAddValidator = [
18 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
19 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
20 body('email').isEmail().withMessage('Should have a valid email'),
21 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
22 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
23
24 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 logger.debug('Checking usersAdd parameters', { parameters: req.body })
26
27 if (areValidationErrors(req, res)) return
28 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
29
30 return next()
31 }
32 ]
33
34 const usersRegisterValidator = [
35 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
36 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
37 body('email').isEmail().withMessage('Should have a valid email'),
38
39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 logger.debug('Checking usersRegister parameters', { parameters: req.body })
41
42 if (areValidationErrors(req, res)) return
43 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
44
45 return next()
46 }
47 ]
48
49 const usersRemoveValidator = [
50 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
51
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking usersRemove parameters', { parameters: req.params })
54
55 if (areValidationErrors(req, res)) return
56 if (!await checkUserIdExist(req.params.id, res)) return
57
58 const user = res.locals.user
59 if (user.username === 'root') {
60 return res.status(400)
61 .send({ error: 'Cannot remove the root user' })
62 .end()
63 }
64
65 return next()
66 }
67 ]
68
69 const usersUpdateValidator = [
70 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
71 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
72 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
73 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
74
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
77
78 if (areValidationErrors(req, res)) return
79 if (!await checkUserIdExist(req.params.id, res)) return
80
81 return next()
82 }
83 ]
84
85 const usersUpdateMeValidator = [
86 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
87 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
88 body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
89 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
90
91 (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 // TODO: Add old password verification
93 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
94
95 if (areValidationErrors(req, res)) return
96
97 return next()
98 }
99 ]
100
101 const usersUpdateMyAvatarValidator = [
102 body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage(
103 'This file is not supported. Please, make sure it is of the following type : '
104 + CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME.join(', ')
105 ),
106
107 (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 logger.debug('Checking usersUpdateMyAvatarValidator parameters', { parameters: req.body })
109
110 if (areValidationErrors(req, res)) return
111
112 const imageFile = req.files['avatarfile'][0] as Express.Multer.File
113 if (imageFile.size > CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max) {
114 res.status(400)
115 .send({ error: `The size of the avatar is too big (>${CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max}).` })
116 .end()
117 return
118 }
119
120 return next()
121 }
122 ]
123
124 const usersGetValidator = [
125 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
126
127 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 logger.debug('Checking usersGet parameters', { parameters: req.body })
129
130 if (areValidationErrors(req, res)) return
131 if (!await checkUserIdExist(req.params.id, res)) return
132
133 return next()
134 }
135 ]
136
137 const usersVideoRatingValidator = [
138 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
139
140 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
141 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
142
143 if (areValidationErrors(req, res)) return
144 if (!await isVideoExist(req.params.videoId, res)) return
145
146 return next()
147 }
148 ]
149
150 const ensureUserRegistrationAllowed = [
151 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
152 const allowed = await isSignupAllowed()
153 if (allowed === false) {
154 return res.status(403)
155 .send({ error: 'User registration is not enabled or user limit is reached.' })
156 .end()
157 }
158
159 return next()
160 }
161 ]
162
163 // ---------------------------------------------------------------------------
164
165 export {
166 usersAddValidator,
167 usersRegisterValidator,
168 usersRemoveValidator,
169 usersUpdateValidator,
170 usersUpdateMeValidator,
171 usersVideoRatingValidator,
172 ensureUserRegistrationAllowed,
173 usersGetValidator,
174 usersUpdateMyAvatarValidator
175 }
176
177 // ---------------------------------------------------------------------------
178
179 async function checkUserIdExist (id: number, res: express.Response) {
180 const user = await UserModel.loadById(id)
181
182 if (!user) {
183 res.status(404)
184 .send({ error: 'User not found' })
185 .end()
186
187 return false
188 }
189
190 res.locals.user = user
191 return true
192 }
193
194 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
195 const user = await UserModel.loadByUsernameOrEmail(username, email)
196
197 if (user) {
198 res.status(409)
199 .send({ error: 'User with this username of email already exists.' })
200 .end()
201 return false
202 }
203
204 return true
205 }