2017-09-15 12:17:08 +02:00
|
|
|
import { body, param } from 'express-validator/check'
|
2017-06-10 22:15:25 +02:00
|
|
|
import 'express-validator'
|
|
|
|
import * as express from 'express'
|
2017-07-11 16:01:56 +02:00
|
|
|
import * as Promise from 'bluebird'
|
|
|
|
import * as validator from 'validator'
|
2017-06-10 22:15:25 +02:00
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-05-15 22:22:03 +02:00
|
|
|
import { checkErrors } from './utils'
|
2017-09-15 12:17:08 +02:00
|
|
|
import {
|
|
|
|
isSignupAllowed,
|
|
|
|
logger,
|
|
|
|
isUserUsernameValid,
|
|
|
|
isUserPasswordValid,
|
|
|
|
isUserVideoQuotaValid,
|
|
|
|
isUserDisplayNSFWValid,
|
|
|
|
isVideoIdOrUUIDValid
|
|
|
|
} from '../../helpers'
|
2017-09-06 16:35:40 +02:00
|
|
|
import { UserInstance, VideoInstance } from '../../models'
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02: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'),
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersAdd parameters', { parameters: req.body })
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2017-07-05 13:26:25 +02:00
|
|
|
|
2017-09-15 12:17:08 +02: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 16:35:40 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersRegister parameters', { parameters: req.body })
|
2017-09-06 16:35:40 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkUserDoesNotAlreadyExist(req.body.username, req.body.email, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
const usersRemoveValidator = [
|
|
|
|
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersRemove parameters', { parameters: req.params })
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkUserExists(req.params.id, res, (err, user) => {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Error in usersRemoveValidator.', err)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
if (user.username === 'root') {
|
|
|
|
return res.status(400)
|
|
|
|
.send({ error: 'Cannot remove the root user' })
|
|
|
|
.end()
|
|
|
|
}
|
2016-10-07 15:32:09 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
return next()
|
|
|
|
})
|
2017-09-06 16:35:40 +02:00
|
|
|
})
|
2017-09-15 12:17:08 +02:00
|
|
|
}
|
|
|
|
]
|
2017-09-05 21:29:39 +02:00
|
|
|
|
2017-09-15 12:17:08 +02: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'),
|
2017-09-05 21:29:39 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersUpdate parameters', { parameters: req.body })
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkUserExists(req.params.id, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02: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'),
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2017-09-15 12:17:08 +02: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 21:29:39 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
checkErrors(req, res, next)
|
|
|
|
}
|
|
|
|
]
|
2017-09-05 21:29:39 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
const usersGetValidator = [
|
|
|
|
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
|
2017-03-08 21:35:43 +01:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
checkErrors(req, res, () => {
|
|
|
|
checkUserExists(req.params.id, res, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2017-03-08 21:35:43 +01:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
const usersVideoRatingValidator = [
|
|
|
|
param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
|
2017-07-11 16:01:56 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
|
2017-07-11 16:01:56 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
checkErrors(req, res, () => {
|
|
|
|
let videoPromise: Promise<VideoInstance>
|
2017-07-05 13:26:25 +02:00
|
|
|
|
2017-09-15 12:17:08 +02: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 21:35:43 +01:00
|
|
|
|
2017-09-15 12:17:08 +02: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-07-25 20:17:28 +02:00
|
|
|
|
2017-09-15 12:17:08 +02:00
|
|
|
return next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2017-07-25 20:17:28 +02:00
|
|
|
|
2016-08-04 22:32:36 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
usersAddValidator,
|
2017-09-06 16:35:40 +02:00
|
|
|
usersRegisterValidator,
|
2017-05-15 22:22:03 +02:00
|
|
|
usersRemoveValidator,
|
|
|
|
usersUpdateValidator,
|
2017-09-05 21:29:39 +02:00
|
|
|
usersUpdateMeValidator,
|
2017-07-25 20:17:28 +02:00
|
|
|
usersVideoRatingValidator,
|
2017-09-05 21:29:39 +02:00
|
|
|
ensureUserRegistrationAllowed,
|
|
|
|
usersGetValidator
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-09-06 16:35:40 +02:00
|
|
|
function checkUserExists (id: number, res: express.Response, callback: (err: Error, user: UserInstance) => void) {
|
2017-09-05 21:29:39 +02:00
|
|
|
db.User.loadById(id)
|
|
|
|
.then(user => {
|
2017-09-14 17:06:31 +02:00
|
|
|
if (!user) {
|
|
|
|
return res.status(404)
|
|
|
|
.send({ error: 'User not found' })
|
|
|
|
.end()
|
|
|
|
}
|
2017-09-05 21:29:39 +02:00
|
|
|
|
|
|
|
res.locals.user = user
|
2017-09-14 17:06:31 +02:00
|
|
|
return callback(null, user)
|
2017-09-05 21:29:39 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Error in user request validator.', err)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
})
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
2017-09-06 16:35:40 +02:00
|
|
|
|
|
|
|
function checkUserDoesNotAlreadyExist (username: string, email: string, res: express.Response, callback: () => void) {
|
|
|
|
db.User.loadByUsernameOrEmail(username, email)
|
|
|
|
.then(user => {
|
2017-09-14 17:06:31 +02:00
|
|
|
if (user) {
|
|
|
|
return res.status(409)
|
|
|
|
.send({ error: 'User already exists.' })
|
|
|
|
.end()
|
|
|
|
}
|
2017-09-06 16:35:40 +02:00
|
|
|
|
2017-09-14 17:06:31 +02:00
|
|
|
return callback()
|
2017-09-06 16:35:40 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Error in usersAdd request validator.', err)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
})
|
|
|
|
}
|