Skip to content
24 changes: 17 additions & 7 deletions client/views/add/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,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.');
Expand All @@ -87,27 +89,35 @@ 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) => {
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) {
showModsError('Some error occured while adding the moderator. Please try again.');
console.log(err);
}
})
}
}
for (let m = 0; m < mods.length; m++) {
Meteor.call('sendEmail',
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];
Expand Down
36 changes: 31 additions & 5 deletions server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -267,11 +272,28 @@ Meteor.methods({
},
addMods(mods, instanceid) {
Comment thread
peter-hank marked this conversation as resolved.
if (this.userId) {
let keys;
const email = Meteor.users.findOne({ _id: this.userId }).emails[0].address;
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) => {
if (checkValidEmail(item) === false) {
return;
}
Accounts.createUser({
email: item,
password: Math.random().toString(36).substr(2, 7),
profile: {
name: item.slice(0, item.indexOf('@'))
}
});
});
var found = mods.find(function(element) {
return element === instance.admin;
});
Expand All @@ -289,16 +311,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;
},
Expand Down