From 809274a5be10029240cad49d5d90d5005d74a7b0 Mon Sep 17 00:00:00 2001 From: singhsanket143 Date: Fri, 26 Jul 2019 00:56:12 +0530 Subject: [PATCH 1/6] Added moderator addition to db --- client/views/add/add.html | 2 +- client/views/add/add.js | 6 ++++++ server/methods.js | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/client/views/add/add.html b/client/views/add/add.html index ded25dd5..fd8caef4 100644 --- a/client/views/add/add.html +++ b/client/views/add/add.html @@ -29,4 +29,4 @@

Add Moderators

- \ No newline at end of file + diff --git a/client/views/add/add.js b/client/views/add/add.js index b769dd3c..4de3dfbf 100644 --- a/client/views/add/add.js +++ b/client/views/add/add.js @@ -17,6 +17,10 @@ function checkPrevMod(modBoxes) { return occurrences === 1; } +function checkModsAccount(moderators) { + +} + Template.add.onCreated(function () { this.numberOfNewMods = new ReactiveVar(1); if (this.data.admin !== Meteor.user().emails[0].address) { @@ -69,7 +73,9 @@ Template.add.events({ 'click #modsdonebutton': function (event, template) { const modBoxes = document.getElementsByClassName('modbox'); const modBoxesArray = Array.from(modBoxes); + const allEmails = template.data.moderators; const modEmails = modBoxesArray.map(b => b.value); + const newMods = modEmails.filter(value => !allEmails.includes(value)); const occurrences = modEmails.filter(val => val !== "").length; if (checkPrevMod(modBoxes) === false) { showModsError('Email ID was already added as a moderator.'); diff --git a/server/methods.js b/server/methods.js index 3d92d256..d8fcc2ca 100644 --- a/server/methods.js +++ b/server/methods.js @@ -267,6 +267,22 @@ Meteor.methods({ if (this.userId) { let keys; const email = Meteor.users.findOne({ _id: this.userId }).emails[0].address; + const existingAccounts = Meteor.users.find({'emails.address': {'$in': mods}}).fetch().map(el => el.emails[0].address); + const newAccounts = mods.filter(email => !existingAccounts.includes(email)); + newAccounts.forEach(async (item) => { + await Accounts.createUser({ + email: item, + password: Math.random().toString(36).substr(2, 7), + profile: { + name: item.slice(0, item.indexOf('@')) + } + }); + await Accounts.forgotPassword({email: item}, (err) => { + if (err) { + console.log(err); + } + }) + }); const instance = Instances.findOne({ _id: instanceid, }); From 2958b3aeb126672ae056d55c780ae103648f8c95 Mon Sep 17 00:00:00 2001 From: singhsanket143 Date: Mon, 19 Aug 2019 18:32:12 +0530 Subject: [PATCH 2/6] Refactor: Removed the empty check function --- client/views/add/add.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/views/add/add.js b/client/views/add/add.js index 4de3dfbf..d014a9c8 100644 --- a/client/views/add/add.js +++ b/client/views/add/add.js @@ -17,10 +17,6 @@ function checkPrevMod(modBoxes) { return occurrences === 1; } -function checkModsAccount(moderators) { - -} - Template.add.onCreated(function () { this.numberOfNewMods = new ReactiveVar(1); if (this.data.admin !== Meteor.user().emails[0].address) { From a295c7c867ac772b0dd8e32807e51b968d954e9d Mon Sep 17 00:00:00 2001 From: singhsanket143 Date: Fri, 23 Aug 2019 01:32:54 +0530 Subject: [PATCH 3/6] Fix: Fixed the issue in sending forget password email during addition of moderator --- client/views/add/add.js | 20 ++++++++++++++------ server/methods.js | 29 ++++++++++++++++++----------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/client/views/add/add.js b/client/views/add/add.js index 7109e5c0..168c1df7 100644 --- a/client/views/add/add.js +++ b/client/views/add/add.js @@ -89,20 +89,28 @@ Template.add.events({ mods.push(modsInput[m].value); } } - Meteor.call('addMods', mods, template.data._id, (error, result) => { - // If the result is an object, there was an error - if (typeof result === 'object' && result.length > 0) { + Meteor.call('addMods', mods, template.data._id, (error, response) => { + console.log(response); + if (typeof response === 'object' && response['status_code'] === false && response['result'].length > 0) { // Alert the error - for (let i = 0; i < result.length; i++) { + for (let i = 0; i < response['result'].length; i++) { // Check is the server returned error corresponding to the addition of owner as moderator - if(result[i].name === 'owner') { + if(response['result'][i].name === 'owner') { // Display the error message - showModsError(`${result[i].value} is already an owner of the instance and has the privileges of a moderator.`); + showModsError(`${response['result'][i].value} is already an owner of the instance and has the privileges of a moderator.`); return false; } } showModsError('Please enter valid email addresses.'); return false; + } else if (typeof response === 'object' && response['status_code'] === true && response['result'].length > 0) { + for(let k = 0; k < response['result'].length; k++) { + Accounts.forgotPassword({email: response['result'][k]}, function(err) { + if (err) { + console.log(err); + } + }) + } } for (let m = 0; m < mods.length; m++) { Meteor.call('sendEmail', diff --git a/server/methods.js b/server/methods.js index 663dbe29..aac976a0 100644 --- a/server/methods.js +++ b/server/methods.js @@ -5,6 +5,11 @@ import { Answers, Questions, Instances, Votes } from '../lib/common.js'; var fs = Npm.require('fs'); +function checkValidEmail(email) { + let filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; + return filter.test(email); +} + Meteor.methods({ // A method that returns the current connection's IP address getIP() { @@ -267,23 +272,21 @@ Meteor.methods({ }, addMods(mods, instanceid) { if (this.userId) { - let keys; + var keys = {}; const email = Meteor.users.findOne({ _id: this.userId }).emails[0].address; const existingAccounts = Meteor.users.find({'emails.address': {'$in': mods}}).fetch().map(el => el.emails[0].address); const newAccounts = mods.filter(email => !existingAccounts.includes(email)); - newAccounts.forEach(async (item) => { - await Accounts.createUser({ + newAccounts.forEach((item) => { + if (checkValidEmail(item) === false) { + return; + } + Accounts.createUser({ email: item, password: Math.random().toString(36).substr(2, 7), profile: { name: item.slice(0, item.indexOf('@')) } }); - await Accounts.forgotPassword({email: item}, (err) => { - if (err) { - console.log(err); - } - }) }); const instance = Instances.findOne({ _id: instanceid, @@ -305,16 +308,20 @@ Meteor.methods({ }, }, (error, count, status) => { if (error) { - keys = error.invalidKeys; + keys['status_code'] = false; + keys['result'] = error.invalidKeys; } }); } else { return false; } - if (keys) { + if (keys && Object.keys(keys).length > 0) { return keys; } - return true; + keys = {}; + keys['status_code'] = true; + keys['result'] = newAccounts; + return keys; } return false; }, From 9d1baa47173cb9048d502de02378e34764066d2b Mon Sep 17 00:00:00 2001 From: singhsanket143 Date: Fri, 23 Aug 2019 01:39:56 +0530 Subject: [PATCH 4/6] Fix: Updated the email text for notification of getting added as mod --- client/views/add/add.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/views/add/add.js b/client/views/add/add.js index 168c1df7..e2c68a1c 100644 --- a/client/views/add/add.js +++ b/client/views/add/add.js @@ -117,7 +117,7 @@ Template.add.events({ mods[m], Meteor.user().emails[0].address, 'You have been added as a moderator on Question Tool', - Meteor.user().profile.name + ' added you as a moderator of ' + template.data.tablename + ' at ' + Iron.Location.get().originalUrl + ' on Question Tool. You are able to modify, combine, and hide questions. You must use this email address when registering to be considered a moderator.'); + Meteor.user().profile.name + ' added you as a moderator of ' + template.data.tablename + ' at ' + Iron.Location.get().originalUrl + ' on Question Tool. You are able to modify, combine, and hide questions.'); } let boxes = document.getElementsByClassName('newmod'); boxes = boxes[boxes.length - 1]; From 0cdcb3f849951822b39b515d2fb1094f5ca18e07 Mon Sep 17 00:00:00 2001 From: singhsanket143 Date: Fri, 23 Aug 2019 01:42:06 +0530 Subject: [PATCH 5/6] Refactor: Refactor the log statements and added alert on error --- client/views/add/add.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/views/add/add.js b/client/views/add/add.js index e2c68a1c..74afb450 100644 --- a/client/views/add/add.js +++ b/client/views/add/add.js @@ -90,7 +90,6 @@ Template.add.events({ } } Meteor.call('addMods', mods, template.data._id, (error, response) => { - console.log(response); if (typeof response === 'object' && response['status_code'] === false && response['result'].length > 0) { // Alert the error for (let i = 0; i < response['result'].length; i++) { @@ -107,6 +106,7 @@ Template.add.events({ for(let k = 0; k < response['result'].length; k++) { Accounts.forgotPassword({email: response['result'][k]}, function(err) { if (err) { + showModsError('Some error occured while adding the moderator. Please try again.'); console.log(err); } }) From 98150491e13d3837642263aead088295e36ead0f Mon Sep 17 00:00:00 2001 From: singhsanket143 Date: Fri, 23 Aug 2019 01:51:02 +0530 Subject: [PATCH 6/6] Fix: Added the condition to check if the user is authorized to add moderators or not --- server/methods.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server/methods.js b/server/methods.js index aac976a0..acc637c8 100644 --- a/server/methods.js +++ b/server/methods.js @@ -273,7 +273,13 @@ Meteor.methods({ addMods(mods, instanceid) { if (this.userId) { var keys = {}; + const instance = Instances.findOne({ + _id: instanceid, + }); const email = Meteor.users.findOne({ _id: this.userId }).emails[0].address; + if(instance.admin !== email) { + return false; + } const existingAccounts = Meteor.users.find({'emails.address': {'$in': mods}}).fetch().map(el => el.emails[0].address); const newAccounts = mods.filter(email => !existingAccounts.includes(email)); newAccounts.forEach((item) => { @@ -288,9 +294,6 @@ Meteor.methods({ } }); }); - const instance = Instances.findOne({ - _id: instanceid, - }); var found = mods.find(function(element) { return element === instance.admin; });