diff options
Diffstat (limited to 'server/middlewares/validators/account.ts')
-rw-r--r-- | server/middlewares/validators/account.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts new file mode 100644 index 000000000..5abe942d6 --- /dev/null +++ b/server/middlewares/validators/account.ts | |||
@@ -0,0 +1,53 @@ | |||
1 | import { param } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { database as db } from '../../initializers/database' | ||
5 | import { checkErrors } from './utils' | ||
6 | import { | ||
7 | logger, | ||
8 | isUserUsernameValid, | ||
9 | isUserPasswordValid, | ||
10 | isUserVideoQuotaValid, | ||
11 | isUserDisplayNSFWValid, | ||
12 | isUserRoleValid, | ||
13 | isAccountNameValid | ||
14 | } from '../../helpers' | ||
15 | import { AccountInstance } from '../../models' | ||
16 | |||
17 | const localAccountValidator = [ | ||
18 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), | ||
19 | |||
20 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
21 | logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) | ||
22 | |||
23 | checkErrors(req, res, () => { | ||
24 | checkLocalAccountExists(req.params.name, res, next) | ||
25 | }) | ||
26 | } | ||
27 | ] | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | export { | ||
32 | localAccountValidator | ||
33 | } | ||
34 | |||
35 | // --------------------------------------------------------------------------- | ||
36 | |||
37 | function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) { | ||
38 | db.Account.loadLocalAccountByName(name) | ||
39 | .then(account => { | ||
40 | if (!account) { | ||
41 | return res.status(404) | ||
42 | .send({ error: 'Account not found' }) | ||
43 | .end() | ||
44 | } | ||
45 | |||
46 | res.locals.account = account | ||
47 | return callback(null, account) | ||
48 | }) | ||
49 | .catch(err => { | ||
50 | logger.error('Error in account request validator.', err) | ||
51 | return res.sendStatus(500) | ||
52 | }) | ||
53 | } | ||