diff options
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/validators/feeds.ts | 13 | ||||
-rw-r--r-- | server/middlewares/validators/users.ts | 28 | ||||
-rw-r--r-- | server/middlewares/validators/videos/video-channels.ts | 16 |
3 files changed, 47 insertions, 10 deletions
diff --git a/server/middlewares/validators/feeds.ts b/server/middlewares/validators/feeds.ts index e4f5c98fe..dd362619d 100644 --- a/server/middlewares/validators/feeds.ts +++ b/server/middlewares/validators/feeds.ts | |||
@@ -1,21 +1,20 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param, query } from 'express-validator/check' | 2 | import { param, query } from 'express-validator/check' |
3 | import { doesAccountIdExist, isAccountNameValid, doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' | 3 | import { doesAccountIdExist, doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' |
4 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' | 4 | import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' |
5 | import { logger } from '../../helpers/logger' | 5 | import { logger } from '../../helpers/logger' |
6 | import { areValidationErrors } from './utils' | 6 | import { areValidationErrors } from './utils' |
7 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' | 7 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' |
8 | import { doesVideoChannelIdExist, doesVideoChannelNameWithHostExist } from '../../helpers/custom-validators/video-channels' | 8 | import { doesVideoChannelIdExist, doesVideoChannelNameWithHostExist } from '../../helpers/custom-validators/video-channels' |
9 | import { doesVideoExist } from '../../helpers/custom-validators/videos' | 9 | import { doesVideoExist } from '../../helpers/custom-validators/videos' |
10 | import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor' | ||
11 | 10 | ||
12 | const videoFeedsValidator = [ | 11 | const videoFeedsValidator = [ |
13 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | 12 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), |
14 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | 13 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), |
15 | query('accountId').optional().custom(isIdOrUUIDValid), | 14 | query('accountId').optional().custom(isIdValid), |
16 | query('accountName').optional().custom(isAccountNameValid), | 15 | query('accountName').optional(), |
17 | query('videoChannelId').optional().custom(isIdOrUUIDValid), | 16 | query('videoChannelId').optional().custom(isIdValid), |
18 | query('videoChannelName').optional().custom(isActorPreferredUsernameValid), | 17 | query('videoChannelName').optional(), |
19 | 18 | ||
20 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 19 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
21 | logger.debug('Checking feeds parameters', { parameters: req.query }) | 20 | logger.debug('Checking feeds parameters', { parameters: req.query }) |
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 6d8cd7894..7a081af33 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts | |||
@@ -25,6 +25,10 @@ import { Redis } from '../../lib/redis' | |||
25 | import { UserModel } from '../../models/account/user' | 25 | import { UserModel } from '../../models/account/user' |
26 | import { areValidationErrors } from './utils' | 26 | import { areValidationErrors } from './utils' |
27 | import { ActorModel } from '../../models/activitypub/actor' | 27 | import { ActorModel } from '../../models/activitypub/actor' |
28 | import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor' | ||
29 | import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' | ||
30 | import { UserCreate } from '../../../shared/models/users' | ||
31 | import { UserRegister } from '../../../shared/models/users/user-register.model' | ||
28 | 32 | ||
29 | const usersAddValidator = [ | 33 | const usersAddValidator = [ |
30 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), | 34 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), |
@@ -49,6 +53,8 @@ const usersRegisterValidator = [ | |||
49 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), | 53 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), |
50 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), | 54 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), |
51 | body('email').isEmail().withMessage('Should have a valid email'), | 55 | body('email').isEmail().withMessage('Should have a valid email'), |
56 | body('channel.name').optional().custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), | ||
57 | body('channel.displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | ||
52 | 58 | ||
53 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 59 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
54 | logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) | 60 | logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) |
@@ -56,6 +62,28 @@ const usersRegisterValidator = [ | |||
56 | if (areValidationErrors(req, res)) return | 62 | if (areValidationErrors(req, res)) return |
57 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return | 63 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return |
58 | 64 | ||
65 | const body: UserRegister = req.body | ||
66 | if (body.channel) { | ||
67 | if (!body.channel.name || !body.channel.displayName) { | ||
68 | return res.status(400) | ||
69 | .send({ error: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' }) | ||
70 | .end() | ||
71 | } | ||
72 | |||
73 | if (body.channel.name === body.username) { | ||
74 | return res.status(400) | ||
75 | .send({ error: 'Channel name cannot be the same than user username.' }) | ||
76 | .end() | ||
77 | } | ||
78 | |||
79 | const existing = await ActorModel.loadLocalByName(body.channel.name) | ||
80 | if (existing) { | ||
81 | return res.status(409) | ||
82 | .send({ error: `Channel with name ${body.channel.name} already exists.` }) | ||
83 | .end() | ||
84 | } | ||
85 | } | ||
86 | |||
59 | return next() | 87 | return next() |
60 | } | 88 | } |
61 | ] | 89 | ] |
diff --git a/server/middlewares/validators/videos/video-channels.ts b/server/middlewares/validators/videos/video-channels.ts index 4b26f0bc4..f5a59cacb 100644 --- a/server/middlewares/validators/videos/video-channels.ts +++ b/server/middlewares/validators/videos/video-channels.ts | |||
@@ -14,6 +14,7 @@ import { VideoChannelModel } from '../../../models/video/video-channel' | |||
14 | import { areValidationErrors } from '../utils' | 14 | import { areValidationErrors } from '../utils' |
15 | import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor' | 15 | import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor' |
16 | import { ActorModel } from '../../../models/activitypub/actor' | 16 | import { ActorModel } from '../../../models/activitypub/actor' |
17 | import { isBooleanValid } from '../../../helpers/custom-validators/misc' | ||
17 | 18 | ||
18 | const videoChannelsAddValidator = [ | 19 | const videoChannelsAddValidator = [ |
19 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), | 20 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), |
@@ -40,9 +41,18 @@ const videoChannelsAddValidator = [ | |||
40 | 41 | ||
41 | const videoChannelsUpdateValidator = [ | 42 | const videoChannelsUpdateValidator = [ |
42 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), | 43 | param('nameWithHost').exists().withMessage('Should have an video channel name with host'), |
43 | body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), | 44 | body('displayName') |
44 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | 45 | .optional() |
45 | body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | 46 | .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), |
47 | body('description') | ||
48 | .optional() | ||
49 | .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | ||
50 | body('support') | ||
51 | .optional() | ||
52 | .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'), | ||
53 | body('bulkVideosSupportUpdate') | ||
54 | .optional() | ||
55 | .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'), | ||
46 | 56 | ||
47 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 57 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
48 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) | 58 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |