aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/helpers/custom-validators/accounts.ts28
-rw-r--r--server/middlewares/validators/account.ts12
2 files changed, 27 insertions, 13 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index cc8641d6b..00dea9039 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -4,16 +4,21 @@ import 'express-validator'
4import * as validator from 'validator' 4import * as validator from 'validator'
5import { AccountModel } from '../../models/account/account' 5import { AccountModel } from '../../models/account/account'
6import { isUserDescriptionValid, isUserUsernameValid } from './users' 6import { isUserDescriptionValid, isUserUsernameValid } from './users'
7import { exists } from './misc'
7 8
8function isAccountNameValid (value: string) { 9function isAccountNameValid (value: string) {
9 return isUserUsernameValid(value) 10 return isUserUsernameValid(value)
10} 11}
11 12
13function isAccountIdValid (value: string) {
14 return exists(value)
15}
16
12function isAccountDescriptionValid (value: string) { 17function isAccountDescriptionValid (value: string) {
13 return isUserDescriptionValid(value) 18 return isUserDescriptionValid(value)
14} 19}
15 20
16function isAccountIdExist (id: number | string, res: Response) { 21function isAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
17 let promise: Bluebird<AccountModel> 22 let promise: Bluebird<AccountModel>
18 23
19 if (validator.isInt('' + id)) { 24 if (validator.isInt('' + id)) {
@@ -22,32 +27,34 @@ function isAccountIdExist (id: number | string, res: Response) {
22 promise = AccountModel.loadByUUID('' + id) 27 promise = AccountModel.loadByUUID('' + id)
23 } 28 }
24 29
25 return isAccountExist(promise, res) 30 return isAccountExist(promise, res, sendNotFound)
26} 31}
27 32
28function isLocalAccountNameExist (name: string, res: Response) { 33function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
29 const promise = AccountModel.loadLocalByName(name) 34 const promise = AccountModel.loadLocalByName(name)
30 35
31 return isAccountExist(promise, res) 36 return isAccountExist(promise, res, sendNotFound)
32} 37}
33 38
34function isAccountNameWithHostExist (nameWithDomain: string, res: Response) { 39function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
35 const [ accountName, host ] = nameWithDomain.split('@') 40 const [ accountName, host ] = nameWithDomain.split('@')
36 41
37 let promise: Bluebird<AccountModel> 42 let promise: Bluebird<AccountModel>
38 if (!host) promise = AccountModel.loadLocalByName(accountName) 43 if (!host) promise = AccountModel.loadLocalByName(accountName)
39 else promise = AccountModel.loadLocalByNameAndHost(accountName, host) 44 else promise = AccountModel.loadLocalByNameAndHost(accountName, host)
40 45
41 return isAccountExist(promise, res) 46 return isAccountExist(promise, res, sendNotFound)
42} 47}
43 48
44async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { 49async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
45 const account = await p 50 const account = await p
46 51
47 if (!account) { 52 if (!account) {
48 res.status(404) 53 if (sendNotFound === true) {
49 .send({ error: 'Account not found' }) 54 res.status(404)
50 .end() 55 .send({ error: 'Account not found' })
56 .end()
57 }
51 58
52 return false 59 return false
53 } 60 }
@@ -60,6 +67,7 @@ async function isAccountExist (p: Bluebird<AccountModel>, res: Response) {
60// --------------------------------------------------------------------------- 67// ---------------------------------------------------------------------------
61 68
62export { 69export {
70 isAccountIdValid,
63 isAccountIdExist, 71 isAccountIdExist,
64 isLocalAccountNameExist, 72 isLocalAccountNameExist,
65 isAccountDescriptionValid, 73 isAccountDescriptionValid,
diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts
index 0c4b7051d..c01e742da 100644
--- a/server/middlewares/validators/account.ts
+++ b/server/middlewares/validators/account.ts
@@ -2,13 +2,14 @@ import * as express from 'express'
2import { param } from 'express-validator/check' 2import { param } from 'express-validator/check'
3import { 3import {
4 isAccountIdExist, 4 isAccountIdExist,
5 isAccountIdValid,
5 isAccountNameValid, 6 isAccountNameValid,
6 isAccountNameWithHostExist, 7 isAccountNameWithHostExist,
7 isLocalAccountNameExist 8 isLocalAccountNameExist
8} from '../../helpers/custom-validators/accounts' 9} from '../../helpers/custom-validators/accounts'
9import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
10import { logger } from '../../helpers/logger' 10import { logger } from '../../helpers/logger'
11import { areValidationErrors } from './utils' 11import { areValidationErrors } from './utils'
12import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
12 13
13const localAccountValidator = [ 14const localAccountValidator = [
14 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), 15 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
@@ -24,13 +25,18 @@ const localAccountValidator = [
24] 25]
25 26
26const accountsGetValidator = [ 27const accountsGetValidator = [
27 param('id').custom(isIdOrUUIDValid).withMessage('Should have a valid id'), 28 param('id').custom(isAccountIdValid).withMessage('Should have a valid id/uuid/name/name with host'),
28 29
29 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 30 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
30 logger.debug('Checking accountsGetValidator parameters', { parameters: req.params }) 31 logger.debug('Checking accountsGetValidator parameters', { parameters: req.params })
31 32
32 if (areValidationErrors(req, res)) return 33 if (areValidationErrors(req, res)) return
33 if (!await isAccountIdExist(req.params.id, res)) return 34
35 let accountFetched = false
36 if (isIdOrUUIDValid(req.params.id)) accountFetched = await isAccountIdExist(req.params.id, res, false)
37 if (!accountFetched) accountFetched = await isAccountNameWithHostExist(req.params.id, res, true)
38
39 if (!accountFetched) return
34 40
35 return next() 41 return next()
36 } 42 }