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