1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param } from 'express-validator/check'
4 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
6 isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
8 } from '../../helpers/custom-validators/users'
9 import { isVideoExist } from '../../helpers/custom-validators/videos'
10 import { logger } from '../../helpers/logger'
11 import { isSignupAllowed } from '../../helpers/utils'
12 import { UserModel } from '../../models/account/user'
13 import { areValidationErrors } from './utils'
15 const usersAddValidator = [
16 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
17 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
18 body('email').isEmail().withMessage('Should have a valid email'),
19 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
20 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking usersAdd parameters', { parameters: req.body })
25 if (areValidationErrors(req, res)) return
26 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
32 const usersRegisterValidator = [
33 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
34 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
35 body('email').isEmail().withMessage('Should have a valid email'),
37 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 logger.debug('Checking usersRegister parameters', { parameters: req.body })
40 if (areValidationErrors(req, res)) return
41 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
47 const usersRemoveValidator = [
48 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking usersRemove parameters', { parameters: req.params })
53 if (areValidationErrors(req, res)) return
54 if (!await checkUserIdExist(req.params.id, res)) return
56 const user = res.locals.user
57 if (user.username === 'root') {
58 return res.status(400)
59 .send({ error: 'Cannot remove the root user' })
67 const usersUpdateValidator = [
68 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
69 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
70 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
71 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
76 if (areValidationErrors(req, res)) return
77 if (!await checkUserIdExist(req.params.id, res)) return
83 const usersUpdateMeValidator = [
84 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
85 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
86 body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
87 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
89 (req: express.Request, res: express.Response, next: express.NextFunction) => {
90 // TODO: Add old password verification
91 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
93 if (areValidationErrors(req, res)) return
99 const usersGetValidator = [
100 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
102 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
103 logger.debug('Checking usersGet parameters', { parameters: req.body })
105 if (areValidationErrors(req, res)) return
106 if (!await checkUserIdExist(req.params.id, res)) return
112 const usersVideoRatingValidator = [
113 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
115 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
116 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
118 if (areValidationErrors(req, res)) return
119 if (!await isVideoExist(req.params.videoId, res)) return
125 const ensureUserRegistrationAllowed = [
126 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 const allowed = await isSignupAllowed()
128 if (allowed === false) {
129 return res.status(403)
130 .send({ error: 'User registration is not enabled or user limit is reached.' })
138 // ---------------------------------------------------------------------------
142 usersRegisterValidator,
143 usersRemoveValidator,
144 usersUpdateValidator,
145 usersUpdateMeValidator,
146 usersVideoRatingValidator,
147 ensureUserRegistrationAllowed,
151 // ---------------------------------------------------------------------------
153 async function checkUserIdExist (id: number, res: express.Response) {
154 const user = await UserModel.loadById(id)
158 .send({ error: 'User not found' })
164 res.locals.user = user
168 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
169 const user = await UserModel.loadByUsernameOrEmail(username, email)
173 .send({ error: 'User with this username of email already exists.' })