Add comments controller
[oweals/peertube.git] / server / middlewares / validators / users.ts
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'
6 import {
7   isUserDisplayNSFWValid,
8   isUserAutoPlayVideoValid,
9   isUserPasswordValid,
10   isUserRoleValid,
11   isUserUsernameValid,
12   isUserVideoQuotaValid
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'
17
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'),
24
25   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26     logger.debug('Checking usersAdd parameters', { parameters: req.body })
27
28     if (areValidationErrors(req, res)) return
29     if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
30
31     return next()
32   }
33 ]
34
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'),
39
40   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41     logger.debug('Checking usersRegister parameters', { parameters: req.body })
42
43     if (areValidationErrors(req, res)) return
44     if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
45
46     return next()
47   }
48 ]
49
50 const usersRemoveValidator = [
51   param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
52
53   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
54     logger.debug('Checking usersRemove parameters', { parameters: req.params })
55
56     if (areValidationErrors(req, res)) return
57     if (!await checkUserIdExist(req.params.id, res)) return
58
59     const user = res.locals.user
60     if (user.username === 'root') {
61       return res.status(400)
62                 .send({ error: 'Cannot remove the root user' })
63                 .end()
64     }
65
66     return next()
67   }
68 ]
69
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'),
75
76   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77     logger.debug('Checking usersUpdate parameters', { parameters: req.body })
78
79     if (areValidationErrors(req, res)) return
80     if (!await checkUserIdExist(req.params.id, res)) return
81
82     return next()
83   }
84 ]
85
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'),
91
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 })
95
96     if (areValidationErrors(req, res)) return
97
98     return next()
99   }
100 ]
101
102 const usersGetValidator = [
103   param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
104
105   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
106     logger.debug('Checking usersGet parameters', { parameters: req.body })
107
108     if (areValidationErrors(req, res)) return
109     if (!await checkUserIdExist(req.params.id, res)) return
110
111     return next()
112   }
113 ]
114
115 const usersVideoRatingValidator = [
116   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
117
118   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
119     logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
120
121     if (areValidationErrors(req, res)) return
122     if (!await isVideoExist(req.params.videoId, res)) return
123
124     return next()
125   }
126 ]
127
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.' })
134                 .end()
135     }
136
137     return next()
138   }
139 ]
140
141 // ---------------------------------------------------------------------------
142
143 export {
144   usersAddValidator,
145   usersRegisterValidator,
146   usersRemoveValidator,
147   usersUpdateValidator,
148   usersUpdateMeValidator,
149   usersVideoRatingValidator,
150   ensureUserRegistrationAllowed,
151   usersGetValidator
152 }
153
154 // ---------------------------------------------------------------------------
155
156 async function checkUserIdExist (id: number, res: express.Response) {
157   const user = await UserModel.loadById(id)
158
159   if (!user) {
160     res.status(404)
161               .send({ error: 'User not found' })
162               .end()
163
164     return false
165   }
166
167   res.locals.user = user
168   return true
169 }
170
171 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
172   const user = await UserModel.loadByUsernameOrEmail(username, email)
173
174   if (user) {
175     res.status(409)
176               .send({ error: 'User with this username of email already exists.' })
177               .end()
178     return false
179   }
180
181   return true
182 }