diff options
-rw-r--r-- | client/src/app/+accounts/accounts.component.ts | 2 | ||||
-rw-r--r-- | client/src/app/shared/account/account.service.ts | 2 | ||||
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 28 | ||||
-rw-r--r-- | server/middlewares/validators/account.ts | 12 |
4 files changed, 29 insertions, 15 deletions
diff --git a/client/src/app/+accounts/accounts.component.ts b/client/src/app/+accounts/accounts.component.ts index 1298803c3..0a52985da 100644 --- a/client/src/app/+accounts/accounts.component.ts +++ b/client/src/app/+accounts/accounts.component.ts | |||
@@ -16,7 +16,7 @@ export class AccountsComponent implements OnInit { | |||
16 | ) {} | 16 | ) {} |
17 | 17 | ||
18 | ngOnInit () { | 18 | ngOnInit () { |
19 | const accountId = parseInt(this.route.snapshot.params['accountId'], 10) | 19 | const accountId = this.route.snapshot.params['accountId'] |
20 | 20 | ||
21 | this.accountService.getAccount(accountId) | 21 | this.accountService.getAccount(accountId) |
22 | .subscribe(account => this.account = account) | 22 | .subscribe(account => this.account = account) |
diff --git a/client/src/app/shared/account/account.service.ts b/client/src/app/shared/account/account.service.ts index 20e52d946..6b261cf53 100644 --- a/client/src/app/shared/account/account.service.ts +++ b/client/src/app/shared/account/account.service.ts | |||
@@ -18,7 +18,7 @@ export class AccountService { | |||
18 | private restExtractor: RestExtractor | 18 | private restExtractor: RestExtractor |
19 | ) {} | 19 | ) {} |
20 | 20 | ||
21 | getAccount (id: number): Observable<Account> { | 21 | getAccount (id: number | string): Observable<Account> { |
22 | return this.authHttp.get<ServerAccount>(AccountService.BASE_ACCOUNT_URL + id) | 22 | return this.authHttp.get<ServerAccount>(AccountService.BASE_ACCOUNT_URL + id) |
23 | .pipe( | 23 | .pipe( |
24 | map(accountHash => new Account(accountHash)), | 24 | map(accountHash => new Account(accountHash)), |
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' | |||
4 | import * as validator from 'validator' | 4 | import * as validator from 'validator' |
5 | import { AccountModel } from '../../models/account/account' | 5 | import { AccountModel } from '../../models/account/account' |
6 | import { isUserDescriptionValid, isUserUsernameValid } from './users' | 6 | import { isUserDescriptionValid, isUserUsernameValid } from './users' |
7 | import { exists } from './misc' | ||
7 | 8 | ||
8 | function isAccountNameValid (value: string) { | 9 | function isAccountNameValid (value: string) { |
9 | return isUserUsernameValid(value) | 10 | return isUserUsernameValid(value) |
10 | } | 11 | } |
11 | 12 | ||
13 | function isAccountIdValid (value: string) { | ||
14 | return exists(value) | ||
15 | } | ||
16 | |||
12 | function isAccountDescriptionValid (value: string) { | 17 | function isAccountDescriptionValid (value: string) { |
13 | return isUserDescriptionValid(value) | 18 | return isUserDescriptionValid(value) |
14 | } | 19 | } |
15 | 20 | ||
16 | function isAccountIdExist (id: number | string, res: Response) { | 21 | function 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 | ||
28 | function isLocalAccountNameExist (name: string, res: Response) { | 33 | function 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 | ||
34 | function isAccountNameWithHostExist (nameWithDomain: string, res: Response) { | 39 | function 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 | ||
44 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { | 49 | async 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 | ||
62 | export { | 69 | export { |
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' | |||
2 | import { param } from 'express-validator/check' | 2 | import { param } from 'express-validator/check' |
3 | import { | 3 | import { |
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' |
9 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' | ||
10 | import { logger } from '../../helpers/logger' | 10 | import { logger } from '../../helpers/logger' |
11 | import { areValidationErrors } from './utils' | 11 | import { areValidationErrors } from './utils' |
12 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' | ||
12 | 13 | ||
13 | const localAccountValidator = [ | 14 | const 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 | ||
26 | const accountsGetValidator = [ | 27 | const 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 | } |