]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
76314386 | 2 | import { body, param, query } from 'express-validator' |
ecb4e35f | 3 | import { omit } from 'lodash' |
b49f22d8 C |
4 | import { Hooks } from '@server/lib/plugins/hooks' |
5 | import { MUserDefault } from '@server/types/models' | |
c0e8b12e | 6 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
b49f22d8 C |
7 | import { UserRole } from '../../../shared/models/users' |
8 | import { UserRegister } from '../../../shared/models/users/user-register.model' | |
d4a8e7a6 | 9 | import { toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' |
b49f22d8 | 10 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' |
b60e5f38 | 11 | import { |
1eddc9a7 | 12 | isUserAdminFlagsValid, |
c1e5bd23 | 13 | isUserAutoPlayNextVideoValid, |
1a12adcd C |
14 | isUserAutoPlayVideoValid, |
15 | isUserBlockedReasonValid, | |
4bbfc6c6 C |
16 | isUserDescriptionValid, |
17 | isUserDisplayNameValid, | |
8f581725 | 18 | isUserNoModal, |
0883b324 | 19 | isUserNSFWPolicyValid, |
ecb4e35f | 20 | isUserPasswordValid, |
45f1bd72 | 21 | isUserPasswordValidOrEmpty, |
ecb4e35f | 22 | isUserRoleValid, |
3e753302 C |
23 | isUserUsernameValid, |
24 | isUserVideoLanguages, | |
1a12adcd | 25 | isUserVideoQuotaDailyValid, |
dae86118 C |
26 | isUserVideoQuotaValid, |
27 | isUserVideosHistoryEnabledValid | |
3fd3ab2d | 28 | } from '../../helpers/custom-validators/users' |
27db7840 | 29 | import { isVideoChannelDisplayNameValid, isVideoChannelUsernameValid } from '../../helpers/custom-validators/video-channels' |
da854ddd | 30 | import { logger } from '../../helpers/logger' |
b49f22d8 | 31 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' |
ecb4e35f | 32 | import { Redis } from '../../lib/redis' |
10363c74 | 33 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../lib/signup' |
7d9ba5c0 | 34 | import { ActorModel } from '../../models/actor/actor' |
10363c74 | 35 | import { UserModel } from '../../models/user/user' |
d4a8e7a6 | 36 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared' |
9bd26629 | 37 | |
8491293b RK |
38 | const usersListValidator = [ |
39 | query('blocked') | |
40 | .optional() | |
f1273314 | 41 | .customSanitizer(toBooleanOrNull) |
8491293b RK |
42 | .isBoolean().withMessage('Should be a valid boolean banned state'), |
43 | ||
ea7337cf | 44 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
8491293b RK |
45 | logger.debug('Checking usersList parameters', { parameters: req.query }) |
46 | ||
47 | if (areValidationErrors(req, res)) return | |
48 | ||
49 | return next() | |
50 | } | |
51 | ] | |
52 | ||
b60e5f38 | 53 | const usersAddValidator = [ |
563d032e | 54 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), |
45f1bd72 | 55 | body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'), |
b60e5f38 | 56 | body('email').isEmail().withMessage('Should have a valid email'), |
27db7840 C |
57 | |
58 | body('channelName').optional().custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'), | |
59 | ||
b60e5f38 | 60 | body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), |
bee0abff | 61 | body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'), |
27db7840 | 62 | |
dea16773 C |
63 | body('role') |
64 | .customSanitizer(toIntOrNull) | |
65 | .custom(isUserRoleValid).withMessage('Should have a valid role'), | |
1eddc9a7 | 66 | body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'), |
9bd26629 | 67 | |
a2431b7d | 68 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 69 | logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') }) |
9bd26629 | 70 | |
a2431b7d C |
71 | if (areValidationErrors(req, res)) return |
72 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return | |
73 | ||
a95a4cc8 C |
74 | const authUser = res.locals.oauth.token.User |
75 | if (authUser.role !== UserRole.ADMINISTRATOR && req.body.role !== UserRole.USER) { | |
76148b27 RK |
76 | return res.fail({ |
77 | status: HttpStatusCode.FORBIDDEN_403, | |
78 | message: 'You can only create users (and not administrators or moderators)' | |
79 | }) | |
a95a4cc8 C |
80 | } |
81 | ||
766d13b4 | 82 | if (req.body.channelName) { |
83 | if (req.body.channelName === req.body.username) { | |
76148b27 | 84 | return res.fail({ message: 'Channel name cannot be the same as user username.' }) |
766d13b4 | 85 | } |
4e68fc86 | 86 | |
766d13b4 | 87 | const existing = await ActorModel.loadLocalByName(req.body.channelName) |
88 | if (existing) { | |
76148b27 RK |
89 | return res.fail({ |
90 | status: HttpStatusCode.CONFLICT_409, | |
91 | message: `Channel with name ${req.body.channelName} already exists.` | |
92 | }) | |
766d13b4 | 93 | } |
4e68fc86 | 94 | } |
95 | ||
a2431b7d | 96 | return next() |
b60e5f38 C |
97 | } |
98 | ] | |
6fcd19ba | 99 | |
b60e5f38 C |
100 | const usersRegisterValidator = [ |
101 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), | |
102 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), | |
103 | body('email').isEmail().withMessage('Should have a valid email'), | |
1f20622f C |
104 | body('displayName') |
105 | .optional() | |
106 | .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), | |
107 | ||
108 | body('channel.name') | |
109 | .optional() | |
27db7840 | 110 | .custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'), |
1f20622f C |
111 | body('channel.displayName') |
112 | .optional() | |
27db7840 | 113 | .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'), |
77a5501f | 114 | |
a2431b7d | 115 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 116 | logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) |
77a5501f | 117 | |
a2431b7d C |
118 | if (areValidationErrors(req, res)) return |
119 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return | |
120 | ||
e590b4a5 C |
121 | const body: UserRegister = req.body |
122 | if (body.channel) { | |
123 | if (!body.channel.name || !body.channel.displayName) { | |
76148b27 | 124 | return res.fail({ message: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' }) |
e590b4a5 C |
125 | } |
126 | ||
1d5342ab | 127 | if (body.channel.name === body.username) { |
76148b27 | 128 | return res.fail({ message: 'Channel name cannot be the same as user username.' }) |
1d5342ab C |
129 | } |
130 | ||
e590b4a5 C |
131 | const existing = await ActorModel.loadLocalByName(body.channel.name) |
132 | if (existing) { | |
76148b27 RK |
133 | return res.fail({ |
134 | status: HttpStatusCode.CONFLICT_409, | |
135 | message: `Channel with name ${body.channel.name} already exists.` | |
136 | }) | |
e590b4a5 C |
137 | } |
138 | } | |
139 | ||
a2431b7d | 140 | return next() |
b60e5f38 C |
141 | } |
142 | ] | |
9bd26629 | 143 | |
b60e5f38 C |
144 | const usersRemoveValidator = [ |
145 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
9bd26629 | 146 | |
a2431b7d | 147 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 148 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) |
9bd26629 | 149 | |
a2431b7d C |
150 | if (areValidationErrors(req, res)) return |
151 | if (!await checkUserIdExist(req.params.id, res)) return | |
152 | ||
153 | const user = res.locals.user | |
154 | if (user.username === 'root') { | |
76148b27 | 155 | return res.fail({ message: 'Cannot remove the root user' }) |
a2431b7d C |
156 | } |
157 | ||
158 | return next() | |
b60e5f38 C |
159 | } |
160 | ] | |
8094a898 | 161 | |
e6921918 C |
162 | const usersBlockingValidator = [ |
163 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
eacb25c4 | 164 | body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'), |
e6921918 C |
165 | |
166 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
eacb25c4 | 167 | logger.debug('Checking usersBlocking parameters', { parameters: req.params }) |
e6921918 C |
168 | |
169 | if (areValidationErrors(req, res)) return | |
170 | if (!await checkUserIdExist(req.params.id, res)) return | |
171 | ||
172 | const user = res.locals.user | |
173 | if (user.username === 'root') { | |
76148b27 | 174 | return res.fail({ message: 'Cannot block the root user' }) |
e6921918 C |
175 | } |
176 | ||
177 | return next() | |
178 | } | |
179 | ] | |
180 | ||
92b9d60c | 181 | const deleteMeValidator = [ |
a1587156 | 182 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
dae86118 | 183 | const user = res.locals.oauth.token.User |
92b9d60c | 184 | if (user.username === 'root') { |
76148b27 | 185 | return res.fail({ message: 'You cannot delete your root account.' }) |
92b9d60c C |
186 | } |
187 | ||
188 | return next() | |
189 | } | |
190 | ] | |
191 | ||
b60e5f38 C |
192 | const usersUpdateValidator = [ |
193 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
b8375da9 | 194 | |
b426edd4 | 195 | body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), |
b60e5f38 | 196 | body('email').optional().isEmail().withMessage('Should have a valid email attribute'), |
fc2ec87a | 197 | body('emailVerified').optional().isBoolean().withMessage('Should have a valid email verified attribute'), |
b60e5f38 | 198 | body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), |
bee0abff | 199 | body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'), |
6d989edc | 200 | body('pluginAuth').optional(), |
dea16773 C |
201 | body('role') |
202 | .optional() | |
203 | .customSanitizer(toIntOrNull) | |
204 | .custom(isUserRoleValid).withMessage('Should have a valid role'), | |
1eddc9a7 | 205 | body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'), |
8094a898 | 206 | |
a2431b7d | 207 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 208 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) |
9bd26629 | 209 | |
a2431b7d C |
210 | if (areValidationErrors(req, res)) return |
211 | if (!await checkUserIdExist(req.params.id, res)) return | |
212 | ||
f8b8c36b C |
213 | const user = res.locals.user |
214 | if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) { | |
76148b27 | 215 | return res.fail({ message: 'Cannot change root role.' }) |
f8b8c36b C |
216 | } |
217 | ||
a2431b7d | 218 | return next() |
b60e5f38 C |
219 | } |
220 | ] | |
9bd26629 | 221 | |
b60e5f38 | 222 | const usersUpdateMeValidator = [ |
d1ab89de C |
223 | body('displayName') |
224 | .optional() | |
225 | .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), | |
226 | body('description') | |
227 | .optional() | |
228 | .custom(isUserDescriptionValid).withMessage('Should have a valid description'), | |
229 | body('currentPassword') | |
230 | .optional() | |
231 | .custom(isUserPasswordValid).withMessage('Should have a valid current password'), | |
232 | body('password') | |
233 | .optional() | |
234 | .custom(isUserPasswordValid).withMessage('Should have a valid password'), | |
235 | body('email') | |
236 | .optional() | |
237 | .isEmail().withMessage('Should have a valid email attribute'), | |
238 | body('nsfwPolicy') | |
239 | .optional() | |
240 | .custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'), | |
241 | body('autoPlayVideo') | |
242 | .optional() | |
243 | .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), | |
3caf77d3 C |
244 | body('videoLanguages') |
245 | .optional() | |
246 | .custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'), | |
1a12adcd C |
247 | body('videosHistoryEnabled') |
248 | .optional() | |
249 | .custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'), | |
7cd4d2ba C |
250 | body('theme') |
251 | .optional() | |
503c6f44 | 252 | .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'), |
8f581725 | 253 | |
43d0ea7f C |
254 | body('noInstanceConfigWarningModal') |
255 | .optional() | |
8f581725 | 256 | .custom(v => isUserNoModal(v)).withMessage('Should have a valid noInstanceConfigWarningModal boolean'), |
43d0ea7f C |
257 | body('noWelcomeModal') |
258 | .optional() | |
8f581725 C |
259 | .custom(v => isUserNoModal(v)).withMessage('Should have a valid noWelcomeModal boolean'), |
260 | body('noAccountSetupWarningModal') | |
261 | .optional() | |
262 | .custom(v => isUserNoModal(v)).withMessage('Should have a valid noAccountSetupWarningModal boolean'), | |
263 | ||
c1e5bd23 C |
264 | body('autoPlayNextVideo') |
265 | .optional() | |
266 | .custom(v => isUserAutoPlayNextVideoValid(v)).withMessage('Should have a valid autoPlayNextVideo boolean'), | |
9bd26629 | 267 | |
a890d1e0 | 268 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 269 | logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') }) |
8094a898 | 270 | |
9a7fd960 C |
271 | const user = res.locals.oauth.token.User |
272 | ||
0ba5f5ba | 273 | if (req.body.password || req.body.email) { |
9a7fd960 | 274 | if (user.pluginAuth !== null) { |
76148b27 | 275 | return res.fail({ message: 'You cannot update your email or password that is associated with an external auth system.' }) |
9a7fd960 C |
276 | } |
277 | ||
a890d1e0 | 278 | if (!req.body.currentPassword) { |
76148b27 | 279 | return res.fail({ message: 'currentPassword parameter is missing.' }) |
a890d1e0 C |
280 | } |
281 | ||
a890d1e0 | 282 | if (await user.isPasswordMatch(req.body.currentPassword) !== true) { |
76148b27 RK |
283 | return res.fail({ |
284 | status: HttpStatusCode.UNAUTHORIZED_401, | |
285 | message: 'currentPassword is invalid.' | |
286 | }) | |
a890d1e0 C |
287 | } |
288 | } | |
289 | ||
a2431b7d C |
290 | if (areValidationErrors(req, res)) return |
291 | ||
292 | return next() | |
b60e5f38 C |
293 | } |
294 | ] | |
8094a898 | 295 | |
b60e5f38 C |
296 | const usersGetValidator = [ |
297 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
76314386 | 298 | query('withStats').optional().isBoolean().withMessage('Should have a valid stats flag'), |
d38b8281 | 299 | |
a2431b7d | 300 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 301 | logger.debug('Checking usersGet parameters', { parameters: req.params }) |
a2431b7d C |
302 | |
303 | if (areValidationErrors(req, res)) return | |
76314386 | 304 | if (!await checkUserIdExist(req.params.id, res, req.query.withStats)) return |
a2431b7d C |
305 | |
306 | return next() | |
b60e5f38 C |
307 | } |
308 | ] | |
d38b8281 | 309 | |
b60e5f38 | 310 | const usersVideoRatingValidator = [ |
d4a8e7a6 | 311 | isValidVideoIdParam('videoId'), |
0a6658fd | 312 | |
a2431b7d | 313 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 314 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) |
0a6658fd | 315 | |
a2431b7d | 316 | if (areValidationErrors(req, res)) return |
0f6acda1 | 317 | if (!await doesVideoExist(req.params.videoId, res, 'id')) return |
a2431b7d C |
318 | |
319 | return next() | |
b60e5f38 C |
320 | } |
321 | ] | |
322 | ||
323 | const ensureUserRegistrationAllowed = [ | |
a2431b7d | 324 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
4ce7eb71 | 325 | const allowedParams = { |
ba7b7e57 RK |
326 | body: req.body, |
327 | ip: req.ip | |
4ce7eb71 C |
328 | } |
329 | ||
330 | const allowedResult = await Hooks.wrapPromiseFun( | |
331 | isSignupAllowed, | |
332 | allowedParams, | |
333 | 'filter:api.user.signup.allowed.result' | |
334 | ) | |
335 | ||
336 | if (allowedResult.allowed === false) { | |
76148b27 RK |
337 | return res.fail({ |
338 | status: HttpStatusCode.FORBIDDEN_403, | |
339 | message: allowedResult.errorMessage || 'User registration is not enabled or user limit is reached.' | |
340 | }) | |
a2431b7d C |
341 | } |
342 | ||
343 | return next() | |
b60e5f38 C |
344 | } |
345 | ] | |
291e8d3e | 346 | |
ff2c1fe8 | 347 | const ensureUserRegistrationAllowedForIP = [ |
a1587156 | 348 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ff2c1fe8 RK |
349 | const allowed = isSignupAllowedForCurrentIP(req.ip) |
350 | ||
351 | if (allowed === false) { | |
76148b27 RK |
352 | return res.fail({ |
353 | status: HttpStatusCode.FORBIDDEN_403, | |
354 | message: 'You are not on a network authorized for registration.' | |
355 | }) | |
ff2c1fe8 RK |
356 | } |
357 | ||
358 | return next() | |
359 | } | |
360 | ] | |
361 | ||
ecb4e35f C |
362 | const usersAskResetPasswordValidator = [ |
363 | body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'), | |
364 | ||
365 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
366 | logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body }) | |
367 | ||
368 | if (areValidationErrors(req, res)) return | |
b426edd4 | 369 | |
ecb4e35f C |
370 | const exists = await checkUserEmailExist(req.body.email, res, false) |
371 | if (!exists) { | |
372 | logger.debug('User with email %s does not exist (asking reset password).', req.body.email) | |
373 | // Do not leak our emails | |
2d53be02 | 374 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
ecb4e35f C |
375 | } |
376 | ||
377 | return next() | |
378 | } | |
379 | ] | |
380 | ||
381 | const usersResetPasswordValidator = [ | |
382 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
383 | body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'), | |
384 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), | |
385 | ||
386 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
387 | logger.debug('Checking usersResetPassword parameters', { parameters: req.params }) | |
388 | ||
389 | if (areValidationErrors(req, res)) return | |
390 | if (!await checkUserIdExist(req.params.id, res)) return | |
391 | ||
dae86118 | 392 | const user = res.locals.user |
ecb4e35f C |
393 | const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id) |
394 | ||
395 | if (redisVerificationString !== req.body.verificationString) { | |
76148b27 RK |
396 | return res.fail({ |
397 | status: HttpStatusCode.FORBIDDEN_403, | |
398 | message: 'Invalid verification string.' | |
399 | }) | |
ecb4e35f C |
400 | } |
401 | ||
402 | return next() | |
403 | } | |
404 | ] | |
405 | ||
d9eaee39 JM |
406 | const usersAskSendVerifyEmailValidator = [ |
407 | body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'), | |
408 | ||
409 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
410 | logger.debug('Checking askUsersSendVerifyEmail parameters', { parameters: req.body }) | |
411 | ||
412 | if (areValidationErrors(req, res)) return | |
413 | const exists = await checkUserEmailExist(req.body.email, res, false) | |
414 | if (!exists) { | |
415 | logger.debug('User with email %s does not exist (asking verify email).', req.body.email) | |
416 | // Do not leak our emails | |
2d53be02 | 417 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
d9eaee39 JM |
418 | } |
419 | ||
420 | return next() | |
421 | } | |
422 | ] | |
423 | ||
424 | const usersVerifyEmailValidator = [ | |
d1ab89de C |
425 | param('id') |
426 | .isInt().not().isEmpty().withMessage('Should have a valid id'), | |
427 | ||
428 | body('verificationString') | |
429 | .not().isEmpty().withMessage('Should have a valid verification string'), | |
430 | body('isPendingEmail') | |
431 | .optional() | |
2b65c4e5 | 432 | .customSanitizer(toBooleanOrNull), |
d9eaee39 JM |
433 | |
434 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
435 | logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params }) | |
436 | ||
437 | if (areValidationErrors(req, res)) return | |
438 | if (!await checkUserIdExist(req.params.id, res)) return | |
439 | ||
dae86118 | 440 | const user = res.locals.user |
d9eaee39 JM |
441 | const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id) |
442 | ||
443 | if (redisVerificationString !== req.body.verificationString) { | |
76148b27 RK |
444 | return res.fail({ |
445 | status: HttpStatusCode.FORBIDDEN_403, | |
446 | message: 'Invalid verification string.' | |
447 | }) | |
d9eaee39 JM |
448 | } |
449 | ||
450 | return next() | |
451 | } | |
452 | ] | |
453 | ||
74d63469 GR |
454 | const userAutocompleteValidator = [ |
455 | param('search').isString().not().isEmpty().withMessage('Should have a search parameter') | |
456 | ] | |
457 | ||
c100a614 | 458 | const ensureAuthUserOwnsAccountValidator = [ |
a1587156 | 459 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
c100a614 YB |
460 | const user = res.locals.oauth.token.User |
461 | ||
462 | if (res.locals.account.id !== user.Account.id) { | |
76148b27 RK |
463 | return res.fail({ |
464 | status: HttpStatusCode.FORBIDDEN_403, | |
465 | message: 'Only owner can access ratings list.' | |
466 | }) | |
c100a614 YB |
467 | } |
468 | ||
469 | return next() | |
470 | } | |
471 | ] | |
472 | ||
a95a4cc8 C |
473 | const ensureCanManageUser = [ |
474 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
475 | const authUser = res.locals.oauth.token.User | |
476 | const onUser = res.locals.user | |
477 | ||
478 | if (authUser.role === UserRole.ADMINISTRATOR) return next() | |
479 | if (authUser.role === UserRole.MODERATOR && onUser.role === UserRole.USER) return next() | |
480 | ||
76148b27 RK |
481 | return res.fail({ |
482 | status: HttpStatusCode.FORBIDDEN_403, | |
483 | message: 'A moderator can only manager users.' | |
484 | }) | |
a95a4cc8 C |
485 | } |
486 | ] | |
487 | ||
9bd26629 C |
488 | // --------------------------------------------------------------------------- |
489 | ||
65fcc311 | 490 | export { |
8491293b | 491 | usersListValidator, |
65fcc311 | 492 | usersAddValidator, |
92b9d60c | 493 | deleteMeValidator, |
77a5501f | 494 | usersRegisterValidator, |
e6921918 | 495 | usersBlockingValidator, |
65fcc311 C |
496 | usersRemoveValidator, |
497 | usersUpdateValidator, | |
8094a898 | 498 | usersUpdateMeValidator, |
291e8d3e | 499 | usersVideoRatingValidator, |
8094a898 | 500 | ensureUserRegistrationAllowed, |
ff2c1fe8 | 501 | ensureUserRegistrationAllowedForIP, |
c5911fd3 | 502 | usersGetValidator, |
ecb4e35f | 503 | usersAskResetPasswordValidator, |
d9eaee39 JM |
504 | usersResetPasswordValidator, |
505 | usersAskSendVerifyEmailValidator, | |
74d63469 | 506 | usersVerifyEmailValidator, |
c100a614 | 507 | userAutocompleteValidator, |
a95a4cc8 C |
508 | ensureAuthUserOwnsAccountValidator, |
509 | ensureCanManageUser | |
8094a898 C |
510 | } |
511 | ||
512 | // --------------------------------------------------------------------------- | |
513 | ||
76314386 | 514 | function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) { |
d5d9b6d7 | 515 | const id = parseInt(idArg + '', 10) |
fb719404 | 516 | return checkUserExist(() => UserModel.loadByIdWithChannels(id, withStats), res) |
ecb4e35f | 517 | } |
a2431b7d | 518 | |
ecb4e35f C |
519 | function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) { |
520 | return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse) | |
65fcc311 | 521 | } |
77a5501f | 522 | |
a2431b7d | 523 | async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) { |
3fd3ab2d | 524 | const user = await UserModel.loadByUsernameOrEmail(username, email) |
a2431b7d C |
525 | |
526 | if (user) { | |
76148b27 RK |
527 | res.fail({ |
528 | status: HttpStatusCode.CONFLICT_409, | |
529 | message: 'User with this username or email already exists.' | |
530 | }) | |
a2431b7d C |
531 | return false |
532 | } | |
533 | ||
2ef6a063 C |
534 | const actor = await ActorModel.loadLocalByName(username) |
535 | if (actor) { | |
76148b27 RK |
536 | res.fail({ |
537 | status: HttpStatusCode.CONFLICT_409, | |
538 | message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' | |
539 | }) | |
2ef6a063 C |
540 | return false |
541 | } | |
542 | ||
a2431b7d | 543 | return true |
77a5501f | 544 | } |
ecb4e35f | 545 | |
b49f22d8 | 546 | async function checkUserExist (finder: () => Promise<MUserDefault>, res: express.Response, abortResponse = true) { |
ecb4e35f C |
547 | const user = await finder() |
548 | ||
549 | if (!user) { | |
550 | if (abortResponse === true) { | |
76148b27 RK |
551 | res.fail({ |
552 | status: HttpStatusCode.NOT_FOUND_404, | |
553 | message: 'User not found' | |
554 | }) | |
ecb4e35f C |
555 | } |
556 | ||
557 | return false | |
558 | } | |
559 | ||
560 | res.locals.user = user | |
ecb4e35f C |
561 | return true |
562 | } |