1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param } from 'express-validator/check'
4 import { isSignupAllowed, logger } from '../../helpers'
5 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
7 isUserDisplayNSFWValid,
12 } from '../../helpers/custom-validators/users'
13 import { isVideoExist } from '../../helpers/custom-validators/videos'
14 import { UserModel } from '../../models/account/user'
15 import { areValidationErrors } from './utils'
17 const usersAddValidator = [
18 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
19 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
20 body('email').isEmail().withMessage('Should have a valid email'),
21 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
22 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
24 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 logger.debug('Checking usersAdd parameters', { parameters: req.body })
27 if (areValidationErrors(req, res)) return
28 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
34 const usersRegisterValidator = [
35 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
36 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
37 body('email').isEmail().withMessage('Should have a valid email'),
39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 logger.debug('Checking usersRegister parameters', { parameters: req.body })
42 if (areValidationErrors(req, res)) return
43 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
49 const usersRemoveValidator = [
50 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking usersRemove parameters', { parameters: req.params })
55 if (areValidationErrors(req, res)) return
56 if (!await checkUserIdExist(req.params.id, res)) return
58 const user = res.locals.user
59 if (user.username === 'root') {
60 return res.status(400)
61 .send({ error: 'Cannot remove the root user' })
69 const usersUpdateValidator = [
70 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
71 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
72 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
73 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
78 if (areValidationErrors(req, res)) return
79 if (!await checkUserIdExist(req.params.id, res)) return
85 const usersUpdateMeValidator = [
86 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
87 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
88 body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
90 (req: express.Request, res: express.Response, next: express.NextFunction) => {
91 // TODO: Add old password verification
92 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
94 if (areValidationErrors(req, res)) return
100 const usersGetValidator = [
101 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
103 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking usersGet parameters', { parameters: req.body })
106 if (areValidationErrors(req, res)) return
107 if (!await checkUserIdExist(req.params.id, res)) return
113 const usersVideoRatingValidator = [
114 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
116 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
117 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
119 if (areValidationErrors(req, res)) return
120 if (!await isVideoExist(req.params.videoId, res)) return
126 const ensureUserRegistrationAllowed = [
127 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 const allowed = await isSignupAllowed()
129 if (allowed === false) {
130 return res.status(403)
131 .send({ error: 'User registration is not enabled or user limit is reached.' })
139 // ---------------------------------------------------------------------------
143 usersRegisterValidator,
144 usersRemoveValidator,
145 usersUpdateValidator,
146 usersUpdateMeValidator,
147 usersVideoRatingValidator,
148 ensureUserRegistrationAllowed,
152 // ---------------------------------------------------------------------------
154 async function checkUserIdExist (id: number, res: express.Response) {
155 const user = await UserModel.loadById(id)
159 .send({ error: 'User not found' })
165 res.locals.user = user
169 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
170 const user = await UserModel.loadByUsernameOrEmail(username, email)
174 .send({ error: 'User with this username of email already exists.' })