1
0
Fork 0
peertube/server/middlewares/validators/account.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-11-09 11:51:58 -05:00
import * as express from 'express'
2017-11-13 11:39:41 -05:00
import { param } from 'express-validator/check'
2017-11-09 11:51:58 -05:00
import {
isUserDisplayNSFWValid,
2017-11-13 11:39:41 -05:00
isUserPasswordValid,
2017-11-09 11:51:58 -05:00
isUserRoleValid,
2017-11-13 11:39:41 -05:00
isUserUsernameValid,
isUserVideoQuotaValid,
logger
2017-11-09 11:51:58 -05:00
} from '../../helpers'
2017-11-14 11:31:26 -05:00
import { isAccountNameValid } from '../../helpers/custom-validators/accounts'
2017-11-13 11:39:41 -05:00
import { database as db } from '../../initializers/database'
2017-11-09 11:51:58 -05:00
import { AccountInstance } from '../../models'
2017-11-13 11:39:41 -05:00
import { checkErrors } from './utils'
2017-11-09 11:51:58 -05:00
const localAccountValidator = [
2017-11-14 11:31:26 -05:00
param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
2017-11-09 11:51:58 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
checkErrors(req, res, () => {
checkLocalAccountExists(req.params.name, res, next)
})
}
]
// ---------------------------------------------------------------------------
export {
localAccountValidator
}
// ---------------------------------------------------------------------------
2017-11-14 11:31:26 -05:00
function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) {
db.Account.loadLocalByName(name)
2017-11-09 11:51:58 -05:00
.then(account => {
if (!account) {
return res.status(404)
.send({ error: 'Account not found' })
.end()
}
res.locals.account = account
return callback(null, account)
})
.catch(err => {
logger.error('Error in account request validator.', err)
return res.sendStatus(500)
})
}