Fix bad to/cc when undo dislike
[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 {
5   isIdOrUUIDValid,
6   isSignupAllowed,
7   isUserDisplayNSFWValid,
8   isUserPasswordValid,
9   isUserRoleValid,
10   isUserUsernameValid,
11   isUserVideoQuotaValid,
12   logger
13 } from '../../helpers'
14 import { isVideoExist } from '../../helpers/custom-validators/videos'
15 import { database as db } from '../../initializers/database'
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
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 })
94
95     if (areValidationErrors(req, res)) return
96
97     return next()
98   }
99 ]
100
101 const usersGetValidator = [
102   param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
103
104   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
105     logger.debug('Checking usersGet parameters', { parameters: req.body })
106
107     if (areValidationErrors(req, res)) return
108     if (!await checkUserIdExist(req.params.id, res)) return
109
110     return next()
111   }
112 ]
113
114 const usersVideoRatingValidator = [
115   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
116
117   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
118     logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
119
120     if (areValidationErrors(req, res)) return
121     if (!await isVideoExist(req.params.videoId, res)) return
122
123     return next()
124   }
125 ]
126
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.' })
133                 .end()
134     }
135
136     return next()
137   }
138 ]
139
140 // ---------------------------------------------------------------------------
141
142 export {
143   usersAddValidator,
144   usersRegisterValidator,
145   usersRemoveValidator,
146   usersUpdateValidator,
147   usersUpdateMeValidator,
148   usersVideoRatingValidator,
149   ensureUserRegistrationAllowed,
150   usersGetValidator
151 }
152
153 // ---------------------------------------------------------------------------
154
155 async function checkUserIdExist (id: number, res: express.Response) {
156   const user = await db.User.loadById(id)
157
158   if (!user) {
159     res.status(404)
160               .send({ error: 'User not found' })
161               .end()
162
163     return false
164   }
165
166   res.locals.user = user
167   return true
168 }
169
170 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
171   const user = await db.User.loadByUsernameOrEmail(username, email)
172
173   if (user) {
174     res.status(409)
175               .send({ error: 'User with this username of email already exists.' })
176               .end()
177     return false
178   }
179
180   return true
181 }