X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fusers.ts;h=840b9fc744379e2a13039efafeb5d31b98824e30;hb=2158ac90341dc3fcae958540de65032da25c8d6e;hp=40dd0f0e96b85413aab764cd11019925c6b68807;hpb=453e83ea5d81d203ba34bc43cd5c2c750ba40568;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 40dd0f0e9..840b9fc74 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,16 +1,20 @@ import * as Bluebird from 'bluebird' import * as express from 'express' -import { body, param } from 'express-validator' +import { body, param, query } from 'express-validator' import { omit } from 'lodash' -import { isIdOrUUIDValid, toBooleanOrNull } from '../../helpers/custom-validators/misc' +import { isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' import { + isNoInstanceConfigWarningModal, + isNoWelcomeModal, isUserAdminFlagsValid, + isUserAutoPlayNextVideoValid, isUserAutoPlayVideoValid, isUserBlockedReasonValid, isUserDescriptionValid, isUserDisplayNameValid, isUserNSFWPolicyValid, isUserPasswordValid, + isUserPasswordValidOrEmpty, isUserRoleValid, isUserUsernameValid, isUserVideoLanguages, @@ -32,14 +36,17 @@ import { isThemeRegistered } from '../../lib/plugins/theme-utils' import { doesVideoExist } from '../../helpers/middlewares' import { UserRole } from '../../../shared/models/users' import { MUserDefault } from '@server/typings/models' +import { Hooks } from '@server/lib/plugins/hooks' const usersAddValidator = [ body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), - body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), + body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'), body('email').isEmail().withMessage('Should have a valid email'), 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('role') + .customSanitizer(toIntOrNull) + .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) => { @@ -51,7 +58,7 @@ const usersAddValidator = [ const authUser = res.locals.oauth.token.User if (authUser.role !== UserRole.ADMINISTRATOR && req.body.role !== UserRole.USER) { return res.status(403) - .json({ error: 'You can only create users (and not administrators or moderators' }) + .json({ error: 'You can only create users (and not administrators or moderators)' }) } return next() @@ -88,7 +95,7 @@ const usersRegisterValidator = [ if (body.channel.name === body.username) { return res.status(400) - .json({ error: 'Channel name cannot be the same than user username.' }) + .json({ error: 'Channel name cannot be the same as user username.' }) } const existing = await ActorModel.loadLocalByName(body.channel.name) @@ -142,7 +149,7 @@ const usersBlockingValidator = [ ] const deleteMeValidator = [ - async (req: express.Request, res: express.Response, next: express.NextFunction) => { + (req: express.Request, res: express.Response, next: express.NextFunction) => { const user = res.locals.oauth.token.User if (user.username === 'root') { return res.status(400) @@ -161,7 +168,10 @@ const usersUpdateValidator = [ body('emailVerified').optional().isBoolean().withMessage('Should have a valid email verified attribute'), 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('role') + .optional() + .customSanitizer(toIntOrNull) + .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) => { @@ -211,6 +221,15 @@ const usersUpdateMeValidator = [ body('theme') .optional() .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'), + body('noInstanceConfigWarningModal') + .optional() + .custom(v => isNoInstanceConfigWarningModal(v)).withMessage('Should have a valid noInstanceConfigWarningModal boolean'), + body('noWelcomeModal') + .optional() + .custom(v => isNoWelcomeModal(v)).withMessage('Should have a valid noWelcomeModal boolean'), + body('autoPlayNextVideo') + .optional() + .custom(v => isUserAutoPlayNextVideoValid(v)).withMessage('Should have a valid autoPlayNextVideo boolean'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') }) @@ -237,12 +256,13 @@ const usersUpdateMeValidator = [ const usersGetValidator = [ param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), + query('withStats').optional().isBoolean().withMessage('Should have a valid stats flag'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersGet parameters', { parameters: req.params }) if (areValidationErrors(req, res)) return - if (!await checkUserIdExist(req.params.id, res)) return + if (!await checkUserIdExist(req.params.id, res, req.query.withStats)) return return next() } @@ -263,10 +283,20 @@ const usersVideoRatingValidator = [ const ensureUserRegistrationAllowed = [ async (req: express.Request, res: express.Response, next: express.NextFunction) => { - const allowed = await isSignupAllowed() - if (allowed === false) { + const allowedParams = { + body: req.body, + ip: req.ip + } + + const allowedResult = await Hooks.wrapPromiseFun( + isSignupAllowed, + allowedParams, + 'filter:api.user.signup.allowed.result' + ) + + if (allowedResult.allowed === false) { return res.status(403) - .json({ error: 'User registration is not enabled or user limit is reached.' }) + .json({ error: allowedResult.errorMessage || 'User registration is not enabled or user limit is reached.' }) } return next() @@ -274,7 +304,7 @@ const ensureUserRegistrationAllowed = [ ] const ensureUserRegistrationAllowedForIP = [ - async (req: express.Request, res: express.Response, next: express.NextFunction) => { + (req: express.Request, res: express.Response, next: express.NextFunction) => { const allowed = isSignupAllowedForCurrentIP(req.ip) if (allowed === false) { @@ -381,7 +411,7 @@ const userAutocompleteValidator = [ ] const ensureAuthUserOwnsAccountValidator = [ - async (req: express.Request, res: express.Response, next: express.NextFunction) => { + (req: express.Request, res: express.Response, next: express.NextFunction) => { const user = res.locals.oauth.token.User if (res.locals.account.id !== user.Account.id) { @@ -431,8 +461,9 @@ export { // --------------------------------------------------------------------------- -function checkUserIdExist (id: number, res: express.Response) { - return checkUserExist(() => UserModel.loadById(id), res) +function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) { + const id = parseInt(idArg + '', 10) + return checkUserExist(() => UserModel.loadById(id, withStats), res) } function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {