Przeglądaj źródła

fix: Name and username validation should require one letter or number

Owen Diffey 3 lat temu
rodzic
commit
37cfb31da2

+ 2 - 6
backend/logic/actions/users.js

@@ -534,7 +534,6 @@ export default {
 
 				// create the user object
 				(hash, _id, next) => {
-					const avatarColors = ["blue", "orange", "green", "purple", "teal"];
 					next(null, {
 						_id,
 						name: username,
@@ -543,10 +542,6 @@ export default {
 							address: email,
 							verificationToken
 						},
-						avatar: {
-							type: "initials",
-							color: avatarColors[Math.random(Math.floor(Math.random() * avatarColors.length))]
-						},
 						services: {
 							password: {
 								password: hash
@@ -558,9 +553,10 @@ export default {
 				// generate the url for gravatar avatar
 				(user, next) => {
 					UtilsModule.runJob("CREATE_GRAVATAR", { email: user.email.address }, this).then(url => {
+						const avatarColors = ["blue", "orange", "green", "purple", "teal"];
 						user.avatar = {
 							type: "initials",
-							color: "blue",
+							color: avatarColors[Math.floor(Math.random() * avatarColors.length)],
 							url
 						};
 						next(null, user);

+ 11 - 2
backend/logic/db/index.js

@@ -128,7 +128,10 @@ class _DBModule extends CoreClass {
 					this.schemas.user
 						.path("username")
 						.validate(
-							username => isLength(username, 2, 32) && regex.custom("a-zA-Z0-9_-").test(username),
+							username =>
+								isLength(username, 2, 32) &&
+								regex.custom("a-zA-Z0-9_-").test(username) &&
+								username.replaceAll(/[_]/g, "").length > 0,
 							"Invalid username."
 						);
 
@@ -140,7 +143,13 @@ class _DBModule extends CoreClass {
 
 					this.schemas.user
 						.path("name")
-						.validate(name => isLength(name, 1, 64) && regex.name.test(name), "Invalid name.");
+						.validate(
+							name =>
+								isLength(name, 1, 64) &&
+								regex.name.test(name) &&
+								name.replaceAll(/[ .'_-]/g, "").length > 0,
+							"Invalid name."
+						);
 
 					// Station
 					this.schemas.station

+ 4 - 0
frontend/src/components/modals/Register.vue

@@ -201,6 +201,10 @@ export default {
 				this.username.message =
 					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
 				this.username.valid = false;
+			} else if (value.replaceAll(/[_]/g, "").length === 0) {
+				this.username.message =
+					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
+				this.username.valid = false;
 			} else {
 				this.username.message = "Everything looks great!";
 				this.username.valid = true;

+ 17 - 8
frontend/src/pages/Settings/Tabs/Account.vue

@@ -136,17 +136,21 @@ export default {
 		// prettier-ignore
 		// eslint-disable-next-line func-names
 		"modifiedUser.username": function (value) {
-		if (!validation.isLength(value, 2, 32)) {
-			this.validation.username.message =
-				"Username must have between 2 and 32 characters.";
-			this.validation.username.valid = false;
-		} else if (
-			!validation.regex.azAZ09_.test(value) &&
-			value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
-		) {
+			if (!validation.isLength(value, 2, 32)) {
+				this.validation.username.message =
+					"Username must have between 2 and 32 characters.";
+				this.validation.username.valid = false;
+			} else if (
+				!validation.regex.azAZ09_.test(value) &&
+				value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
+			) {
 				this.validation.username.message =
 					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
 				this.validation.username.valid = false;
+			} else if (value.replaceAll(/[_]/g, "").length === 0) {
+				this.validation.username.message =
+					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
+				this.validation.username.valid = false;
 			} else {
 				this.validation.username.message = "Everything looks great!";
 				this.validation.username.valid = true;
@@ -256,6 +260,11 @@ export default {
 					"Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
 				);
 
+			if (username.replaceAll(/[_]/g, "").length === 0)
+				return new Toast(
+					"Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number."
+				);
+
 			this.$refs.saveButton.saveStatus = "disabled";
 
 			return this.socket.dispatch(

+ 2 - 2
frontend/src/pages/Settings/Tabs/Profile.vue

@@ -150,9 +150,9 @@ export default {
 				return new Toast(
 					"Invalid name format. Only letters, numbers, spaces, apostrophes, underscores and hyphens are allowed."
 				);
-			if (name.replaceAll(/[0-9 .'_-]/g, "").length === 0)
+			if (name.replaceAll(/[ .'_-]/g, "").length === 0)
 				return new Toast(
-					"Invalid name format. Only letters, numbers, spaces, apostrophes, underscores and hyphens are allowed, and there has to be at least one letter."
+					"Invalid name format. Only letters, numbers, spaces, apostrophes, underscores and hyphens are allowed, and there has to be at least one letter or number."
 				);
 
 			this.$refs.saveButton.status = "disabled";

+ 7 - 0
frontend/src/store/modules/user.js

@@ -62,6 +62,13 @@ const modules = {
 							)
 						);
 
+					if (username.replaceAll(/[_]/g, "").length === 0)
+						return reject(
+							new Error(
+								"Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number."
+							)
+						);
+
 					if (!validation.isLength(password, 6, 200))
 						return reject(
 							new Error(