瀏覽代碼

Worked on limits on frontend.

KrisVos130 8 年之前
父節點
當前提交
b50923dd2f

+ 26 - 8
frontend/components/Modals/CreateCommunityStation.vue

@@ -2,7 +2,7 @@
 	<modal title='Create Community Station'>
 		<div slot='body'>
 			<!-- validation to check if exists http://bulma.io/documentation/elements/form/ -->
-			<label class='label'>Name (lowercase, a-z, used in the url)</label>
+			<label class='label'>Name (unique lowercase station id)</label>
 			<p class='control'>
 				<input class='input' type='text' placeholder='Name...' v-model='newCommunity.name' autofocus>
 			</p>
@@ -25,6 +25,7 @@
 	import { Toast } from 'vue-roaster';
 	import Modal from './Modal.vue';
 	import io from '../../io';
+	import validation from '../../validation';
 
 	export default {
 		components: { Modal },
@@ -48,15 +49,32 @@
 				this.$parent.modals.createCommunityStation = !this.$parent.modals.createCommunityStation;
 			},
 			submitModal: function () {
-				let _this = this;
-				if (_this.newCommunity.name == '') return Toast.methods.addToast('Name cannot be a blank field', 3000);
-				if (_this.newCommunity.displayName == '') return Toast.methods.addToast('Display Name cannot be a blank field', 3000);
-				if (_this.newCommunity.description == '') return Toast.methods.addToast('Description cannot be a blank field', 3000);
+				const name = this.newCommunity.name;
+				const displayName = this.newCommunity.displayName;
+				const description = this.newCommunity.description;
+				if (!name || !displayName || !description) return Toast.methods.addToast('Please fill in all fields', 8000);
+
+				if (!validation.isLength(name, 2, 16)) return Toast.methods.addToast('Name must have between 2 and 16 characters.', 8000);
+				if (!validation.regex.az09_.test(name)) return Toast.methods.addToast('Invalid name format. Allowed characters: a-z, 0-9 and _.', 8000);
+
+
+				if (!validation.isLength(displayName, 2, 32)) return Toast.methods.addToast('Display name must have between 2 and 32 characters.', 8000);
+				if (!validation.regex.azAZ09_.test(displayName)) return Toast.methods.addToast('Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
+
+
+				if (!validation.isLength(description, 2, 200)) return Toast.methods.addToast('Description must have between 2 and 200 characters.', 8000);
+				let characters = description.split("");
+				characters = characters.filter(function(character) {
+					return character.charCodeAt(0) === 21328;
+				});
+				if (characters.length !== 0) return Toast.methods.addToast('Invalid description format. Swastika\'s are not allowed.', 8000);
+
+
 				this.socket.emit('stations.create', {
-					name: _this.newCommunity.name,
+					name: name,
 					type: 'community',
-					displayName: _this.newCommunity.displayName,
-					description: _this.newCommunity.description
+					displayName: displayName,
+					description: description
 				}, res => {
 					if (res.status === 'success') Toast.methods.addToast(`You have added the station successfully`, 4000);
 					else Toast.methods.addToast(res.message, 4000);

+ 31 - 14
frontend/components/Modals/EditStation.vue

@@ -43,6 +43,7 @@
 	import { Toast } from 'vue-roaster';
 	import Modal from './Modal.vue';
 	import io from '../../io';
+	import validation from '../../validation';
 
 	export default {
 		data: function() {
@@ -67,13 +68,17 @@
 				if (this.$parent.station.partyMode !== this.editing.partyMode) this.updatePartyMode();
 			},
 			updateName: function () {
-				let _this = this;
-				this.socket.emit('stations.updateName', this.editing._id, this.editing.name, res => {
+				const name = this.editing.name;
+				if (!validation.isLength(name, 2, 16)) return Toast.methods.addToast('Name must have between 2 and 16 characters.', 8000);
+				if (!validation.regex.az09_.test(name)) return Toast.methods.addToast('Invalid name format. Allowed characters: a-z, 0-9 and _.', 8000);
+
+
+				this.socket.emit('stations.updateName', this.editing._id, name, res => {
 					if (res.status === 'success') {
-						if (_this.$parent.station) _this.$parent.station.name = _this.editing.name;
+						if (this.$parent.station) _this.$parent.station.name = name;
 						else {
-							_this.$parent.stations.forEach((station, index) => {
-								if (station._id === _this.editing._id) return _this.$parent.stations[index].name = _this.editing.name;
+							this.$parent.stations.forEach((station, index) => {
+								if (station._id === this.editing._id) return this.$parent.stations[index].name = name;
 							});
 						}
 					}
@@ -81,13 +86,17 @@
 				});
 			},
 			updateDisplayName: function () {
-				let _this = this;
-				this.socket.emit('stations.updateDisplayName', this.editing._id, this.editing.displayName, res => {
+				const displayName = this.editing.displayName;
+				if (!validation.isLength(displayName, 2, 32)) return Toast.methods.addToast('Display name must have between 2 and 32 characters.', 8000);
+				if (!validation.regex.azAZ09_.test(displayName)) return Toast.methods.addToast('Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
+
+
+				this.socket.emit('stations.updateDisplayName', this.editing._id, displayName, res => {
 					if (res.status === 'success') {
-						if (_this.$parent.station) _this.$parent.station.displayName = _this.editing.displayName;
+						if (this.$parent.station) _this.$parent.station.displayName = displayName;
 						else {
-							_this.$parent.stations.forEach((station, index) => {
-								if (station._id === _this.editing._id) return _this.$parent.stations[index].displayName = _this.editing.displayName;
+							this.$parent.stations.forEach((station, index) => {
+								if (station._id === this.editing._id) return this.$parent.stations[index].displayName = displayName;
 							});
 						}
 					}
@@ -95,13 +104,21 @@
 				});
 			},
 			updateDescription: function () {
-				let _this = this;
-				this.socket.emit('stations.updateDescription', this.editing._id, this.editing.description, res => {
+				const description = this.editing.description;
+				if (!validation.isLength(description, 2, 200)) return Toast.methods.addToast('Description must have between 2 and 200 characters.', 8000);
+				let characters = description.split("");
+				characters = characters.filter(function(character) {
+					return character.charCodeAt(0) === 21328;
+				});
+				if (characters.length !== 0) return Toast.methods.addToast('Invalid description format. Swastika\'s are not allowed.', 8000);
+
+
+				this.socket.emit('stations.updateDescription', this.editing._id, description, res => {
 					if (res.status === 'success') {
-						if (_this.$parent.station) _this.$parent.station.description = _this.editing.description;
+						if (_this.$parent.station) _this.$parent.station.description = description;
 						else {
 							_this.$parent.stations.forEach((station, index) => {
-								if (station._id === station._id) return _this.$parent.stations[index].description = _this.editing.description;
+								if (station._id === station._id) return _this.$parent.stations[index].description = description;
 							});
 						}
 						return Toast.methods.addToast(res.message, 4000);

+ 15 - 5
frontend/components/Modals/EditUser.vue

@@ -39,6 +39,7 @@
 	import io from '../../io';
 	import { Toast } from 'vue-roaster';
 	import Modal from './Modal.vue';
+	import validation from '../../validation';
 
 	export default {
 		components: { Modal },
@@ -49,23 +50,32 @@
 		},
 		methods: {
 			updateUsername: function () {
-				this.socket.emit(`users.updateUsername`, this.editing._id, this.editing.username, res => {
+				const username = this.editing.username;
+				if (!validation.isLength(username, 2, 32)) return Toast.methods.addToast('Username must have between 2 and 32 characters.', 8000);
+				if (!validation.regex.azAZ09_.test(username)) return Toast.methods.addToast('Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
+
+
+				this.socket.emit(`users.updateUsername`, this.editing._id, username, res => {
 					Toast.methods.addToast(res.message, 4000);
 				});
 			},
 			updateEmail: function () {
-				this.socket.emit(`users.updateEmail`, this.editing._id, this.editing.email, res => {
+				const email = this.editing.email;
+				if (!validation.isLength(email, 3, 254)) return Toast.methods.addToast('Email must have between 3 and 254 characters.', 8000);
+				if (email.indexOf('@') !== email.lastIndexOf('@') || !validation.regex.emailSimple.test(email)) return Toast.methods.addToast('Invalid email format.', 8000);
+
+
+				this.socket.emit(`users.updateEmail`, this.editing._id, email, res => {
 					Toast.methods.addToast(res.message, 4000);
 				});
 			},
 			updateRole: function () {
-				let _this = this;
 				this.socket.emit(`users.updateRole`, this.editing._id, this.editing.role, res => {
 					Toast.methods.addToast(res.message, 4000);
 					if (
 							res.status === 'success' &&
-							_this.editing.role === 'default' &&
-							_this.editing._id === _this.$parent.$parent.$parent.userId
+							this.editing.role === 'default' &&
+							this.editing._id === this.$parent.$parent.$parent.userId
 					) location.reload();
 				});
 			}

+ 25 - 10
frontend/components/User/Settings.vue

@@ -77,6 +77,7 @@
 
 	import LoginModal from '../Modals/Login.vue'
 	import io from '../../io'
+	import validation from '../../validation';
 
 	export default {
 		data() {
@@ -123,24 +124,34 @@
 		},
 		methods: {
 			changeEmail: function () {
-				if (!this.user.email.address) return Toast.methods.addToast('Email cannot be empty', 8000);
-				this.socket.emit('users.updateEmail', this.$parent.userId, this.user.email.address, res => {
+				const email = this.user.email.address;
+				if (!validation.isLength(email, 3, 254)) return Toast.methods.addToast('Email must have between 3 and 254 characters.', 8000);
+				if (email.indexOf('@') !== email.lastIndexOf('@') || !validation.regex.emailSimple.test(email)) return Toast.methods.addToast('Invalid email format.', 8000);
+
+
+				this.socket.emit('users.updateEmail', this.$parent.userId, email, res => {
 					if (res.status !== 'success') Toast.methods.addToast(res.message, 8000);
 					else Toast.methods.addToast('Successfully changed email address', 4000);
 				});
 			},
 			changeUsername: function () {
-				let _this = this;
-				if (!_this.user.username) return Toast.methods.addToast('Username cannot be empty', 8000);
-				_this.socket.emit('users.updateUsername', this.$parent.userId, _this.user.username, res => {
+				const username = this.user.username;
+				if (!validation.isLength(username, 2, 32)) return Toast.methods.addToast('Username must have between 2 and 32 characters.', 8000);
+				if (!validation.regex.azAZ09_.test(username)) return Toast.methods.addToast('Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
+
+
+				this.socket.emit('users.updateUsername', this.$parent.userId, username, res => {
 					if (res.status !== 'success') Toast.methods.addToast(res.message, 8000);
 					else Toast.methods.addToast('Successfully changed username', 4000);
 				});
 			},
 			changePassword: function () {
-				let _this = this;
-				if (!_this.newPassword) return Toast.methods.addToast('New password cannot be empty', 8000);
-				_this.socket.emit('users.updatePassword', _this.newPassword, res => {
+				const newPassword = this.newPassword;
+				if (!validation.isLength(newPassword, 6, 200)) return Toast.methods.addToast('Password must have between 6 and 200 characters.', 8000);
+				if (!validation.regex.password.test(newPassword)) return Toast.methods.addToast('Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.', 8000);
+
+
+				this.socket.emit('users.updatePassword', newPassword, res => {
 					if (res.status !== 'success') Toast.methods.addToast(res.message, 8000);
 					else Toast.methods.addToast('Successfully changed password', 4000);
 				});
@@ -163,8 +174,12 @@
 				});
 			},
 			setPassword: function () {
-				if (!this.setNewPassword) return Toast.methods.addToast('Password cannot be empty', 8000);
-				this.socket.emit('users.changePasswordWithCode', this.passwordCode, this.setNewPassword, res => {
+				const newPassword = this.setNewPassword;
+				if (!validation.isLength(newPassword, 6, 200)) return Toast.methods.addToast('Password must have between 6 and 200 characters.', 8000);
+				if (!validation.regex.password.test(newPassword)) return Toast.methods.addToast('Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.', 8000);
+
+
+				this.socket.emit('users.changePasswordWithCode', this.passwordCode, newPassword, res => {
 					Toast.methods.addToast(res.message, 8000);
 				});
 			},