]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import * as Bluebird from 'bluebird' |
69818c93 | 2 | import * as express from 'express' |
a2431b7d C |
3 | import 'express-validator' |
4 | import { body, param } from 'express-validator/check' | |
ecb4e35f | 5 | import { omit } from 'lodash' |
3fd3ab2d | 6 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
b60e5f38 | 7 | import { |
ecb4e35f C |
8 | isAvatarFile, |
9 | isUserAutoPlayVideoValid, | |
ed56ad11 | 10 | isUserDescriptionValid, isUserDisplayNameValid, |
0883b324 | 11 | isUserNSFWPolicyValid, |
ecb4e35f C |
12 | isUserPasswordValid, |
13 | isUserRoleValid, | |
14 | isUserUsernameValid, | |
3fd3ab2d C |
15 | isUserVideoQuotaValid |
16 | } from '../../helpers/custom-validators/users' | |
47564bbe | 17 | import { isVideoExist } from '../../helpers/custom-validators/videos' |
da854ddd C |
18 | import { logger } from '../../helpers/logger' |
19 | import { isSignupAllowed } from '../../helpers/utils' | |
c5911fd3 | 20 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
ecb4e35f | 21 | import { Redis } from '../../lib/redis' |
3fd3ab2d | 22 | import { UserModel } from '../../models/account/user' |
a2431b7d | 23 | import { areValidationErrors } from './utils' |
9bd26629 | 24 | |
b60e5f38 | 25 | const usersAddValidator = [ |
563d032e | 26 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), |
b60e5f38 C |
27 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), |
28 | body('email').isEmail().withMessage('Should have a valid email'), | |
29 | body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), | |
954605a8 | 30 | body('role').custom(isUserRoleValid).withMessage('Should have a valid role'), |
9bd26629 | 31 | |
a2431b7d | 32 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 33 | logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') }) |
9bd26629 | 34 | |
a2431b7d C |
35 | if (areValidationErrors(req, res)) return |
36 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return | |
37 | ||
38 | return next() | |
b60e5f38 C |
39 | } |
40 | ] | |
6fcd19ba | 41 | |
b60e5f38 C |
42 | const usersRegisterValidator = [ |
43 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), | |
44 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), | |
45 | body('email').isEmail().withMessage('Should have a valid email'), | |
77a5501f | 46 | |
a2431b7d | 47 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 48 | logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) |
77a5501f | 49 | |
a2431b7d C |
50 | if (areValidationErrors(req, res)) return |
51 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return | |
52 | ||
53 | return next() | |
b60e5f38 C |
54 | } |
55 | ] | |
9bd26629 | 56 | |
b60e5f38 C |
57 | const usersRemoveValidator = [ |
58 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
9bd26629 | 59 | |
a2431b7d | 60 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 61 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) |
9bd26629 | 62 | |
a2431b7d C |
63 | if (areValidationErrors(req, res)) return |
64 | if (!await checkUserIdExist(req.params.id, res)) return | |
65 | ||
66 | const user = res.locals.user | |
67 | if (user.username === 'root') { | |
68 | return res.status(400) | |
69 | .send({ error: 'Cannot remove the root user' }) | |
70 | .end() | |
71 | } | |
72 | ||
73 | return next() | |
b60e5f38 C |
74 | } |
75 | ] | |
8094a898 | 76 | |
b60e5f38 C |
77 | const usersUpdateValidator = [ |
78 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
79 | body('email').optional().isEmail().withMessage('Should have a valid email attribute'), | |
80 | body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), | |
954605a8 | 81 | body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'), |
8094a898 | 82 | |
a2431b7d | 83 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 84 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) |
9bd26629 | 85 | |
a2431b7d C |
86 | if (areValidationErrors(req, res)) return |
87 | if (!await checkUserIdExist(req.params.id, res)) return | |
88 | ||
f8b8c36b C |
89 | const user = res.locals.user |
90 | if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) { | |
91 | return res.status(400) | |
92 | .send({ error: 'Cannot change root role.' }) | |
93 | .end() | |
94 | } | |
95 | ||
a2431b7d | 96 | return next() |
b60e5f38 C |
97 | } |
98 | ] | |
9bd26629 | 99 | |
b60e5f38 | 100 | const usersUpdateMeValidator = [ |
ed56ad11 | 101 | body('displayName').optional().custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), |
2422c46b | 102 | body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'), |
b60e5f38 C |
103 | body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), |
104 | body('email').optional().isEmail().withMessage('Should have a valid email attribute'), | |
0883b324 | 105 | body('nsfwPolicy').optional().custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'), |
7efe153b | 106 | body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), |
9bd26629 | 107 | |
b60e5f38 C |
108 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
109 | // TODO: Add old password verification | |
ce97fe36 | 110 | logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') }) |
8094a898 | 111 | |
a2431b7d C |
112 | if (areValidationErrors(req, res)) return |
113 | ||
114 | return next() | |
b60e5f38 C |
115 | } |
116 | ] | |
8094a898 | 117 | |
c5911fd3 C |
118 | const usersUpdateMyAvatarValidator = [ |
119 | body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage( | |
120 | 'This file is not supported. Please, make sure it is of the following type : ' | |
01de67b9 | 121 | + CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME.join(', ') |
c5911fd3 C |
122 | ), |
123 | ||
124 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
e8e12200 | 125 | logger.debug('Checking usersUpdateMyAvatarValidator parameters', { files: req.files }) |
c5911fd3 C |
126 | |
127 | if (areValidationErrors(req, res)) return | |
128 | ||
01de67b9 C |
129 | const imageFile = req.files['avatarfile'][0] as Express.Multer.File |
130 | if (imageFile.size > CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max) { | |
131 | res.status(400) | |
132 | .send({ error: `The size of the avatar is too big (>${CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max}).` }) | |
133 | .end() | |
134 | return | |
135 | } | |
136 | ||
c5911fd3 C |
137 | return next() |
138 | } | |
139 | ] | |
140 | ||
b60e5f38 C |
141 | const usersGetValidator = [ |
142 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
d38b8281 | 143 | |
a2431b7d | 144 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 145 | logger.debug('Checking usersGet parameters', { parameters: req.params }) |
a2431b7d C |
146 | |
147 | if (areValidationErrors(req, res)) return | |
148 | if (!await checkUserIdExist(req.params.id, res)) return | |
149 | ||
150 | return next() | |
b60e5f38 C |
151 | } |
152 | ] | |
d38b8281 | 153 | |
b60e5f38 | 154 | const usersVideoRatingValidator = [ |
72c7248b | 155 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), |
0a6658fd | 156 | |
a2431b7d | 157 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 158 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) |
0a6658fd | 159 | |
a2431b7d C |
160 | if (areValidationErrors(req, res)) return |
161 | if (!await isVideoExist(req.params.videoId, res)) return | |
162 | ||
163 | return next() | |
b60e5f38 C |
164 | } |
165 | ] | |
166 | ||
167 | const ensureUserRegistrationAllowed = [ | |
a2431b7d C |
168 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
169 | const allowed = await isSignupAllowed() | |
170 | if (allowed === false) { | |
171 | return res.status(403) | |
172 | .send({ error: 'User registration is not enabled or user limit is reached.' }) | |
173 | .end() | |
174 | } | |
175 | ||
176 | return next() | |
b60e5f38 C |
177 | } |
178 | ] | |
291e8d3e | 179 | |
ecb4e35f C |
180 | const usersAskResetPasswordValidator = [ |
181 | body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'), | |
182 | ||
183 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
184 | logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body }) | |
185 | ||
186 | if (areValidationErrors(req, res)) return | |
187 | const exists = await checkUserEmailExist(req.body.email, res, false) | |
188 | if (!exists) { | |
189 | logger.debug('User with email %s does not exist (asking reset password).', req.body.email) | |
190 | // Do not leak our emails | |
191 | return res.status(204).end() | |
192 | } | |
193 | ||
194 | return next() | |
195 | } | |
196 | ] | |
197 | ||
198 | const usersResetPasswordValidator = [ | |
199 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
200 | body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'), | |
201 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), | |
202 | ||
203 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
204 | logger.debug('Checking usersResetPassword parameters', { parameters: req.params }) | |
205 | ||
206 | if (areValidationErrors(req, res)) return | |
207 | if (!await checkUserIdExist(req.params.id, res)) return | |
208 | ||
209 | const user = res.locals.user as UserModel | |
210 | const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id) | |
211 | ||
212 | if (redisVerificationString !== req.body.verificationString) { | |
213 | return res | |
214 | .status(403) | |
215 | .send({ error: 'Invalid verification string.' }) | |
f076daa7 | 216 | .end() |
ecb4e35f C |
217 | } |
218 | ||
219 | return next() | |
220 | } | |
221 | ] | |
222 | ||
9bd26629 C |
223 | // --------------------------------------------------------------------------- |
224 | ||
65fcc311 C |
225 | export { |
226 | usersAddValidator, | |
77a5501f | 227 | usersRegisterValidator, |
65fcc311 C |
228 | usersRemoveValidator, |
229 | usersUpdateValidator, | |
8094a898 | 230 | usersUpdateMeValidator, |
291e8d3e | 231 | usersVideoRatingValidator, |
8094a898 | 232 | ensureUserRegistrationAllowed, |
c5911fd3 | 233 | usersGetValidator, |
ecb4e35f C |
234 | usersUpdateMyAvatarValidator, |
235 | usersAskResetPasswordValidator, | |
236 | usersResetPasswordValidator | |
8094a898 C |
237 | } |
238 | ||
239 | // --------------------------------------------------------------------------- | |
240 | ||
ecb4e35f C |
241 | function checkUserIdExist (id: number, res: express.Response) { |
242 | return checkUserExist(() => UserModel.loadById(id), res) | |
243 | } | |
a2431b7d | 244 | |
ecb4e35f C |
245 | function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) { |
246 | return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse) | |
65fcc311 | 247 | } |
77a5501f | 248 | |
a2431b7d | 249 | async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) { |
3fd3ab2d | 250 | const user = await UserModel.loadByUsernameOrEmail(username, email) |
a2431b7d C |
251 | |
252 | if (user) { | |
253 | res.status(409) | |
edf7f40a | 254 | .send({ error: 'User with this username or email already exists.' }) |
a2431b7d C |
255 | .end() |
256 | return false | |
257 | } | |
258 | ||
259 | return true | |
77a5501f | 260 | } |
ecb4e35f C |
261 | |
262 | async function checkUserExist (finder: () => Bluebird<UserModel>, res: express.Response, abortResponse = true) { | |
263 | const user = await finder() | |
264 | ||
265 | if (!user) { | |
266 | if (abortResponse === true) { | |
267 | res.status(404) | |
268 | .send({ error: 'User not found' }) | |
269 | .end() | |
270 | } | |
271 | ||
272 | return false | |
273 | } | |
274 | ||
275 | res.locals.user = user | |
276 | ||
277 | return true | |
278 | } |