1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param } from 'express-validator/check'
7 isUserDisplayNSFWValid,
11 isUserVideoQuotaValid,
13 } from '../../helpers'
14 import { isVideoExist } from '../../helpers/custom-validators/videos'
15 import { database as db } from '../../initializers/database'
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'),
91 (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 // TODO: Add old password verification
93 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
95 if (areValidationErrors(req, res)) return
101 const usersGetValidator = [
102 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
104 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
105 logger.debug('Checking usersGet parameters', { parameters: req.body })
107 if (areValidationErrors(req, res)) return
108 if (!await checkUserIdExist(req.params.id, res)) return
114 const usersVideoRatingValidator = [
115 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
117 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
118 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
120 if (areValidationErrors(req, res)) return
121 if (!await isVideoExist(req.params.videoId, res)) return
127 const ensureUserRegistrationAllowed = [
128 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
129 const allowed = await isSignupAllowed()
130 if (allowed === false) {
131 return res.status(403)
132 .send({ error: 'User registration is not enabled or user limit is reached.' })
140 // ---------------------------------------------------------------------------
144 usersRegisterValidator,
145 usersRemoveValidator,
146 usersUpdateValidator,
147 usersUpdateMeValidator,
148 usersVideoRatingValidator,
149 ensureUserRegistrationAllowed,
153 // ---------------------------------------------------------------------------
155 async function checkUserIdExist (id: number, res: express.Response) {
156 const user = await db.User.loadById(id)
160 .send({ error: 'User not found' })
166 res.locals.user = user
170 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
171 const user = await db.User.loadByUsernameOrEmail(username, email)
175 .send({ error: 'User with this username of email already exists.' })