1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import { body, param, query } from 'express-validator'
4 import { omit } from 'lodash'
5 import { isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
7 isNoInstanceConfigWarningModal,
10 isUserAutoPlayNextVideoValid,
11 isUserAutoPlayVideoValid,
12 isUserBlockedReasonValid,
13 isUserDescriptionValid,
14 isUserDisplayNameValid,
15 isUserNSFWPolicyValid,
17 isUserPasswordValidOrEmpty,
21 isUserVideoQuotaDailyValid,
22 isUserVideoQuotaValid,
23 isUserVideosHistoryEnabledValid
24 } from '../../helpers/custom-validators/users'
25 import { logger } from '../../helpers/logger'
26 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
27 import { Redis } from '../../lib/redis'
28 import { UserModel } from '../../models/account/user'
29 import { areValidationErrors } from './utils'
30 import { ActorModel } from '../../models/activitypub/actor'
31 import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
32 import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
33 import { UserRegister } from '../../../shared/models/users/user-register.model'
34 import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
35 import { isThemeRegistered } from '../../lib/plugins/theme-utils'
36 import { doesVideoExist } from '../../helpers/middlewares'
37 import { UserRole } from '../../../shared/models/users'
38 import { MUserDefault } from '@server/types/models'
39 import { Hooks } from '@server/lib/plugins/hooks'
41 const usersListValidator = [
44 .isBoolean().withMessage('Should be a valid boolean banned state'),
46 (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking usersList parameters', { parameters: req.query })
49 if (areValidationErrors(req, res)) return
55 const usersAddValidator = [
56 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
57 body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'),
58 body('email').isEmail().withMessage('Should have a valid email'),
59 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
60 body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
62 .customSanitizer(toIntOrNull)
63 .custom(isUserRoleValid).withMessage('Should have a valid role'),
64 body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
66 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
69 if (areValidationErrors(req, res)) return
70 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
72 const authUser = res.locals.oauth.token.User
73 if (authUser.role !== UserRole.ADMINISTRATOR && req.body.role !== UserRole.USER) {
74 return res.status(403)
75 .json({ error: 'You can only create users (and not administrators or moderators)' })
82 const usersRegisterValidator = [
83 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
84 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
85 body('email').isEmail().withMessage('Should have a valid email'),
88 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
92 .custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
93 body('channel.displayName')
95 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
100 if (areValidationErrors(req, res)) return
101 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
103 const body: UserRegister = req.body
105 if (!body.channel.name || !body.channel.displayName) {
106 return res.status(400)
107 .json({ error: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' })
110 if (body.channel.name === body.username) {
111 return res.status(400)
112 .json({ error: 'Channel name cannot be the same as user username.' })
115 const existing = await ActorModel.loadLocalByName(body.channel.name)
117 return res.status(409)
118 .json({ error: `Channel with name ${body.channel.name} already exists.` })
126 const usersRemoveValidator = [
127 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
129 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 logger.debug('Checking usersRemove 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 remove the root user' })
145 const usersBlockingValidator = [
146 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
147 body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'),
149 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
150 logger.debug('Checking usersBlocking parameters', { parameters: req.params })
152 if (areValidationErrors(req, res)) return
153 if (!await checkUserIdExist(req.params.id, res)) return
155 const user = res.locals.user
156 if (user.username === 'root') {
157 return res.status(400)
158 .json({ error: 'Cannot block the root user' })
165 const deleteMeValidator = [
166 (req: express.Request, res: express.Response, next: express.NextFunction) => {
167 const user = res.locals.oauth.token.User
168 if (user.username === 'root') {
169 return res.status(400)
170 .json({ error: 'You cannot delete your root account.' })
178 const usersUpdateValidator = [
179 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
180 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
181 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
182 body('emailVerified').optional().isBoolean().withMessage('Should have a valid email verified attribute'),
183 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
184 body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
187 .customSanitizer(toIntOrNull)
188 .custom(isUserRoleValid).withMessage('Should have a valid role'),
189 body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
191 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
192 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
194 if (areValidationErrors(req, res)) return
195 if (!await checkUserIdExist(req.params.id, res)) return
197 const user = res.locals.user
198 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
199 return res.status(400)
200 .json({ error: 'Cannot change root role.' })
207 const usersUpdateMeValidator = [
210 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
213 .custom(isUserDescriptionValid).withMessage('Should have a valid description'),
214 body('currentPassword')
216 .custom(isUserPasswordValid).withMessage('Should have a valid current password'),
219 .custom(isUserPasswordValid).withMessage('Should have a valid password'),
222 .isEmail().withMessage('Should have a valid email attribute'),
225 .custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
226 body('autoPlayVideo')
228 .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
229 body('videoLanguages')
231 .custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'),
232 body('videosHistoryEnabled')
234 .custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'),
237 .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
238 body('noInstanceConfigWarningModal')
240 .custom(v => isNoInstanceConfigWarningModal(v)).withMessage('Should have a valid noInstanceConfigWarningModal boolean'),
241 body('noWelcomeModal')
243 .custom(v => isNoWelcomeModal(v)).withMessage('Should have a valid noWelcomeModal boolean'),
244 body('autoPlayNextVideo')
246 .custom(v => isUserAutoPlayNextVideoValid(v)).withMessage('Should have a valid autoPlayNextVideo boolean'),
248 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
249 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
251 const user = res.locals.oauth.token.User
253 if (req.body.password || req.body.email) {
254 if (user.pluginAuth !== null) {
255 return res.status(400)
256 .json({ error: 'You cannot update your email or password that is associated with an external auth system.' })
259 if (!req.body.currentPassword) {
260 return res.status(400)
261 .json({ error: 'currentPassword parameter is missing.' })
264 if (await user.isPasswordMatch(req.body.currentPassword) !== true) {
265 return res.status(401)
266 .json({ error: 'currentPassword is invalid.' })
270 if (areValidationErrors(req, res)) return
276 const usersGetValidator = [
277 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
278 query('withStats').optional().isBoolean().withMessage('Should have a valid stats flag'),
280 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
281 logger.debug('Checking usersGet parameters', { parameters: req.params })
283 if (areValidationErrors(req, res)) return
284 if (!await checkUserIdExist(req.params.id, res, req.query.withStats)) return
290 const usersVideoRatingValidator = [
291 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
293 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
294 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
296 if (areValidationErrors(req, res)) return
297 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
303 const ensureUserRegistrationAllowed = [
304 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
305 const allowedParams = {
310 const allowedResult = await Hooks.wrapPromiseFun(
313 'filter:api.user.signup.allowed.result'
316 if (allowedResult.allowed === false) {
317 return res.status(403)
318 .json({ error: allowedResult.errorMessage || 'User registration is not enabled or user limit is reached.' })
325 const ensureUserRegistrationAllowedForIP = [
326 (req: express.Request, res: express.Response, next: express.NextFunction) => {
327 const allowed = isSignupAllowedForCurrentIP(req.ip)
329 if (allowed === false) {
330 return res.status(403)
331 .json({ error: 'You are not on a network authorized for registration.' })
338 const usersAskResetPasswordValidator = [
339 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
341 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
342 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
344 if (areValidationErrors(req, res)) return
346 const exists = await checkUserEmailExist(req.body.email, res, false)
348 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
349 // Do not leak our emails
350 return res.status(204).end()
357 const usersResetPasswordValidator = [
358 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
359 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
360 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
362 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
363 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
365 if (areValidationErrors(req, res)) return
366 if (!await checkUserIdExist(req.params.id, res)) return
368 const user = res.locals.user
369 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
371 if (redisVerificationString !== req.body.verificationString) {
374 .json({ error: 'Invalid verification string.' })
381 const usersAskSendVerifyEmailValidator = [
382 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
384 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
385 logger.debug('Checking askUsersSendVerifyEmail parameters', { parameters: req.body })
387 if (areValidationErrors(req, res)) return
388 const exists = await checkUserEmailExist(req.body.email, res, false)
390 logger.debug('User with email %s does not exist (asking verify email).', req.body.email)
391 // Do not leak our emails
392 return res.status(204).end()
399 const usersVerifyEmailValidator = [
401 .isInt().not().isEmpty().withMessage('Should have a valid id'),
403 body('verificationString')
404 .not().isEmpty().withMessage('Should have a valid verification string'),
405 body('isPendingEmail')
407 .customSanitizer(toBooleanOrNull),
409 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
410 logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params })
412 if (areValidationErrors(req, res)) return
413 if (!await checkUserIdExist(req.params.id, res)) return
415 const user = res.locals.user
416 const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id)
418 if (redisVerificationString !== req.body.verificationString) {
421 .json({ error: 'Invalid verification string.' })
428 const userAutocompleteValidator = [
429 param('search').isString().not().isEmpty().withMessage('Should have a search parameter')
432 const ensureAuthUserOwnsAccountValidator = [
433 (req: express.Request, res: express.Response, next: express.NextFunction) => {
434 const user = res.locals.oauth.token.User
436 if (res.locals.account.id !== user.Account.id) {
437 return res.status(403)
438 .json({ error: 'Only owner can access ratings list.' })
445 const ensureCanManageUser = [
446 (req: express.Request, res: express.Response, next: express.NextFunction) => {
447 const authUser = res.locals.oauth.token.User
448 const onUser = res.locals.user
450 if (authUser.role === UserRole.ADMINISTRATOR) return next()
451 if (authUser.role === UserRole.MODERATOR && onUser.role === UserRole.USER) return next()
453 return res.status(403)
454 .json({ error: 'A moderator can only manager users.' })
458 // ---------------------------------------------------------------------------
464 usersRegisterValidator,
465 usersBlockingValidator,
466 usersRemoveValidator,
467 usersUpdateValidator,
468 usersUpdateMeValidator,
469 usersVideoRatingValidator,
470 ensureUserRegistrationAllowed,
471 ensureUserRegistrationAllowedForIP,
473 usersAskResetPasswordValidator,
474 usersResetPasswordValidator,
475 usersAskSendVerifyEmailValidator,
476 usersVerifyEmailValidator,
477 userAutocompleteValidator,
478 ensureAuthUserOwnsAccountValidator,
482 // ---------------------------------------------------------------------------
484 function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) {
485 const id = parseInt(idArg + '', 10)
486 return checkUserExist(() => UserModel.loadById(id, withStats), res)
489 function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
490 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
493 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
494 const user = await UserModel.loadByUsernameOrEmail(username, email)
498 .json({ error: 'User with this username or email already exists.' })
502 const actor = await ActorModel.loadLocalByName(username)
505 .json({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
512 async function checkUserExist (finder: () => Bluebird<MUserDefault>, res: express.Response, abortResponse = true) {
513 const user = await finder()
516 if (abortResponse === true) {
518 .json({ error: 'User not found' })
524 res.locals.user = user