PeerTube/server/middlewares/validators/users.ts

210 lines
6.7 KiB
TypeScript
Raw Normal View History

2017-09-15 05:17:08 -05:00
import { body, param } from 'express-validator/check'
2017-06-10 15:15:25 -05:00
import 'express-validator'
import * as express from 'express'
import * as Promise from 'bluebird'
import * as validator from 'validator'
2017-06-10 15:15:25 -05:00
2017-05-22 13:58:25 -05:00
import { database as db } from '../../initializers/database'
2017-05-15 15:22:03 -05:00
import { checkErrors } from './utils'
2017-09-15 05:17:08 -05:00
import {
isSignupAllowed,
logger,
isUserUsernameValid,
isUserPasswordValid,
isUserVideoQuotaValid,
isUserDisplayNSFWValid,
isIdOrUUIDValid,
isUserRoleValid
2017-09-15 05:17:08 -05:00
} from '../../helpers'
2017-09-06 09:35:40 -05:00
import { UserInstance, VideoInstance } from '../../models'
2017-09-15 05:17:08 -05:00
const usersAddValidator = [
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
body('email').isEmail().withMessage('Should have a valid email'),
body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
body('role').custom(isUserRoleValid).withMessage('Should have a valid role'),
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking usersAdd parameters', { parameters: req.body })
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
})
}
]
2017-09-15 05:17:08 -05:00
const usersRegisterValidator = [
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
body('email').isEmail().withMessage('Should have a valid email'),
2017-09-06 09:35:40 -05:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking usersRegister parameters', { parameters: req.body })
2017-09-06 09:35:40 -05:00
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
})
}
]
2017-09-15 05:17:08 -05:00
const usersRemoveValidator = [
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking usersRemove parameters', { parameters: req.params })
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkUserExists(req.params.id, res, (err, user) => {
if (err) {
logger.error('Error in usersRemoveValidator.', err)
return res.sendStatus(500)
}
2017-09-15 05:17:08 -05:00
if (user.username === 'root') {
return res.status(400)
.send({ error: 'Cannot remove the root user' })
.end()
}
2016-10-07 08:32:09 -05:00
2017-09-15 05:17:08 -05:00
return next()
})
2017-09-06 09:35:40 -05:00
})
2017-09-15 05:17:08 -05:00
}
]
2017-09-05 14:29:39 -05:00
2017-09-15 05:17:08 -05:00
const usersUpdateValidator = [
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'),
2017-09-05 14:29:39 -05:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking usersUpdate parameters', { parameters: req.body })
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkUserExists(req.params.id, res, next)
})
}
]
2017-09-15 05:17:08 -05:00
const usersUpdateMeValidator = [
body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
body('displayNSFW').optional().custom(isUserDisplayNSFWValid).withMessage('Should have a valid display Not Safe For Work attribute'),
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
// TODO: Add old password verification
logger.debug('Checking usersUpdateMe parameters', { parameters: req.body })
2017-09-05 14:29:39 -05:00
2017-09-15 05:17:08 -05:00
checkErrors(req, res, next)
}
]
2017-09-05 14:29:39 -05:00
2017-09-15 05:17:08 -05:00
const usersGetValidator = [
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
2017-03-08 14:35:43 -06:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
checkErrors(req, res, () => {
checkUserExists(req.params.id, res, next)
})
}
]
2017-03-08 14:35:43 -06:00
2017-09-15 05:17:08 -05:00
const usersVideoRatingValidator = [
2017-10-24 12:41:09 -05:00
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
let videoPromise: Promise<VideoInstance>
2017-09-15 05:17:08 -05:00
if (validator.isUUID(req.params.videoId)) {
videoPromise = db.Video.loadByUUID(req.params.videoId)
} else {
videoPromise = db.Video.load(req.params.videoId)
}
2017-03-08 14:35:43 -06:00
2017-09-15 05:17:08 -05:00
videoPromise
.then(video => {
if (!video) {
return res.status(404)
.json({ error: 'Video not found' })
.end()
}
return next()
})
.catch(err => {
logger.error('Error in user request validator.', err)
return res.sendStatus(500)
})
})
}
]
const ensureUserRegistrationAllowed = [
(req: express.Request, res: express.Response, next: express.NextFunction) => {
isSignupAllowed().then(allowed => {
if (allowed === false) {
return res.status(403)
.send({ error: 'User registration is not enabled or user limit is reached.' })
.end()
}
2017-09-15 05:17:08 -05:00
return next()
})
}
]
// ---------------------------------------------------------------------------
2017-05-15 15:22:03 -05:00
export {
usersAddValidator,
2017-09-06 09:35:40 -05:00
usersRegisterValidator,
2017-05-15 15:22:03 -05:00
usersRemoveValidator,
usersUpdateValidator,
2017-09-05 14:29:39 -05:00
usersUpdateMeValidator,
usersVideoRatingValidator,
2017-09-05 14:29:39 -05:00
ensureUserRegistrationAllowed,
usersGetValidator
}
// ---------------------------------------------------------------------------
2017-09-06 09:35:40 -05:00
function checkUserExists (id: number, res: express.Response, callback: (err: Error, user: UserInstance) => void) {
2017-09-05 14:29:39 -05:00
db.User.loadById(id)
.then(user => {
if (!user) {
return res.status(404)
.send({ error: 'User not found' })
.end()
}
2017-09-05 14:29:39 -05:00
res.locals.user = user
return callback(null, user)
2017-09-05 14:29:39 -05:00
})
.catch(err => {
logger.error('Error in user request validator.', err)
return res.sendStatus(500)
})
2017-05-15 15:22:03 -05:00
}
2017-09-06 09:35:40 -05:00
function checkUserDoesNotAlreadyExist (username: string, email: string, res: express.Response, callback: () => void) {
db.User.loadByUsernameOrEmail(username, email)
.then(user => {
if (user) {
return res.status(409)
.send({ error: 'User already exists.' })
.end()
}
2017-09-06 09:35:40 -05:00
return callback()
2017-09-06 09:35:40 -05:00
})
.catch(err => {
logger.error('Error in usersAdd request validator.', err)
return res.sendStatus(500)
})
}