collections.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. Websites.allow({
  2. insert: function (userId, doc) {
  3. // Ensure sane arguments.
  4. check(doc, {
  5. url: String,
  6. title: String,
  7. description: String,
  8. poster: Object,
  9. postDate: Date
  10. });
  11. // Reject anonymous inserts.
  12. if (!userId) {
  13. throw new Meteor.Error("logged-out", "User must be logged in to post a site.");
  14. // return false;
  15. }
  16. // Reject non-new inserts.
  17. const url = doc.url;
  18. if (Websites.findOne({ url })) {
  19. throw new Meteor.Error("duplicate", "User may only post new sites.");
  20. // return false;
  21. }
  22. // Reject wrong-looking URLs
  23. // TODO: find a validation package usable server-side.
  24. // The popular themeteorchef:jquery-validation appears to be client-only.
  25. // For now using a very limited check.
  26. const URL_BOGO_REGEX = /^https?:\/\/.+$/;
  27. if (!URL_BOGO_REGEX.test(doc.url)) {
  28. throw new Meteor.Error("bad-url", "Users may only post http(s) URLs.");
  29. // return false;
  30. }
  31. // Reject empty titles and descriptions.
  32. if (doc.title === "") {
  33. throw new Meteor.Error("empty-title", "Title may not be empty");
  34. // return false;
  35. }
  36. if (doc.description === "") {
  37. throw new Meteor.Error("empty-description", "Description may not be empty");
  38. // return false;
  39. }
  40. return true;
  41. },
  42. /**
  43. * Access check for update operations. NOT SAFE: needs deeper modifier checks.
  44. *
  45. * @param {String} userId
  46. * The user attempting the modification.
  47. * @param {Object} doc
  48. * The original document to modify.
  49. * @param {Array} fields
  50. * The list of affected fields.
  51. * @param {Object} modifier
  52. * The MongoDB update modifier.
  53. * @returns {boolean}
  54. * True to allow update.
  55. */
  56. update: function (userId, doc, fields, modifier) {
  57. if (!userId) {
  58. throw new Meteor.Error("logged-out", "User must be logged to vote on a site.");
  59. }
  60. const orderedFields = fields.sort();
  61. if (!_.isEqual(orderedFields, ["minus", "minusScore"]) &&
  62. !_.isEqual(orderedFields, ["plus", "plusScore"]) &&
  63. !_.isEqual(orderedFields, ["minus", "minusScore", "plus", "plusScore"]) &&
  64. !_.isEqual(orderedFields, ["comments"])) {
  65. throw new Meteor.Error("invalid-field", "May only update minus[Score] and plus[Score] or comments.");
  66. }
  67. // FIXME : check modifier.
  68. return true;
  69. }
  70. });