]>
Commit | Line | Data |
---|---|---|
e4f97bab | 1 | import * as express from 'express' |
7a7724e6 | 2 | import { param } from 'express-validator/check' |
e4f97bab | 3 | import { |
e4f97bab | 4 | isUserDisplayNSFWValid, |
7a7724e6 | 5 | isUserPasswordValid, |
e4f97bab | 6 | isUserRoleValid, |
7a7724e6 C |
7 | isUserUsernameValid, |
8 | isUserVideoQuotaValid, | |
9 | logger | |
e4f97bab | 10 | } from '../../helpers' |
350e31d6 | 11 | import { isAccountNameValid } from '../../helpers/custom-validators/accounts' |
7a7724e6 | 12 | import { database as db } from '../../initializers/database' |
e4f97bab | 13 | import { AccountInstance } from '../../models' |
7a7724e6 | 14 | import { checkErrors } from './utils' |
e4f97bab C |
15 | |
16 | const localAccountValidator = [ | |
350e31d6 | 17 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), |
e4f97bab C |
18 | |
19 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
20 | logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) | |
21 | ||
22 | checkErrors(req, res, () => { | |
23 | checkLocalAccountExists(req.params.name, res, next) | |
24 | }) | |
25 | } | |
26 | ] | |
27 | ||
28 | // --------------------------------------------------------------------------- | |
29 | ||
30 | export { | |
31 | localAccountValidator | |
32 | } | |
33 | ||
34 | // --------------------------------------------------------------------------- | |
35 | ||
350e31d6 C |
36 | function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) { |
37 | db.Account.loadLocalByName(name) | |
e4f97bab C |
38 | .then(account => { |
39 | if (!account) { | |
40 | return res.status(404) | |
41 | .send({ error: 'Account not found' }) | |
42 | .end() | |
43 | } | |
44 | ||
45 | res.locals.account = account | |
46 | return callback(null, account) | |
47 | }) | |
48 | .catch(err => { | |
49 | logger.error('Error in account request validator.', err) | |
50 | return res.sendStatus(500) | |
51 | }) | |
52 | } |