collections.js 2.1 KB

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