1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import { body, param } from 'express-validator'
4 import { omit } from 'lodash'
5 import { isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
8 isUserAutoPlayVideoValid,
9 isUserBlockedReasonValid,
10 isUserDescriptionValid,
11 isUserDisplayNameValid,
12 isUserNSFWPolicyValid,
17 isUserVideoQuotaDailyValid,
18 isUserVideoQuotaValid,
19 isUserVideosHistoryEnabledValid
20 } from '../../helpers/custom-validators/users'
21 import { logger } from '../../helpers/logger'
22 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
23 import { Redis } from '../../lib/redis'
24 import { UserModel } from '../../models/account/user'
25 import { areValidationErrors } from './utils'
26 import { ActorModel } from '../../models/activitypub/actor'
27 import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
28 import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
29 import { UserRegister } from '../../../shared/models/users/user-register.model'
30 import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
31 import { isThemeRegistered } from '../../lib/plugins/theme-utils'
32 import { doesVideoExist } from '../../helpers/middlewares'
33 import { UserRole } from '../../../shared/models/users'
35 const usersAddValidator = [
36 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
37 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
38 body('email').isEmail().withMessage('Should have a valid email'),
39 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
40 body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
42 .customSanitizer(toIntOrNull)
43 .custom(isUserRoleValid).withMessage('Should have a valid role'),
44 body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
46 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
49 if (areValidationErrors(req, res)) return
50 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
52 const authUser = res.locals.oauth.token.User
53 if (authUser.role !== UserRole.ADMINISTRATOR && req.body.role !== UserRole.USER) {
54 return res.status(403)
55 .json({ error: 'You can only create users (and not administrators or moderators)' })
62 const usersRegisterValidator = [
63 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
64 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
65 body('email').isEmail().withMessage('Should have a valid email'),
68 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
72 .custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
73 body('channel.displayName')
75 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
77 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
78 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
80 if (areValidationErrors(req, res)) return
81 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
83 const body: UserRegister = req.body
85 if (!body.channel.name || !body.channel.displayName) {
86 return res.status(400)
87 .json({ error: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' })
90 if (body.channel.name === body.username) {
91 return res.status(400)
92 .json({ error: 'Channel name cannot be the same than user username.' })
95 const existing = await ActorModel.loadLocalByName(body.channel.name)
97 return res.status(409)
98 .json({ error: `Channel with name ${body.channel.name} already exists.` })
106 const usersRemoveValidator = [
107 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
109 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 logger.debug('Checking usersRemove parameters', { parameters: req.params })
112 if (areValidationErrors(req, res)) return
113 if (!await checkUserIdExist(req.params.id, res)) return
115 const user = res.locals.user
116 if (user.username === 'root') {
117 return res.status(400)
118 .json({ error: 'Cannot remove the root user' })
125 const usersBlockingValidator = [
126 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
127 body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'),
129 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 logger.debug('Checking usersBlocking parameters', { parameters: req.params })
132 if (areValidationErrors(req, res)) return
133 if (!await checkUserIdExist(req.params.id, res)) return
135 const user = res.locals.user
136 if (user.username === 'root') {
137 return res.status(400)
138 .json({ error: 'Cannot block the root user' })
145 const deleteMeValidator = [
146 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
147 const user = res.locals.oauth.token.User
148 if (user.username === 'root') {
149 return res.status(400)
150 .json({ error: 'You cannot delete your root account.' })
158 const usersUpdateValidator = [
159 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
160 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
161 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
162 body('emailVerified').optional().isBoolean().withMessage('Should have a valid email verified attribute'),
163 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
164 body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
167 .customSanitizer(toIntOrNull)
168 .custom(isUserRoleValid).withMessage('Should have a valid role'),
169 body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
171 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
172 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
174 if (areValidationErrors(req, res)) return
175 if (!await checkUserIdExist(req.params.id, res)) return
177 const user = res.locals.user
178 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
179 return res.status(400)
180 .json({ error: 'Cannot change root role.' })
187 const usersUpdateMeValidator = [
190 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
193 .custom(isUserDescriptionValid).withMessage('Should have a valid description'),
194 body('currentPassword')
196 .custom(isUserPasswordValid).withMessage('Should have a valid current password'),
199 .custom(isUserPasswordValid).withMessage('Should have a valid password'),
202 .isEmail().withMessage('Should have a valid email attribute'),
205 .custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
206 body('autoPlayVideo')
208 .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
209 body('videoLanguages')
211 .custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'),
212 body('videosHistoryEnabled')
214 .custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'),
217 .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
219 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
220 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
222 if (req.body.password || req.body.email) {
223 if (!req.body.currentPassword) {
224 return res.status(400)
225 .json({ error: 'currentPassword parameter is missing.' })
229 const user = res.locals.oauth.token.User
230 if (await user.isPasswordMatch(req.body.currentPassword) !== true) {
231 return res.status(401)
232 .json({ error: 'currentPassword is invalid.' })
236 if (areValidationErrors(req, res)) return
242 const usersGetValidator = [
243 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
245 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
246 logger.debug('Checking usersGet parameters', { parameters: req.params })
248 if (areValidationErrors(req, res)) return
249 if (!await checkUserIdExist(req.params.id, res)) return
255 const usersVideoRatingValidator = [
256 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
258 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
259 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
261 if (areValidationErrors(req, res)) return
262 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
268 const ensureUserRegistrationAllowed = [
269 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
270 const allowed = await isSignupAllowed()
271 if (allowed === false) {
272 return res.status(403)
273 .json({ error: 'User registration is not enabled or user limit is reached.' })
280 const ensureUserRegistrationAllowedForIP = [
281 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
282 const allowed = isSignupAllowedForCurrentIP(req.ip)
284 if (allowed === false) {
285 return res.status(403)
286 .json({ error: 'You are not on a network authorized for registration.' })
293 const usersAskResetPasswordValidator = [
294 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
296 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
297 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
299 if (areValidationErrors(req, res)) return
301 const exists = await checkUserEmailExist(req.body.email, res, false)
303 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
304 // Do not leak our emails
305 return res.status(204).end()
312 const usersResetPasswordValidator = [
313 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
314 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
315 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
317 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
318 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
320 if (areValidationErrors(req, res)) return
321 if (!await checkUserIdExist(req.params.id, res)) return
323 const user = res.locals.user
324 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
326 if (redisVerificationString !== req.body.verificationString) {
329 .json({ error: 'Invalid verification string.' })
336 const usersAskSendVerifyEmailValidator = [
337 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
339 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
340 logger.debug('Checking askUsersSendVerifyEmail parameters', { parameters: req.body })
342 if (areValidationErrors(req, res)) return
343 const exists = await checkUserEmailExist(req.body.email, res, false)
345 logger.debug('User with email %s does not exist (asking verify email).', req.body.email)
346 // Do not leak our emails
347 return res.status(204).end()
354 const usersVerifyEmailValidator = [
356 .isInt().not().isEmpty().withMessage('Should have a valid id'),
358 body('verificationString')
359 .not().isEmpty().withMessage('Should have a valid verification string'),
360 body('isPendingEmail')
362 .customSanitizer(toBooleanOrNull),
364 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
365 logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params })
367 if (areValidationErrors(req, res)) return
368 if (!await checkUserIdExist(req.params.id, res)) return
370 const user = res.locals.user
371 const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id)
373 if (redisVerificationString !== req.body.verificationString) {
376 .json({ error: 'Invalid verification string.' })
383 const userAutocompleteValidator = [
384 param('search').isString().not().isEmpty().withMessage('Should have a search parameter')
387 const ensureAuthUserOwnsAccountValidator = [
388 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
389 const user = res.locals.oauth.token.User
391 if (res.locals.account.id !== user.Account.id) {
392 return res.status(403)
393 .json({ error: 'Only owner can access ratings list.' })
400 const ensureCanManageUser = [
401 (req: express.Request, res: express.Response, next: express.NextFunction) => {
402 const authUser = res.locals.oauth.token.User
403 const onUser = res.locals.user
405 if (authUser.role === UserRole.ADMINISTRATOR) return next()
406 if (authUser.role === UserRole.MODERATOR && onUser.role === UserRole.USER) return next()
408 return res.status(403)
409 .json({ error: 'A moderator can only manager users.' })
413 // ---------------------------------------------------------------------------
418 usersRegisterValidator,
419 usersBlockingValidator,
420 usersRemoveValidator,
421 usersUpdateValidator,
422 usersUpdateMeValidator,
423 usersVideoRatingValidator,
424 ensureUserRegistrationAllowed,
425 ensureUserRegistrationAllowedForIP,
427 usersAskResetPasswordValidator,
428 usersResetPasswordValidator,
429 usersAskSendVerifyEmailValidator,
430 usersVerifyEmailValidator,
431 userAutocompleteValidator,
432 ensureAuthUserOwnsAccountValidator,
436 // ---------------------------------------------------------------------------
438 function checkUserIdExist (id: number, res: express.Response) {
439 return checkUserExist(() => UserModel.loadById(id), res)
442 function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
443 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
446 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
447 const user = await UserModel.loadByUsernameOrEmail(username, email)
451 .json({ error: 'User with this username or email already exists.' })
455 const actor = await ActorModel.loadLocalByName(username)
458 .json({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
465 async function checkUserExist (finder: () => Bluebird<UserModel>, res: express.Response, abortResponse = true) {
466 const user = await finder()
469 if (abortResponse === true) {
471 .json({ error: 'User not found' })
477 res.locals.user = user