]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/users.ts
db40a5c889e09cf40d1b245b7ffd06ef94dfd534
[github/Chocobozzz/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 { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
5 import {
6 isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
7 isUserVideoQuotaValid
8 } from '../../helpers/custom-validators/users'
9 import { isVideoExist } from '../../helpers/custom-validators/videos'
10 import { logger } from '../../helpers/logger'
11 import { isSignupAllowed } from '../../helpers/utils'
12 import { UserModel } from '../../models/account/user'
13 import { areValidationErrors } from './utils'
14
15 const usersAddValidator = [
16 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
17 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
18 body('email').isEmail().withMessage('Should have a valid email'),
19 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
20 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
21
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking usersAdd parameters', { parameters: req.body })
24
25 if (areValidationErrors(req, res)) return
26 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
27
28 return next()
29 }
30 ]
31
32 const usersRegisterValidator = [
33 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
34 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
35 body('email').isEmail().withMessage('Should have a valid email'),
36
37 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 logger.debug('Checking usersRegister parameters', { parameters: req.body })
39
40 if (areValidationErrors(req, res)) return
41 if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
42
43 return next()
44 }
45 ]
46
47 const usersRemoveValidator = [
48 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
49
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking usersRemove parameters', { parameters: req.params })
52
53 if (areValidationErrors(req, res)) return
54 if (!await checkUserIdExist(req.params.id, res)) return
55
56 const user = res.locals.user
57 if (user.username === 'root') {
58 return res.status(400)
59 .send({ error: 'Cannot remove the root user' })
60 .end()
61 }
62
63 return next()
64 }
65 ]
66
67 const usersUpdateValidator = [
68 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
69 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
70 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
71 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
72
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
75
76 if (areValidationErrors(req, res)) return
77 if (!await checkUserIdExist(req.params.id, res)) return
78
79 return next()
80 }
81 ]
82
83 const usersUpdateMeValidator = [
84 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
85 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
86 body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
87 body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
88
89 (req: express.Request, res: express.Response, next: express.NextFunction) => {
90 // TODO: Add old password verification
91 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
92
93 if (areValidationErrors(req, res)) return
94
95 return next()
96 }
97 ]
98
99 const usersGetValidator = [
100 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
101
102 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
103 logger.debug('Checking usersGet parameters', { parameters: req.body })
104
105 if (areValidationErrors(req, res)) return
106 if (!await checkUserIdExist(req.params.id, res)) return
107
108 return next()
109 }
110 ]
111
112 const usersVideoRatingValidator = [
113 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
114
115 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
116 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
117
118 if (areValidationErrors(req, res)) return
119 if (!await isVideoExist(req.params.videoId, res)) return
120
121 return next()
122 }
123 ]
124
125 const ensureUserRegistrationAllowed = [
126 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 const allowed = await isSignupAllowed()
128 if (allowed === false) {
129 return res.status(403)
130 .send({ error: 'User registration is not enabled or user limit is reached.' })
131 .end()
132 }
133
134 return next()
135 }
136 ]
137
138 // ---------------------------------------------------------------------------
139
140 export {
141 usersAddValidator,
142 usersRegisterValidator,
143 usersRemoveValidator,
144 usersUpdateValidator,
145 usersUpdateMeValidator,
146 usersVideoRatingValidator,
147 ensureUserRegistrationAllowed,
148 usersGetValidator
149 }
150
151 // ---------------------------------------------------------------------------
152
153 async function checkUserIdExist (id: number, res: express.Response) {
154 const user = await UserModel.loadById(id)
155
156 if (!user) {
157 res.status(404)
158 .send({ error: 'User not found' })
159 .end()
160
161 return false
162 }
163
164 res.locals.user = user
165 return true
166 }
167
168 async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
169 const user = await UserModel.loadByUsernameOrEmail(username, email)
170
171 if (user) {
172 res.status(409)
173 .send({ error: 'User with this username of email already exists.' })
174 .end()
175 return false
176 }
177
178 return true
179 }