Browse Source

Limited avatar initials to first 2 characters and limited user name only to valid characters

Kristian Vos 3 years ago
parent
commit
cbd389ea5a

+ 8 - 0
backend/logic/db/index.js

@@ -22,6 +22,7 @@ const regex = {
 	az09_: /^[a-z0-9_]+$/,
 	emailSimple: /^[\x00-\x7F]+@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?$/,
 	ascii: /^[\x00-\x7F]+$/,
+	name: /^[\p{L} .'-]+$/u,
 	custom: regex => new RegExp(`^[${regex}]+$`)
 };
 
@@ -137,6 +138,13 @@ class _DBModule extends CoreClass {
 						return regex.emailSimple.test(email) && regex.ascii.test(email);
 					}, "Invalid email.");
 
+					this.schemas.user
+						.path("name")
+						.validate(
+							name => isLength(name, 1, 64) && regex.name.test(name),
+							"Invalid name."
+						);
+
 					// Station
 					this.schemas.station
 						.path("name")

+ 1 - 0
frontend/src/components/ProfilePicture.vue

@@ -34,6 +34,7 @@ export default {
 			return this.name
 				.split(" ")
 				.map(word => word.charAt(0))
+				.splice(0, 2)
 				.join("")
 				.toUpperCase();
 		}

+ 5 - 0
frontend/src/pages/Settings/tabs/Profile.vue

@@ -143,6 +143,11 @@ export default {
 			if (!validation.isLength(name, 1, 64))
 				return new Toast("Name must have between 1 and 64 characters.");
 
+			if (!validation.regex.name.test(name))
+				return new Toast(
+					"Invalid name format. Only letters, spaces, apostrophes and hyphens are allowed."
+				);
+
 			this.$refs.saveButton.status = "disabled";
 
 			return this.socket.dispatch(

+ 1 - 0
frontend/src/validation.js

@@ -4,6 +4,7 @@ export default {
 		az09_: /^[a-z0-9_]+$/,
 		emailSimple: /^[\x00-\x7F]+@[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)?$/,
 		ascii: /^[\x00-\x7F]+$/,
+		name: /^[\p{L} .'-]+$/u,
 		password: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~])[A-Za-z\d!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/,
 		custom: regex => {
 			return new RegExp(`^[${regex}]+$`);