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,
8 isUserAutoPlayVideoValid,
13 } from '../../helpers/custom-validators/users'
14 import { isVideoExist } from '../../helpers/custom-validators/videos'
15 import { UserModel } from '../../models/account/user'
16 import { areValidationErrors } from './utils'
18 const usersAddValidator = [
19 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
20 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
21 body('email').isEmail().withMessage('Should have a valid email'),
22 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
23 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking usersAdd parameters', { parameters: req.body })
28 if (areValidationErrors(req, res)) return
29 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
35 const usersRegisterValidator = [
36 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
37 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
38 body('email').isEmail().withMessage('Should have a valid email'),
40 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 logger.debug('Checking usersRegister parameters', { parameters: req.body })
43 if (areValidationErrors(req, res)) return
44 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
50 const usersRemoveValidator = [
51 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
53 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
54 logger.debug('Checking usersRemove parameters', { parameters: req.params })
56 if (areValidationErrors(req, res)) return
57 if (!await checkUserIdExist(req.params.id, res)) return
59 const user = res.locals.user
60 if (user.username === 'root') {
61 return res.status(400)
62 .send({ error: 'Cannot remove the root user' })
70 const usersUpdateValidator = [
71 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
72 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
73 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
74 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
79 if (areValidationErrors(req, res)) return
80 if (!await checkUserIdExist(req.params.id, res)) return
86 const usersUpdateMeValidator = [
87 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
88 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
89 body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
90 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
92 (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 // TODO: Add old password verification
94 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
96 if (areValidationErrors(req, res)) return
102 const usersGetValidator = [
103 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
105 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
106 logger.debug('Checking usersGet parameters', { parameters: req.body })
108 if (areValidationErrors(req, res)) return
109 if (!await checkUserIdExist(req.params.id, res)) return
115 const usersVideoRatingValidator = [
116 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
118 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
119 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
121 if (areValidationErrors(req, res)) return
122 if (!await isVideoExist(req.params.videoId, res)) return
128 const ensureUserRegistrationAllowed = [
129 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 const allowed = await isSignupAllowed()
131 if (allowed === false) {
132 return res.status(403)
133 .send({ error: 'User registration is not enabled or user limit is reached.' })
141 // ---------------------------------------------------------------------------
145 usersRegisterValidator,
146 usersRemoveValidator,
147 usersUpdateValidator,
148 usersUpdateMeValidator,
149 usersVideoRatingValidator,
150 ensureUserRegistrationAllowed,
154 // ---------------------------------------------------------------------------
156 async function checkUserIdExist (id: number, res: express.Response) {
157 const user = await UserModel.loadById(id)
161 .send({ error: 'User not found' })
167 res.locals.user = user
171 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
172 const user = await UserModel.loadByUsernameOrEmail(username, email)
176 .send({ error: 'User with this username of email already exists.' })