]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/users.ts
Begin to add avatar to actors
[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,
7 isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
8 isUserVideoQuotaValid
9 } from '../../helpers/custom-validators/users'
10 import { isVideoExist, isVideoFile } from '../../helpers/custom-validators/videos'
11 import { logger } from '../../helpers/logger'
12 import { isSignupAllowed } from '../../helpers/utils'
13 import { CONSTRAINTS_FIELDS } from '../../initializers'
14 import { UserModel } from '../../models/account/user'
15 import { areValidationErrors } from './utils'
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.ACTOR.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 return next()
113 }
114 ]
115
116 const usersGetValidator = [
117 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
118
119 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
120 logger.debug('Checking usersGet parameters', { parameters: req.body })
121
122 if (areValidationErrors(req, res)) return
123 if (!await checkUserIdExist(req.params.id, res)) return
124
125 return next()
126 }
127 ]
128
129 const usersVideoRatingValidator = [
130 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
131
132 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
133 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
134
135 if (areValidationErrors(req, res)) return
136 if (!await isVideoExist(req.params.videoId, res)) return
137
138 return next()
139 }
140 ]
141
142 const ensureUserRegistrationAllowed = [
143 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
144 const allowed = await isSignupAllowed()
145 if (allowed === false) {
146 return res.status(403)
147 .send({ error: 'User registration is not enabled or user limit is reached.' })
148 .end()
149 }
150
151 return next()
152 }
153 ]
154
155 // ---------------------------------------------------------------------------
156
157 export {
158 usersAddValidator,
159 usersRegisterValidator,
160 usersRemoveValidator,
161 usersUpdateValidator,
162 usersUpdateMeValidator,
163 usersVideoRatingValidator,
164 ensureUserRegistrationAllowed,
165 usersGetValidator,
166 usersUpdateMyAvatarValidator
167 }
168
169 // ---------------------------------------------------------------------------
170
171 async function checkUserIdExist (id: number, res: express.Response) {
172 const user = await UserModel.loadById(id)
173
174 if (!user) {
175 res.status(404)
176 .send({ error: 'User not found' })
177 .end()
178
179 return false
180 }
181
182 res.locals.user = user
183 return true
184 }
185
186 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
187 const user = await UserModel.loadByUsernameOrEmail(username, email)
188
189 if (user) {
190 res.status(409)
191 .send({ error: 'User with this username of email already exists.' })
192 .end()
193 return false
194 }
195
196 return true
197 }