X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fusers.ts;h=a507afc5bf513ac9e6636236342c68f0658a9160;hb=b4055e1c23eeefb0c8a85a77f312b2827d98f483;hp=4be446732d1959adc44ddb48c442dac6f13f808f;hpb=2ba92871319d7af63472c1380664a9f9eeb1c690;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 4be446732..a507afc5b 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -5,6 +5,7 @@ import { body, param } from 'express-validator/check' import { omit } from 'lodash' import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { + isUserAdminFlagsValid, isUserAutoPlayVideoValid, isUserBlockedReasonValid, isUserDescriptionValid, @@ -12,7 +13,7 @@ import { isUserNSFWPolicyValid, isUserPasswordValid, isUserRoleValid, - isUserUsernameValid, + isUserUsernameValid, isUserVideoLanguages, isUserVideoQuotaDailyValid, isUserVideoQuotaValid, isUserVideosHistoryEnabledValid @@ -24,6 +25,11 @@ import { Redis } from '../../lib/redis' import { UserModel } from '../../models/account/user' import { areValidationErrors } from './utils' import { ActorModel } from '../../models/activitypub/actor' +import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor' +import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' +import { UserRegister } from '../../../shared/models/users/user-register.model' +import { isThemeNameValid } from '../../helpers/custom-validators/plugins' +import { isThemeRegistered } from '../../lib/plugins/theme-utils' const usersAddValidator = [ body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), @@ -32,6 +38,7 @@ const usersAddValidator = [ body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'), body('role').custom(isUserRoleValid).withMessage('Should have a valid role'), + body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') }) @@ -47,6 +54,16 @@ const usersRegisterValidator = [ body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), body('email').isEmail().withMessage('Should have a valid email'), + body('displayName') + .optional() + .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), + + body('channel.name') + .optional() + .custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), + body('channel.displayName') + .optional() + .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) @@ -54,6 +71,28 @@ const usersRegisterValidator = [ if (areValidationErrors(req, res)) return if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return + const body: UserRegister = req.body + if (body.channel) { + if (!body.channel.name || !body.channel.displayName) { + return res.status(400) + .send({ error: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' }) + .end() + } + + if (body.channel.name === body.username) { + return res.status(400) + .send({ error: 'Channel name cannot be the same than user username.' }) + .end() + } + + const existing = await ActorModel.loadLocalByName(body.channel.name) + if (existing) { + return res.status(409) + .send({ error: `Channel with name ${body.channel.name} already exists.` }) + .end() + } + } + return next() } ] @@ -120,6 +159,7 @@ const usersUpdateValidator = [ body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'), body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'), + body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersUpdate parameters', { parameters: req.body }) @@ -139,21 +179,41 @@ const usersUpdateValidator = [ ] const usersUpdateMeValidator = [ - body('displayName').optional().custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), - body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'), - body('currentPassword').optional().custom(isUserPasswordValid).withMessage('Should have a valid current password'), - body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), - body('email').optional().isEmail().withMessage('Should have a valid email attribute'), - body('nsfwPolicy').optional().custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'), - body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), + body('displayName') + .optional() + .custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), + body('description') + .optional() + .custom(isUserDescriptionValid).withMessage('Should have a valid description'), + body('currentPassword') + .optional() + .custom(isUserPasswordValid).withMessage('Should have a valid current password'), + body('password') + .optional() + .custom(isUserPasswordValid).withMessage('Should have a valid password'), + body('email') + .optional() + .isEmail().withMessage('Should have a valid email attribute'), + body('nsfwPolicy') + .optional() + .custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'), + body('autoPlayVideo') + .optional() + .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), + body('videoLanguages') + .optional() + .custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'), body('videosHistoryEnabled') .optional() .custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'), + body('theme') + .optional() + .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') }) - if (req.body.password) { + if (req.body.password || req.body.email) { if (!req.body.currentPassword) { return res.status(400) .send({ error: 'currentPassword parameter is missing.' }) @@ -290,8 +350,14 @@ const usersAskSendVerifyEmailValidator = [ ] const usersVerifyEmailValidator = [ - param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), - body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'), + param('id') + .isInt().not().isEmpty().withMessage('Should have a valid id'), + + body('verificationString') + .not().isEmpty().withMessage('Should have a valid verification string'), + body('isPendingEmail') + .optional() + .toBoolean(), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params }) @@ -317,6 +383,20 @@ const userAutocompleteValidator = [ param('search').isString().not().isEmpty().withMessage('Should have a search parameter') ] +const ensureAuthUserOwnsAccountValidator = [ + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + const user = res.locals.oauth.token.User + + if (res.locals.account.id !== user.Account.id) { + return res.status(403) + .send({ error: 'Only owner can access ratings list.' }) + .end() + } + + return next() + } +] + // --------------------------------------------------------------------------- export { @@ -335,7 +415,8 @@ export { usersResetPasswordValidator, usersAskSendVerifyEmailValidator, usersVerifyEmailValidator, - userAutocompleteValidator + userAutocompleteValidator, + ensureAuthUserOwnsAccountValidator } // ---------------------------------------------------------------------------