a.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Meteor.isBanned = function() {
  2. var userData = Meteor.users.findOne(Meteor.userId());
  3. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.ban !== undefined) {
  4. var ban = userData.punishments.ban;
  5. if (new Date(ban.bannedUntil).getTime() <= new Date().getTime()) {
  6. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.ban": ""}});
  7. return false;
  8. } else {
  9. return true;
  10. }
  11. } else {
  12. return false;
  13. }
  14. };
  15. Meteor.isMuted = function() {
  16. var userData = Meteor.users.findOne(Meteor.userId());
  17. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.mute !== undefined) {
  18. var mute = userData.punishments.mute;
  19. if (new Date(mute.bannedUntil).getTime() <= new Date().getTime()) {
  20. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.mute": ""}});
  21. return false;
  22. } else {
  23. return true;
  24. }
  25. } else {
  26. return false;
  27. }
  28. };
  29. Meteor.updatedMethods = function(methods) {
  30. var methodsNames = _.keys(methods);
  31. _.each(methodsNames, function(methodName) {
  32. var method = {};
  33. method[methodName] = function() {
  34. if (typeof methods[methodName] === "function") {
  35. return methods[methodName].apply(this, arguments);
  36. } else {
  37. if (Meteor.isBanned()) {
  38. throw new Meteor.Error(401, "Invalid permissions.");
  39. }
  40. _.each(methods[methodName].requirements, function(requirement) {
  41. if (requirement === "moderator") {
  42. if (!Meteor.userId() || !Meteor.user() || !(Meteor.user().profile.rank === "admin" || Meteor.user().profile.type === "moderator")) {
  43. throw new Meteor.Error(401, "Invalid permissions.");
  44. }
  45. } else if (requirement === "admin") {
  46. if (!Meteor.userId() || !Meteor.user() || Meteor.user().profile.rank !== "admin") {
  47. throw new Meteor.Error(401, "Invalid permissions.");
  48. }
  49. } else if (requirement === "login") {
  50. if (!Meteor.userId() || !Meteor.user()) {
  51. throw new Meteor.Error(401, "Invalid permissions.");
  52. }
  53. } else if (requirement === "noLogin") {
  54. if (Meteor.userId() || Meteor.user()) {
  55. throw new Meteor.Error(401, "Invalid permissions.");
  56. }
  57. } else if (requirement === "noMute") {
  58. if (Meteor.isMuted()) {
  59. throw new Meteor.Error(401, "Invalid permissions.");
  60. }
  61. }
  62. });
  63. return methods[methodName].code.apply(this, arguments);
  64. }
  65. };
  66. Meteor.methods(method);
  67. });
  68. };