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'
8 isUserAutoPlayVideoValid, isUserBlockedReasonValid,
9 isUserDescriptionValid,
10 isUserDisplayNameValid,
11 isUserNSFWPolicyValid,
16 } from '../../helpers/custom-validators/users'
17 import { isVideoExist } from '../../helpers/custom-validators/videos'
18 import { logger } from '../../helpers/logger'
19 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
20 import { Redis } from '../../lib/redis'
21 import { UserModel } from '../../models/account/user'
22 import { areValidationErrors } from './utils'
23 import { ActorModel } from '../../models/activitypub/actor'
25 const usersAddValidator = [
26 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
27 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
28 body('email').isEmail().withMessage('Should have a valid email'),
29 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
30 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
32 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
35 if (areValidationErrors(req, res)) return
36 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
42 const usersRegisterValidator = [
43 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
44 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
45 body('email').isEmail().withMessage('Should have a valid email'),
47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
50 if (areValidationErrors(req, res)) return
51 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
57 const usersRemoveValidator = [
58 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
60 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
61 logger.debug('Checking usersRemove parameters', { parameters: req.params })
63 if (areValidationErrors(req, res)) return
64 if (!await checkUserIdExist(req.params.id, res)) return
66 const user = res.locals.user
67 if (user.username === 'root') {
68 return res.status(400)
69 .send({ error: 'Cannot remove the root user' })
77 const usersBlockingValidator = [
78 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
79 body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'),
81 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
82 logger.debug('Checking usersBlocking parameters', { parameters: req.params })
84 if (areValidationErrors(req, res)) return
85 if (!await checkUserIdExist(req.params.id, res)) return
87 const user = res.locals.user
88 if (user.username === 'root') {
89 return res.status(400)
90 .send({ error: 'Cannot block the root user' })
98 const deleteMeValidator = [
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 const user: UserModel = res.locals.oauth.token.User
101 if (user.username === 'root') {
102 return res.status(400)
103 .send({ error: 'You cannot delete your root account.' })
111 const usersUpdateValidator = [
112 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
113 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
114 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
115 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
117 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
118 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
120 if (areValidationErrors(req, res)) return
121 if (!await checkUserIdExist(req.params.id, res)) return
123 const user = res.locals.user
124 if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
125 return res.status(400)
126 .send({ error: 'Cannot change root role.' })
134 const usersUpdateMeValidator = [
135 body('displayName').optional().custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
136 body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'),
137 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
138 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
139 body('nsfwPolicy').optional().custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
140 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
142 (req: express.Request, res: express.Response, next: express.NextFunction) => {
143 // TODO: Add old password verification
144 logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
146 if (areValidationErrors(req, res)) return
152 const usersGetValidator = [
153 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
155 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
156 logger.debug('Checking usersGet parameters', { parameters: req.params })
158 if (areValidationErrors(req, res)) return
159 if (!await checkUserIdExist(req.params.id, res)) return
165 const usersVideoRatingValidator = [
166 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
168 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
169 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
171 if (areValidationErrors(req, res)) return
172 if (!await isVideoExist(req.params.videoId, res)) return
178 const ensureUserRegistrationAllowed = [
179 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
180 const allowed = await isSignupAllowed()
181 if (allowed === false) {
182 return res.status(403)
183 .send({ error: 'User registration is not enabled or user limit is reached.' })
191 const ensureUserRegistrationAllowedForIP = [
192 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
193 const allowed = isSignupAllowedForCurrentIP(req.ip)
195 if (allowed === false) {
196 return res.status(403)
197 .send({ error: 'You are not on a network authorized for registration.' })
205 const usersAskResetPasswordValidator = [
206 body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
208 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
209 logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
211 if (areValidationErrors(req, res)) return
212 const exists = await checkUserEmailExist(req.body.email, res, false)
214 logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
215 // Do not leak our emails
216 return res.status(204).end()
223 const usersResetPasswordValidator = [
224 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
225 body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
226 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
228 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
229 logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
231 if (areValidationErrors(req, res)) return
232 if (!await checkUserIdExist(req.params.id, res)) return
234 const user = res.locals.user as UserModel
235 const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
237 if (redisVerificationString !== req.body.verificationString) {
240 .send({ error: 'Invalid verification string.' })
248 // ---------------------------------------------------------------------------
253 usersRegisterValidator,
254 usersBlockingValidator,
255 usersRemoveValidator,
256 usersUpdateValidator,
257 usersUpdateMeValidator,
258 usersVideoRatingValidator,
259 ensureUserRegistrationAllowed,
260 ensureUserRegistrationAllowedForIP,
262 usersAskResetPasswordValidator,
263 usersResetPasswordValidator
266 // ---------------------------------------------------------------------------
268 function checkUserIdExist (id: number, res: express.Response) {
269 return checkUserExist(() => UserModel.loadById(id), res)
272 function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
273 return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
276 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
277 const user = await UserModel.loadByUsernameOrEmail(username, email)
281 .send({ error: 'User with this username or email already exists.' })
286 const actor = await ActorModel.loadLocalByName(username)
289 .send({ error: 'Another actor (account/channel) with this name already exists.' })
297 async function checkUserExist (finder: () => Bluebird<UserModel>, res: express.Response, abortResponse = true) {
298 const user = await finder()
301 if (abortResponse === true) {
303 .send({ error: 'User not found' })
310 res.locals.user = user