1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import 'express-validator'
4 import { body, param } from 'express-validator/check'
5 import { omit } from 'lodash'
6 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
9 isUserAutoPlayVideoValid,
10 isUserDisplayNSFWValid,
15 } from '../../helpers/custom-validators/users'
16 import { isVideoExist } from '../../helpers/custom-validators/videos'
17 import { logger } from '../../helpers/logger'
18 import { isSignupAllowed } from '../../helpers/utils'
19 import { CONSTRAINTS_FIELDS } from '../../initializers'
20 import { Redis } from '../../lib/redis'
21 import { UserModel } from '../../models/account/user'
22 import { areValidationErrors } from './utils'
24 const usersAddValidator = [
25 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
26 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
27 body('email').isEmail().withMessage('Should have a valid email'),
28 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
29 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
34 if (areValidationErrors(req, res)) return
35 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
41 const usersRegisterValidator = [
42 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
43 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
44 body('email').isEmail().withMessage('Should have a valid email'),
46 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
49 if (areValidationErrors(req, res)) return
50 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
56 const usersRemoveValidator = [
57 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
59 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking usersRemove parameters', { parameters: req.params })
62 if (areValidationErrors(req, res)) return
63 if (!await checkUserIdExist(req.params.id, res)) return
65 const user = res.locals.user
66 if (user.username === 'root') {
67 return res.status(400)
68 .send({ error: 'Cannot remove the root user' })
76 const usersUpdateValidator = [
77 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
78 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
79 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
80 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
82 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
83 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
85 if (areValidationErrors(req, res)) return
86 if (!await checkUserIdExist(req.params.id, res)) return
88 const user = res.locals.user
89 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
90 return res.status(400)
91 .send({ error: 'Cannot change root role.' })
99 const usersUpdateMeValidator = [
100 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
101 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
102 body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
103 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
105 (req: express.Request, res: express.Response, next: express.NextFunction) => {
106 // TODO: Add old password verification
107 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
109 if (areValidationErrors(req, res)) return
115 const usersUpdateMyAvatarValidator = [
116 body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage(
117 'This file is not supported. Please, make sure it is of the following type : '
118 + CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME.join(', ')
121 (req: express.Request, res: express.Response, next: express.NextFunction) => {
122 logger.debug('Checking usersUpdateMyAvatarValidator parameters', { files: req.files })
124 if (areValidationErrors(req, res)) return
126 const imageFile = req.files['avatarfile'][0] as Express.Multer.File
127 if (imageFile.size > CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max) {
129 .send({ error: `The size of the avatar is too big (>${CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max}).` })
138 const usersGetValidator = [
139 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
141 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
142 logger.debug('Checking usersGet parameters', { parameters: req.params })
144 if (areValidationErrors(req, res)) return
145 if (!await checkUserIdExist(req.params.id, res)) return
151 const usersVideoRatingValidator = [
152 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
154 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
155 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
157 if (areValidationErrors(req, res)) return
158 if (!await isVideoExist(req.params.videoId, res)) return
164 const ensureUserRegistrationAllowed = [
165 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
166 const allowed = await isSignupAllowed()
167 if (allowed === false) {
168 return res.status(403)
169 .send({ error: 'User registration is not enabled or user limit is reached.' })
177 const usersAskResetPasswordValidator = [
178 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
180 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
181 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
183 if (areValidationErrors(req, res)) return
184 const exists = await checkUserEmailExist(req.body.email, res, false)
186 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
187 // Do not leak our emails
188 return res.status(204).end()
195 const usersResetPasswordValidator = [
196 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
197 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
198 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
200 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
201 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
203 if (areValidationErrors(req, res)) return
204 if (!await checkUserIdExist(req.params.id, res)) return
206 const user = res.locals.user as UserModel
207 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
209 if (redisVerificationString !== req.body.verificationString) {
212 .send({ error: 'Invalid verification string.' })
220 // ---------------------------------------------------------------------------
224 usersRegisterValidator,
225 usersRemoveValidator,
226 usersUpdateValidator,
227 usersUpdateMeValidator,
228 usersVideoRatingValidator,
229 ensureUserRegistrationAllowed,
231 usersUpdateMyAvatarValidator,
232 usersAskResetPasswordValidator,
233 usersResetPasswordValidator
236 // ---------------------------------------------------------------------------
238 function checkUserIdExist (id: number, res: express.Response) {
239 return checkUserExist(() => UserModel.loadById(id), res)
242 function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
243 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
246 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
247 const user = await UserModel.loadByUsernameOrEmail(username, email)
251 .send({ error: 'User with this username of email already exists.' })
259 async function checkUserExist (finder: () => Bluebird<UserModel>, res: express.Response, abortResponse = true) {
260 const user = await finder()
263 if (abortResponse === true) {
265 .send({ error: 'User not found' })
272 res.locals.user = user