1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { omit } from 'lodash'
4 import { Hooks } from '@server/lib/plugins/hooks'
5 import { MUserDefault } from '@server/types/models'
6 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
7 import { UserRole } from '../../../shared/models/users'
8 import { UserRegister } from '../../../shared/models/users/user-register.model'
9 import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
10 import { isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
11 import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
13 isNoInstanceConfigWarningModal,
15 isUserAdminFlagsValid,
16 isUserAutoPlayNextVideoValid,
17 isUserAutoPlayVideoValid,
18 isUserBlockedReasonValid,
19 isUserDescriptionValid,
20 isUserDisplayNameValid,
21 isUserNSFWPolicyValid,
23 isUserPasswordValidOrEmpty,
27 isUserVideoQuotaDailyValid,
28 isUserVideoQuotaValid,
29 isUserVideosHistoryEnabledValid
30 } from '../../helpers/custom-validators/users'
31 import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
32 import { logger } from '../../helpers/logger'
33 import { doesVideoExist } from '../../helpers/middlewares'
34 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
35 import { isThemeRegistered } from '../../lib/plugins/theme-utils'
36 import { Redis } from '../../lib/redis'
37 import { UserModel } from '../../models/user/user'
38 import { ActorModel } from '../../models/actor/actor'
39 import { areValidationErrors } from './utils'
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) {
77 .status(HttpStatusCode.FORBIDDEN_403)
78 .json({ error: 'You can only create users (and not administrators or moderators)' })
81 if (req.body.channelName) {
82 if (req.body.channelName === req.body.username) {
84 .status(HttpStatusCode.BAD_REQUEST_400)
85 .json({ error: 'Channel name cannot be the same as user username.' })
88 const existing = await ActorModel.loadLocalByName(req.body.channelName)
91 .status(HttpStatusCode.CONFLICT_409)
92 .json({ error: `Channel with name ${req.body.channelName} already exists.` })
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'),
106 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
110 .custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
111 body('channel.displayName')
113 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
115 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
116 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
118 if (areValidationErrors(req, res)) return
119 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
121 const body: UserRegister = req.body
123 if (!body.channel.name || !body.channel.displayName) {
125 .status(HttpStatusCode.BAD_REQUEST_400)
126 .json({ error: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' })
129 if (body.channel.name === body.username) {
130 return res.status(HttpStatusCode.BAD_REQUEST_400)
131 .json({ error: 'Channel name cannot be the same as user username.' })
134 const existing = await ActorModel.loadLocalByName(body.channel.name)
136 return res.status(HttpStatusCode.CONFLICT_409)
137 .json({ error: `Channel with name ${body.channel.name} already exists.` })
145 const usersRemoveValidator = [
146 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
148 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
149 logger.debug('Checking usersRemove parameters', { parameters: req.params })
151 if (areValidationErrors(req, res)) return
152 if (!await checkUserIdExist(req.params.id, res)) return
154 const user = res.locals.user
155 if (user.username === 'root') {
156 return res.status(HttpStatusCode.BAD_REQUEST_400)
157 .json({ error: 'Cannot remove the root user' })
164 const usersBlockingValidator = [
165 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
166 body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'),
168 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
169 logger.debug('Checking usersBlocking parameters', { parameters: req.params })
171 if (areValidationErrors(req, res)) return
172 if (!await checkUserIdExist(req.params.id, res)) return
174 const user = res.locals.user
175 if (user.username === 'root') {
176 return res.status(HttpStatusCode.BAD_REQUEST_400)
177 .json({ error: 'Cannot block the root user' })
184 const deleteMeValidator = [
185 (req: express.Request, res: express.Response, next: express.NextFunction) => {
186 const user = res.locals.oauth.token.User
187 if (user.username === 'root') {
188 return res.status(HttpStatusCode.BAD_REQUEST_400)
189 .json({ error: 'You cannot delete your root account.' })
197 const usersUpdateValidator = [
198 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
200 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
201 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
202 body('emailVerified').optional().isBoolean().withMessage('Should have a valid email verified attribute'),
203 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
204 body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
205 body('pluginAuth').optional(),
208 .customSanitizer(toIntOrNull)
209 .custom(isUserRoleValid).withMessage('Should have a valid role'),
210 body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
212 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
213 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
215 if (areValidationErrors(req, res)) return
216 if (!await checkUserIdExist(req.params.id, res)) return
218 const user = res.locals.user
219 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
220 return res.status(HttpStatusCode.BAD_REQUEST_400)
221 .json({ error: 'Cannot change root role.' })
228 const usersUpdateMeValidator = [
231 .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
234 .custom(isUserDescriptionValid).withMessage('Should have a valid description'),
235 body('currentPassword')
237 .custom(isUserPasswordValid).withMessage('Should have a valid current password'),
240 .custom(isUserPasswordValid).withMessage('Should have a valid password'),
243 .isEmail().withMessage('Should have a valid email attribute'),
246 .custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
247 body('autoPlayVideo')
249 .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
250 body('videoLanguages')
252 .custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'),
253 body('videosHistoryEnabled')
255 .custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'),
258 .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
259 body('noInstanceConfigWarningModal')
261 .custom(v => isNoInstanceConfigWarningModal(v)).withMessage('Should have a valid noInstanceConfigWarningModal boolean'),
262 body('noWelcomeModal')
264 .custom(v => isNoWelcomeModal(v)).withMessage('Should have a valid noWelcomeModal boolean'),
265 body('autoPlayNextVideo')
267 .custom(v => isUserAutoPlayNextVideoValid(v)).withMessage('Should have a valid autoPlayNextVideo boolean'),
269 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
270 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
272 const user = res.locals.oauth.token.User
274 if (req.body.password || req.body.email) {
275 if (user.pluginAuth !== null) {
276 return res.status(HttpStatusCode.BAD_REQUEST_400)
277 .json({ error: 'You cannot update your email or password that is associated with an external auth system.' })
280 if (!req.body.currentPassword) {
281 return res.status(HttpStatusCode.BAD_REQUEST_400)
282 .json({ error: 'currentPassword parameter is missing.' })
285 if (await user.isPasswordMatch(req.body.currentPassword) !== true) {
286 return res.status(HttpStatusCode.UNAUTHORIZED_401)
287 .json({ error: 'currentPassword is invalid.' })
291 if (areValidationErrors(req, res)) return
297 const usersGetValidator = [
298 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
299 query('withStats').optional().isBoolean().withMessage('Should have a valid stats flag'),
301 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
302 logger.debug('Checking usersGet parameters', { parameters: req.params })
304 if (areValidationErrors(req, res)) return
305 if (!await checkUserIdExist(req.params.id, res, req.query.withStats)) return
311 const usersVideoRatingValidator = [
312 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
314 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
315 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
317 if (areValidationErrors(req, res)) return
318 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
324 const ensureUserRegistrationAllowed = [
325 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
326 const allowedParams = {
331 const allowedResult = await Hooks.wrapPromiseFun(
334 'filter:api.user.signup.allowed.result'
337 if (allowedResult.allowed === false) {
338 return res.status(HttpStatusCode.FORBIDDEN_403)
339 .json({ error: allowedResult.errorMessage || 'User registration is not enabled or user limit is reached.' })
346 const ensureUserRegistrationAllowedForIP = [
347 (req: express.Request, res: express.Response, next: express.NextFunction) => {
348 const allowed = isSignupAllowedForCurrentIP(req.ip)
350 if (allowed === false) {
351 return res.status(HttpStatusCode.FORBIDDEN_403)
352 .json({ error: 'You are not on a network authorized for registration.' })
359 const usersAskResetPasswordValidator = [
360 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
362 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
363 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
365 if (areValidationErrors(req, res)) return
367 const exists = await checkUserEmailExist(req.body.email, res, false)
369 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
370 // Do not leak our emails
371 return res.status(HttpStatusCode.NO_CONTENT_204).end()
378 const usersResetPasswordValidator = [
379 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
380 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
381 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
383 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
384 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
386 if (areValidationErrors(req, res)) return
387 if (!await checkUserIdExist(req.params.id, res)) return
389 const user = res.locals.user
390 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
392 if (redisVerificationString !== req.body.verificationString) {
394 .status(HttpStatusCode.FORBIDDEN_403)
395 .json({ error: 'Invalid verification string.' })
402 const usersAskSendVerifyEmailValidator = [
403 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
405 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
406 logger.debug('Checking askUsersSendVerifyEmail parameters', { parameters: req.body })
408 if (areValidationErrors(req, res)) return
409 const exists = await checkUserEmailExist(req.body.email, res, false)
411 logger.debug('User with email %s does not exist (asking verify email).', req.body.email)
412 // Do not leak our emails
413 return res.status(HttpStatusCode.NO_CONTENT_204).end()
420 const usersVerifyEmailValidator = [
422 .isInt().not().isEmpty().withMessage('Should have a valid id'),
424 body('verificationString')
425 .not().isEmpty().withMessage('Should have a valid verification string'),
426 body('isPendingEmail')
428 .customSanitizer(toBooleanOrNull),
430 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
431 logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params })
433 if (areValidationErrors(req, res)) return
434 if (!await checkUserIdExist(req.params.id, res)) return
436 const user = res.locals.user
437 const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id)
439 if (redisVerificationString !== req.body.verificationString) {
441 .status(HttpStatusCode.FORBIDDEN_403)
442 .json({ error: 'Invalid verification string.' })
449 const userAutocompleteValidator = [
450 param('search').isString().not().isEmpty().withMessage('Should have a search parameter')
453 const ensureAuthUserOwnsAccountValidator = [
454 (req: express.Request, res: express.Response, next: express.NextFunction) => {
455 const user = res.locals.oauth.token.User
457 if (res.locals.account.id !== user.Account.id) {
458 return res.status(HttpStatusCode.FORBIDDEN_403)
459 .json({ error: 'Only owner can access ratings list.' })
466 const ensureCanManageUser = [
467 (req: express.Request, res: express.Response, next: express.NextFunction) => {
468 const authUser = res.locals.oauth.token.User
469 const onUser = res.locals.user
471 if (authUser.role === UserRole.ADMINISTRATOR) return next()
472 if (authUser.role === UserRole.MODERATOR && onUser.role === UserRole.USER) return next()
474 return res.status(HttpStatusCode.FORBIDDEN_403)
475 .json({ error: 'A moderator can only manager users.' })
479 // ---------------------------------------------------------------------------
485 usersRegisterValidator,
486 usersBlockingValidator,
487 usersRemoveValidator,
488 usersUpdateValidator,
489 usersUpdateMeValidator,
490 usersVideoRatingValidator,
491 ensureUserRegistrationAllowed,
492 ensureUserRegistrationAllowedForIP,
494 usersAskResetPasswordValidator,
495 usersResetPasswordValidator,
496 usersAskSendVerifyEmailValidator,
497 usersVerifyEmailValidator,
498 userAutocompleteValidator,
499 ensureAuthUserOwnsAccountValidator,
503 // ---------------------------------------------------------------------------
505 function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) {
506 const id = parseInt(idArg + '', 10)
507 return checkUserExist(() => UserModel.loadByIdWithChannels(id, withStats), res)
510 function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
511 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
514 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
515 const user = await UserModel.loadByUsernameOrEmail(username, email)
518 res.status(HttpStatusCode.CONFLICT_409)
519 .json({ error: 'User with this username or email already exists.' })
523 const actor = await ActorModel.loadLocalByName(username)
525 res.status(HttpStatusCode.CONFLICT_409)
526 .json({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
533 async function checkUserExist (finder: () => Promise<MUserDefault>, res: express.Response, abortResponse = true) {
534 const user = await finder()
537 if (abortResponse === true) {
538 res.status(HttpStatusCode.NOT_FOUND_404)
539 .json({ error: 'User not found' })
545 res.locals.user = user