PeerTube/server/middlewares/validators/account.ts

65 lines
2.1 KiB
TypeScript
Raw Normal View History

2017-11-09 17:51:58 +01:00
import * as express from 'express'
2017-11-13 17:39:41 +01:00
import { param } from 'express-validator/check'
2018-02-21 16:44:18 +01:00
import {
isAccountIdExist,
isAccountIdValid,
2018-02-21 16:44:18 +01:00
isAccountNameValid,
isAccountNameWithHostExist,
isLocalAccountNameExist
} from '../../helpers/custom-validators/accounts'
2017-12-28 11:16:08 +01:00
import { logger } from '../../helpers/logger'
2017-11-27 17:30:46 +01:00
import { areValidationErrors } from './utils'
import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
2017-11-09 17:51:58 +01:00
const localAccountValidator = [
2017-11-14 17:31:26 +01:00
param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
2017-11-09 17:51:58 +01:00
2017-11-27 17:30:46 +01:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2017-11-09 17:51:58 +01:00
logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
2017-11-27 17:30:46 +01:00
if (areValidationErrors(req, res)) return
if (!await isLocalAccountNameExist(req.params.name, res)) return
return next()
2017-11-09 17:51:58 +01:00
}
]
2018-01-03 16:38:50 +01:00
const accountsGetValidator = [
param('id').custom(isAccountIdValid).withMessage('Should have a valid id/uuid/name/name with host'),
2018-01-03 16:38:50 +01:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking accountsGetValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
let accountFetched = false
if (isIdOrUUIDValid(req.params.id)) accountFetched = await isAccountIdExist(req.params.id, res, false)
if (!accountFetched) accountFetched = await isAccountNameWithHostExist(req.params.id, res, true)
if (!accountFetched) return
2018-01-03 16:38:50 +01:00
return next()
}
]
2018-02-21 16:44:18 +01:00
const accountsNameWithHostGetValidator = [
param('nameWithHost').exists().withMessage('Should have an account name with host'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params })
if (areValidationErrors(req, res)) return
if (!await isAccountNameWithHostExist(req.params.nameWithHost, res)) return
return next()
}
]
2017-11-09 17:51:58 +01:00
// ---------------------------------------------------------------------------
export {
2018-01-03 16:38:50 +01:00
localAccountValidator,
2018-02-21 16:44:18 +01:00
accountsGetValidator,
accountsNameWithHostGetValidator
2017-11-09 17:51:58 +01:00
}