X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fmiddlewares%2Fvalidators%2Fusers.ts;h=8fbab4dd049226217894d3674ce04fd707c8ca7b;hb=2ef6a0635cb6be32ef6028fb76d1c8ba4a6f7109;hp=5dd8caa3f575467a7507d240e3620de7bf7cf163;hpb=0883b3245bf0deb9106c4041e9afbd3521b79280;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 5dd8caa3f..8fbab4dd0 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -7,7 +7,7 @@ import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { isAvatarFile, isUserAutoPlayVideoValid, - isUserDescriptionValid, + isUserDescriptionValid, isUserDisplayNameValid, isUserNSFWPolicyValid, isUserPasswordValid, isUserRoleValid, @@ -16,11 +16,12 @@ import { } from '../../helpers/custom-validators/users' import { isVideoExist } from '../../helpers/custom-validators/videos' import { logger } from '../../helpers/logger' -import { isSignupAllowed } from '../../helpers/utils' -import { CONSTRAINTS_FIELDS } from '../../initializers' +import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils' +import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' import { Redis } from '../../lib/redis' import { UserModel } from '../../models/account/user' import { areValidationErrors } from './utils' +import { ActorModel } from '../../models/activitypub/actor' const usersAddValidator = [ body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), @@ -98,6 +99,7 @@ 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('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), body('email').optional().isEmail().withMessage('Should have a valid email attribute'), @@ -176,6 +178,20 @@ const ensureUserRegistrationAllowed = [ } ] +const ensureUserRegistrationAllowedForIP = [ + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + const allowed = isSignupAllowedForCurrentIP(req.ip) + + if (allowed === false) { + return res.status(403) + .send({ error: 'You are not on a network authorized for registration.' }) + .end() + } + + return next() + } +] + const usersAskResetPasswordValidator = [ body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'), @@ -229,6 +245,7 @@ export { usersUpdateMeValidator, usersVideoRatingValidator, ensureUserRegistrationAllowed, + ensureUserRegistrationAllowedForIP, usersGetValidator, usersUpdateMyAvatarValidator, usersAskResetPasswordValidator, @@ -255,6 +272,14 @@ async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: return false } + const actor = await ActorModel.loadLocalByName(username) + if (actor) { + res.status(409) + .send({ error: 'Another actor (account/channel) with this name already exists.' }) + .end() + return false + } + return true }