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 .customSanitizer(toBooleanOrNull)
45 .isBoolean().withMessage('Should be a valid boolean banned state'),
47 (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking usersList parameters', { parameters: req.query })
50 if (areValidationErrors(req, res)) return
56 const usersAddValidator = [
57 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
58 body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'),
59 body('email').isEmail().withMessage('Should have a valid email'),
60 body('channelName').optional().custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
61 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
62 body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
64 .customSanitizer(toIntOrNull)
65 .custom(isUserRoleValid).withMessage('Should have a valid role'),
66 body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
68 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
69 logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
71 if (areValidationErrors(req, res)) return
72 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
74 const authUser = res.locals.oauth.token.User
75 if (authUser.role !== UserRole.ADMINISTRATOR && req.body.role !== UserRole.USER) {
76 return res.status(403)
77 .json({ error: 'You can only create users (and not administrators or moderators)' })
80 if (req.body.channelName) {
81 if (req.body.channelName === req.body.username) {
82 return res.status(400)
83 .json({ error: 'Channel name cannot be the same as user username.' })
86 const existing = await ActorModel.loadLocalByName(req.body.channelName)
88 return res.status(409)
89 .json({ error: `Channel with name ${req.body.channelName} already exists.` })
97 const usersRegisterValidator = [
98 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
99 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
100 body('email').isEmail().withMessage('Should have a valid email'),
103 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
107 .custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
108 body('channel.displayName')
110 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
112 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
113 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
115 if (areValidationErrors(req, res)) return
116 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
118 const body: UserRegister = req.body
120 if (!body.channel.name || !body.channel.displayName) {
121 return res.status(400)
122 .json({ error: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' })
125 if (body.channel.name === body.username) {
126 return res.status(400)
127 .json({ error: 'Channel name cannot be the same as user username.' })
130 const existing = await ActorModel.loadLocalByName(body.channel.name)
132 return res.status(409)
133 .json({ error: `Channel with name ${body.channel.name} already exists.` })
141 const usersRemoveValidator = [
142 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
144 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
145 logger.debug('Checking usersRemove parameters', { parameters: req.params })
147 if (areValidationErrors(req, res)) return
148 if (!await checkUserIdExist(req.params.id, res)) return
150 const user = res.locals.user
151 if (user.username === 'root') {
152 return res.status(400)
153 .json({ error: 'Cannot remove the root user' })
160 const usersBlockingValidator = [
161 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
162 body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'),
164 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
165 logger.debug('Checking usersBlocking parameters', { parameters: req.params })
167 if (areValidationErrors(req, res)) return
168 if (!await checkUserIdExist(req.params.id, res)) return
170 const user = res.locals.user
171 if (user.username === 'root') {
172 return res.status(400)
173 .json({ error: 'Cannot block the root user' })
180 const deleteMeValidator = [
181 (req: express.Request, res: express.Response, next: express.NextFunction) => {
182 const user = res.locals.oauth.token.User
183 if (user.username === 'root') {
184 return res.status(400)
185 .json({ error: 'You cannot delete your root account.' })
193 const usersUpdateValidator = [
194 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
195 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
196 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
197 body('emailVerified').optional().isBoolean().withMessage('Should have a valid email verified attribute'),
198 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
199 body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
202 .customSanitizer(toIntOrNull)
203 .custom(isUserRoleValid).withMessage('Should have a valid role'),
204 body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
206 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
207 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
209 if (areValidationErrors(req, res)) return
210 if (!await checkUserIdExist(req.params.id, res)) return
212 const user = res.locals.user
213 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
214 return res.status(400)
215 .json({ error: 'Cannot change root role.' })
222 const usersUpdateMeValidator = [
225 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
228 .custom(isUserDescriptionValid).withMessage('Should have a valid description'),
229 body('currentPassword')
231 .custom(isUserPasswordValid).withMessage('Should have a valid current password'),
234 .custom(isUserPasswordValid).withMessage('Should have a valid password'),
237 .isEmail().withMessage('Should have a valid email attribute'),
240 .custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
241 body('autoPlayVideo')
243 .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
244 body('videoLanguages')
246 .custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'),
247 body('videosHistoryEnabled')
249 .custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'),
252 .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
253 body('noInstanceConfigWarningModal')
255 .custom(v => isNoInstanceConfigWarningModal(v)).withMessage('Should have a valid noInstanceConfigWarningModal boolean'),
256 body('noWelcomeModal')
258 .custom(v => isNoWelcomeModal(v)).withMessage('Should have a valid noWelcomeModal boolean'),
259 body('autoPlayNextVideo')
261 .custom(v => isUserAutoPlayNextVideoValid(v)).withMessage('Should have a valid autoPlayNextVideo boolean'),
263 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
264 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
266 const user = res.locals.oauth.token.User
268 if (req.body.password || req.body.email) {
269 if (user.pluginAuth !== null) {
270 return res.status(400)
271 .json({ error: 'You cannot update your email or password that is associated with an external auth system.' })
274 if (!req.body.currentPassword) {
275 return res.status(400)
276 .json({ error: 'currentPassword parameter is missing.' })
279 if (await user.isPasswordMatch(req.body.currentPassword) !== true) {
280 return res.status(401)
281 .json({ error: 'currentPassword is invalid.' })
285 if (areValidationErrors(req, res)) return
291 const usersGetValidator = [
292 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
293 query('withStats').optional().isBoolean().withMessage('Should have a valid stats flag'),
295 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
296 logger.debug('Checking usersGet parameters', { parameters: req.params })
298 if (areValidationErrors(req, res)) return
299 if (!await checkUserIdExist(req.params.id, res, req.query.withStats)) return
305 const usersVideoRatingValidator = [
306 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
308 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
309 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
311 if (areValidationErrors(req, res)) return
312 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
318 const ensureUserRegistrationAllowed = [
319 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
320 const allowedParams = {
325 const allowedResult = await Hooks.wrapPromiseFun(
328 'filter:api.user.signup.allowed.result'
331 if (allowedResult.allowed === false) {
332 return res.status(403)
333 .json({ error: allowedResult.errorMessage || 'User registration is not enabled or user limit is reached.' })
340 const ensureUserRegistrationAllowedForIP = [
341 (req: express.Request, res: express.Response, next: express.NextFunction) => {
342 const allowed = isSignupAllowedForCurrentIP(req.ip)
344 if (allowed === false) {
345 return res.status(403)
346 .json({ error: 'You are not on a network authorized for registration.' })
353 const usersAskResetPasswordValidator = [
354 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
356 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
357 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
359 if (areValidationErrors(req, res)) return
361 const exists = await checkUserEmailExist(req.body.email, res, false)
363 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
364 // Do not leak our emails
365 return res.status(204).end()
372 const usersResetPasswordValidator = [
373 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
374 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
375 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
377 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
378 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
380 if (areValidationErrors(req, res)) return
381 if (!await checkUserIdExist(req.params.id, res)) return
383 const user = res.locals.user
384 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
386 if (redisVerificationString !== req.body.verificationString) {
389 .json({ error: 'Invalid verification string.' })
396 const usersAskSendVerifyEmailValidator = [
397 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
399 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
400 logger.debug('Checking askUsersSendVerifyEmail parameters', { parameters: req.body })
402 if (areValidationErrors(req, res)) return
403 const exists = await checkUserEmailExist(req.body.email, res, false)
405 logger.debug('User with email %s does not exist (asking verify email).', req.body.email)
406 // Do not leak our emails
407 return res.status(204).end()
414 const usersVerifyEmailValidator = [
416 .isInt().not().isEmpty().withMessage('Should have a valid id'),
418 body('verificationString')
419 .not().isEmpty().withMessage('Should have a valid verification string'),
420 body('isPendingEmail')
422 .customSanitizer(toBooleanOrNull),
424 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
425 logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params })
427 if (areValidationErrors(req, res)) return
428 if (!await checkUserIdExist(req.params.id, res)) return
430 const user = res.locals.user
431 const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id)
433 if (redisVerificationString !== req.body.verificationString) {
436 .json({ error: 'Invalid verification string.' })
443 const userAutocompleteValidator = [
444 param('search').isString().not().isEmpty().withMessage('Should have a search parameter')
447 const ensureAuthUserOwnsAccountValidator = [
448 (req: express.Request, res: express.Response, next: express.NextFunction) => {
449 const user = res.locals.oauth.token.User
451 if (res.locals.account.id !== user.Account.id) {
452 return res.status(403)
453 .json({ error: 'Only owner can access ratings list.' })
460 const ensureCanManageUser = [
461 (req: express.Request, res: express.Response, next: express.NextFunction) => {
462 const authUser = res.locals.oauth.token.User
463 const onUser = res.locals.user
465 if (authUser.role === UserRole.ADMINISTRATOR) return next()
466 if (authUser.role === UserRole.MODERATOR && onUser.role === UserRole.USER) return next()
468 return res.status(403)
469 .json({ error: 'A moderator can only manager users.' })
473 // ---------------------------------------------------------------------------
479 usersRegisterValidator,
480 usersBlockingValidator,
481 usersRemoveValidator,
482 usersUpdateValidator,
483 usersUpdateMeValidator,
484 usersVideoRatingValidator,
485 ensureUserRegistrationAllowed,
486 ensureUserRegistrationAllowedForIP,
488 usersAskResetPasswordValidator,
489 usersResetPasswordValidator,
490 usersAskSendVerifyEmailValidator,
491 usersVerifyEmailValidator,
492 userAutocompleteValidator,
493 ensureAuthUserOwnsAccountValidator,
497 // ---------------------------------------------------------------------------
499 function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) {
500 const id = parseInt(idArg + '', 10)
501 return checkUserExist(() => UserModel.loadByIdWithChannels(id, withStats), res)
504 function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
505 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
508 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
509 const user = await UserModel.loadByUsernameOrEmail(username, email)
513 .json({ error: 'User with this username or email already exists.' })
517 const actor = await ActorModel.loadLocalByName(username)
520 .json({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
527 async function checkUserExist (finder: () => Bluebird<MUserDefault>, res: express.Response, abortResponse = true) {
528 const user = await finder()
531 if (abortResponse === true) {
533 .json({ error: 'User not found' })
539 res.locals.user = user