]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/users.ts
Update readme, architecture
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
CommitLineData
b60e5f38 1import { body, param } from 'express-validator/check'
69818c93
C
2import 'express-validator'
3import * as express from 'express'
0a6658fd
C
4import * as Promise from 'bluebird'
5import * as validator from 'validator'
69818c93 6
e02643f3 7import { database as db } from '../../initializers/database'
65fcc311 8import { checkErrors } from './utils'
b60e5f38
C
9import {
10 isSignupAllowed,
11 logger,
12 isUserUsernameValid,
13 isUserPasswordValid,
14 isUserVideoQuotaValid,
15 isUserDisplayNSFWValid,
954605a8
C
16 isIdOrUUIDValid,
17 isUserRoleValid
b60e5f38 18} from '../../helpers'
77a5501f 19import { UserInstance, VideoInstance } from '../../models'
9bd26629 20
b60e5f38 21const usersAddValidator = [
563d032e 22 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
b60e5f38
C
23 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
24 body('email').isEmail().withMessage('Should have a valid email'),
25 body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
954605a8 26 body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
9bd26629 27
b60e5f38
C
28 (req: express.Request, res: express.Response, next: express.NextFunction) => {
29 logger.debug('Checking usersAdd parameters', { parameters: req.body })
9bd26629 30
b60e5f38
C
31 checkErrors(req, res, () => {
32 checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
33 })
34 }
35]
6fcd19ba 36
b60e5f38
C
37const usersRegisterValidator = [
38 body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
39 body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
40 body('email').isEmail().withMessage('Should have a valid email'),
77a5501f 41
b60e5f38
C
42 (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking usersRegister parameters', { parameters: req.body })
77a5501f 44
b60e5f38
C
45 checkErrors(req, res, () => {
46 checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
47 })
48 }
49]
9bd26629 50
b60e5f38
C
51const usersRemoveValidator = [
52 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
9bd26629 53
b60e5f38
C
54 (req: express.Request, res: express.Response, next: express.NextFunction) => {
55 logger.debug('Checking usersRemove parameters', { parameters: req.params })
9bd26629 56
b60e5f38
C
57 checkErrors(req, res, () => {
58 checkUserExists(req.params.id, res, (err, user) => {
59 if (err) {
60 logger.error('Error in usersRemoveValidator.', err)
61 return res.sendStatus(500)
62 }
9bd26629 63
b60e5f38
C
64 if (user.username === 'root') {
65 return res.status(400)
66 .send({ error: 'Cannot remove the root user' })
67 .end()
68 }
af1068ce 69
b60e5f38
C
70 return next()
71 })
77a5501f 72 })
b60e5f38
C
73 }
74]
8094a898 75
b60e5f38
C
76const usersUpdateValidator = [
77 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
78 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
79 body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
954605a8 80 body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
8094a898 81
b60e5f38
C
82 (req: express.Request, res: express.Response, next: express.NextFunction) => {
83 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
9bd26629 84
b60e5f38
C
85 checkErrors(req, res, () => {
86 checkUserExists(req.params.id, res, next)
87 })
88 }
89]
9bd26629 90
b60e5f38
C
91const usersUpdateMeValidator = [
92 body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
93 body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
94 body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
9bd26629 95
b60e5f38
C
96 (req: express.Request, res: express.Response, next: express.NextFunction) => {
97 // TODO: Add old password verification
98 logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
8094a898 99
b60e5f38
C
100 checkErrors(req, res, next)
101 }
102]
8094a898 103
b60e5f38
C
104const usersGetValidator = [
105 param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
d38b8281 106
b60e5f38
C
107 (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 checkErrors(req, res, () => {
109 checkUserExists(req.params.id, res, next)
110 })
111 }
112]
d38b8281 113
b60e5f38 114const usersVideoRatingValidator = [
72c7248b 115 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
0a6658fd 116
b60e5f38
C
117 (req: express.Request, res: express.Response, next: express.NextFunction) => {
118 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
0a6658fd 119
b60e5f38
C
120 checkErrors(req, res, () => {
121 let videoPromise: Promise<VideoInstance>
6fcd19ba 122
b60e5f38
C
123 if (validator.isUUID(req.params.videoId)) {
124 videoPromise = db.Video.loadByUUID(req.params.videoId)
125 } else {
126 videoPromise = db.Video.load(req.params.videoId)
127 }
d38b8281 128
b60e5f38
C
129 videoPromise
130 .then(video => {
131 if (!video) {
132 return res.status(404)
133 .json({ error: 'Video not found' })
134 .end()
135 }
136
137 return next()
138 })
139 .catch(err => {
140 logger.error('Error in user request validator.', err)
141 return res.sendStatus(500)
142 })
143 })
144 }
145]
146
147const ensureUserRegistrationAllowed = [
148 (req: express.Request, res: express.Response, next: express.NextFunction) => {
149 isSignupAllowed().then(allowed => {
150 if (allowed === false) {
151 return res.status(403)
152 .send({ error: 'User registration is not enabled or user limit is reached.' })
153 .end()
154 }
291e8d3e 155
b60e5f38
C
156 return next()
157 })
158 }
159]
291e8d3e 160
9bd26629
C
161// ---------------------------------------------------------------------------
162
65fcc311
C
163export {
164 usersAddValidator,
77a5501f 165 usersRegisterValidator,
65fcc311
C
166 usersRemoveValidator,
167 usersUpdateValidator,
8094a898 168 usersUpdateMeValidator,
291e8d3e 169 usersVideoRatingValidator,
8094a898
C
170 ensureUserRegistrationAllowed,
171 usersGetValidator
172}
173
174// ---------------------------------------------------------------------------
175
77a5501f 176function checkUserExists (id: number, res: express.Response, callback: (err: Error, user: UserInstance) => void) {
8094a898
C
177 db.User.loadById(id)
178 .then(user => {
bfb3a98f
C
179 if (!user) {
180 return res.status(404)
181 .send({ error: 'User not found' })
182 .end()
183 }
8094a898
C
184
185 res.locals.user = user
bfb3a98f 186 return callback(null, user)
8094a898
C
187 })
188 .catch(err => {
189 logger.error('Error in user request validator.', err)
190 return res.sendStatus(500)
191 })
65fcc311 192}
77a5501f
C
193
194function checkUserDoesNotAlreadyExist (username: string, email: string, res: express.Response, callback: () => void) {
195 db.User.loadByUsernameOrEmail(username, email)
196 .then(user => {
bfb3a98f
C
197 if (user) {
198 return res.status(409)
563d032e 199 .send({ error: 'User with this username of email already exists.' })
bfb3a98f
C
200 .end()
201 }
77a5501f 202
bfb3a98f 203 return callback()
77a5501f
C
204 })
205 .catch(err => {
206 logger.error('Error in usersAdd request validator.', err)
207 return res.sendStatus(500)
208 })
209}