server formatting

This commit is contained in:
Yaman Qalieh 2016-08-18 06:54:22 -04:00
parent 15f1ba4085
commit 8fe801020c

View File

@ -16,7 +16,7 @@ superadmins = [
]; ];
worktype = ["test", "quiz", "project", "normal"]; worktype = ["test", "quiz", "project", "normal"];
var possiblelist = ["moderators","banned"]; var possiblelist = ["moderators", "banned"];
for (var i = 0; i < superadmins.length; i++) { for (var i = 0; i < superadmins.length; i++) {
var superadmin = superadmins[i]; var superadmin = superadmins[i];
@ -81,7 +81,11 @@ Meteor.publish('users', function() {
if (Roles.userIsInRole(this.userId, ['superadmin', 'admin'])) { if (Roles.userIsInRole(this.userId, ['superadmin', 'admin'])) {
return Meteor.users.find(); return Meteor.users.find();
} else { } else {
return Meteor.users.find({}, {fields: {'services.google.email': 1}}); return Meteor.users.find({}, {
fields: {
'services.google.email': 1
}
});
} }
}); });
@ -160,34 +164,62 @@ Meteor.methods({
} }
}, },
'changeAdmin': function(input) { 'changeAdmin': function(input) {
var found = Meteor.users.find({_id: input[0]}); var found = Meteor.users.find({
var foundclass = classes.find({_id: input[1]}); _id: input[0]
});
var foundclass = classes.find({
_id: input[1]
});
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) {
classes.update({_id: input[1]}, {$set: {admin: input[0]}}); classes.update({
_id: input[1]
}, {
$set: {
admin: input[0]
}
});
} else if (found && foundclass && foundclass.admin == Meteor.userId() && } else if (found && foundclass && foundclass.admin == Meteor.userId() &&
foundclass.banned.indexOf(input[0]) === -1 && foundclass.banned.indexOf(input[0]) === -1 &&
foundclass.subscribers.indexOf(input[0]) !== -1) { foundclass.subscribers.indexOf(input[0]) !== -1) {
classes.update({_id: input[1]}, {$set: {admin: input[0]}}); classes.update({
_id: input[1]
}, {
$set: {
admin: input[0]
}
});
} else { } else {
throw "Unauthorized"; throw "Unauthorized";
} }
}, },
'trackUserInClass': function(input) { 'trackUserInClass': function(input) {
var foundclass = classes.findOne({_id: input[1]}); var foundclass = classes.findOne({
_id: input[1]
});
var userlist = input[2]; var userlist = input[2];
var index = possiblelist.indexOf(input[2]); var index = possiblelist.indexOf(input[2]);
var set = {}; var set = {};
set[userlist] = foundclass[userlist].concat(input[0]); set[userlist] = foundclass[userlist].concat(input[0]);
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) {
classes.update({_id: input[1]}, {$set: set}); classes.update({
_id: input[1]
}, {
$set: set
});
} else if (foundclass && foundclass.admin == Meteor.userId() && index !== -1 && } else if (foundclass && foundclass.admin == Meteor.userId() && index !== -1 &&
(index === 1 || foundclass.moderators.indexOf(Meteor.userId()) !== -1) && (index === 1 || foundclass.moderators.indexOf(Meteor.userId()) !== -1) &&
foundclass[userlist].indexOf(input[0]) === -1) { foundclass[userlist].indexOf(input[0]) === -1) {
classes.update({_id: input[1]}, {$set: set}); classes.update({
_id: input[1]
}, {
$set: set
});
} }
}, },
'untrackUserInClass': function(input) { 'untrackUserInClass': function(input) {
var foundclass = classes.findOne({_id: input[1]}); var foundclass = classes.findOne({
_id: input[1]
});
var userlist = input[2]; var userlist = input[2];
var index = possiblelist.indexOf(input[2]); var index = possiblelist.indexOf(input[2]);
var set = {}; var set = {};
@ -195,11 +227,19 @@ Meteor.methods({
set[userlist] = foundclass[userlist]; set[userlist] = foundclass[userlist];
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) {
classes.update({_id: input[1]}, {$set: set}); classes.update({
_id: input[1]
}, {
$set: set
});
} else if (foundclass && foundclass.admin == Meteor.userId() && index !== -1 && } else if (foundclass && foundclass.admin == Meteor.userId() && index !== -1 &&
(index === 1 || foundclass.moderators.indexOf(Meteor.userId()) !== -1) && (index === 1 || foundclass.moderators.indexOf(Meteor.userId()) !== -1) &&
foundclass[userlist].indexOf(input[0]) !== -1) { foundclass[userlist].indexOf(input[0]) !== -1) {
classes.update({_id: input[1]}, {$set: set}); classes.update({
_id: input[1]
}, {
$set: set
});
} }
}, },
'deleteClass': function(classid) { 'deleteClass': function(classid) {
@ -209,7 +249,9 @@ Meteor.methods({
if (Meteor.user() !== null && found !== null && if (Meteor.user() !== null && found !== null &&
(found.admin === Meteor.user()._id || Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']))) { (found.admin === Meteor.user()._id || Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']))) {
for (var i = 0; i < found.subscribers.length; i++) { for (var i = 0; i < found.subscribers.length; i++) {
var current = Meteor.users.findOne({_id:found.subscribers[i]}).profile; var current = Meteor.users.findOne({
_id: found.subscribers[i]
}).profile;
var index = current.classes.indexOf(classid); var index = current.classes.indexOf(classid);
current.classes.splice(index, 1); current.classes.splice(index, 1);
Meteor.users.update({ Meteor.users.update({
@ -318,7 +360,7 @@ Meteor.methods({
}, { }, {
$set: { $set: {
comments: comments, comments: comments,
user:user, user: user,
time: new Date() time: new Date()
} }
}); });
@ -331,8 +373,7 @@ Meteor.methods({
var currentclass = classes.findOne({ var currentclass = classes.findOne({
_id: workobject.class _id: workobject.class
}); });
if (currentclass.subscribers.indexOf(Meteor.userId()) != -1 && if (currentclass.subscribers.indexOf(Meteor.userId()) != -1 && ["confirmations", "reports", "done"].indexOf(input[1]) != -1) {
["confirmations", "reports", "done"].indexOf(input[1]) != -1) {
userindex = workobject[input[1]].indexOf(Meteor.userId()); userindex = workobject[input[1]].indexOf(Meteor.userId());
if (userindex === -1) { if (userindex === -1) {
workobject[input[1]] = workobject[input[1]].concat(Meteor.userId()); workobject[input[1]] = workobject[input[1]].concat(Meteor.userId());
@ -406,7 +447,13 @@ Meteor.methods({
found !== null && found !== null &&
pass === found.code && pass === found.code &&
prof.classes.indexOf(change) === -1) { prof.classes.indexOf(change) === -1) {
classes.update({_id: found._id}, {$set: {subscribers: found.subscribers.concat(Meteor.userId())}}); classes.update({
_id: found._id
}, {
$set: {
subscribers: found.subscribers.concat(Meteor.userId())
}
});
var current = Meteor.user().profile; var current = Meteor.user().profile;
current.classes = current.classes.concat(change); current.classes = current.classes.concat(change);
Meteor.users.update({ Meteor.users.update({
@ -422,13 +469,29 @@ Meteor.methods({
} }
}, },
'joinPrivateClass': function(input) { 'joinPrivateClass': function(input) {
var found = classes.findOne({status: true, privacy: true, code:input}); var found = classes.findOne({
status: true,
privacy: true,
code: input
});
var current = Meteor.user().profile; var current = Meteor.user().profile;
if (found !== undefined && input !== undefined && if (found !== undefined && input !== undefined &&
current.classes.indexOf(found._id) === -1) { current.classes.indexOf(found._id) === -1) {
classes.update({_id: found._id}, {$set: {subscribers: found.subscribers.concat(Meteor.userId())}}); classes.update({
_id: found._id
}, {
$set: {
subscribers: found.subscribers.concat(Meteor.userId())
}
});
current.classes = current.classes.concat(found._id); current.classes = current.classes.concat(found._id);
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}}); Meteor.users.update({
_id: Meteor.userId()
}, {
$set: {
profile: current
}
});
return true; return true;
} else { } else {
return false; return false;
@ -450,8 +513,16 @@ Meteor.methods({
profile: current profile: current
} }
}); });
var newstudents = classes.findOne({_id: change}).subscribers.splice(Meteor.userId(), 1); var newstudents = classes.findOne({
classes.update({_id: change}, {$set: {subscribers: newstudents}}); _id: change
}).subscribers.splice(Meteor.userId(), 1);
classes.update({
_id: change
}, {
$set: {
subscribers: newstudents
}
});
return 1; return 1;
} else { } else {
throw "You are currently the admin of this class. Transfer ownership in order to leave this class."; throw "You are currently the admin of this class. Transfer ownership in order to leave this class.";