PeerTube/server/middlewares/validators/account.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-11-09 10:51:58 -06:00
import * as express from 'express'
2017-11-13 10:39:41 -06:00
import { param } from 'express-validator/check'
2017-11-09 10:51:58 -06:00
import {
isUserDisplayNSFWValid,
2017-11-13 10:39:41 -06:00
isUserPasswordValid,
2017-11-09 10:51:58 -06:00
isUserRoleValid,
2017-11-13 10:39:41 -06:00
isUserUsernameValid,
isUserVideoQuotaValid,
logger
2017-11-09 10:51:58 -06:00
} from '../../helpers'
2017-11-13 10:39:41 -06:00
import { isAccountNameWithHostValid } from '../../helpers/custom-validators/video-accounts'
import { database as db } from '../../initializers/database'
2017-11-09 10:51:58 -06:00
import { AccountInstance } from '../../models'
2017-11-13 10:39:41 -06:00
import { checkErrors } from './utils'
2017-11-09 10:51:58 -06:00
const localAccountValidator = [
2017-11-13 10:39:41 -06:00
param('nameWithHost').custom(isAccountNameWithHostValid).withMessage('Should have a valid account with domain name (myuser@domain.tld)'),
2017-11-09 10:51:58 -06: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-13 10:39:41 -06:00
function checkLocalAccountExists (nameWithHost: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) {
const [ name, host ] = nameWithHost.split('@')
db.Account.loadLocalAccountByNameAndPod(name, host)
2017-11-09 10:51:58 -06: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)
})
}