settings.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. Settings = new Mongo.Collection('settings');
  2. Settings.attachSchema(new SimpleSchema({
  3. disableRegistration: {
  4. type: Boolean,
  5. },
  6. 'mailServer.username': {
  7. type: String,
  8. optional: true,
  9. },
  10. 'mailServer.password': {
  11. type: String,
  12. optional: true,
  13. },
  14. 'mailServer.host': {
  15. type: String,
  16. optional: true,
  17. },
  18. 'mailServer.port': {
  19. type: String,
  20. optional: true,
  21. },
  22. 'mailServer.from': {
  23. type: String,
  24. optional: true,
  25. defaultValue: 'Wekan',
  26. },
  27. createdAt: {
  28. type: Date,
  29. denyUpdate: true,
  30. },
  31. modifiedAt: {
  32. type: Date,
  33. },
  34. }));
  35. Settings.helpers({
  36. mailUrl () {
  37. const mailUrl = `smtp://${this.mailServer.username}:${this.mailServer.password}@${this.mailServer.host}:${this.mailServer.port}/`;
  38. return mailUrl;
  39. },
  40. });
  41. Settings.allow({
  42. update(userId) {
  43. const user = Users.findOne(userId);
  44. return user && user.isAdmin;
  45. },
  46. });
  47. Settings.before.update((userId, doc, fieldNames, modifier) => {
  48. modifier.$set = modifier.$set || {};
  49. modifier.$set.modifiedAt = new Date();
  50. });
  51. if (Meteor.isServer) {
  52. Meteor.startup(() => {
  53. const setting = Settings.findOne({});
  54. if(!setting){
  55. const now = new Date();
  56. const defaultSetting = {disableRegistration: false, mailServer: {
  57. username: '', password:'', host: '', port:'', from: '',
  58. }, createdAt: now, modifiedAt: now};
  59. Settings.insert(defaultSetting);
  60. }
  61. const newSetting = Settings.findOne();
  62. process.env.MAIL_URL = newSetting.mailUrl();
  63. Accounts.emailTemplates.from = newSetting.mailServer.from;
  64. });
  65. function getRandomNum (min, max) {
  66. const range = max - min;
  67. const rand = Math.random();
  68. return (min + Math.round(rand * range));
  69. }
  70. function sendInvitationEmail (_id){
  71. const icode = InvitationCodes.findOne(_id);
  72. const author = Users.findOne(Meteor.userId());
  73. try {
  74. const params = {
  75. email: icode.email,
  76. inviter: Users.findOne(icode.authorId).username,
  77. user: icode.email.split('@')[0],
  78. icode: icode.code,
  79. url: FlowRouter.url('sign-up'),
  80. };
  81. const lang = author.getLanguage();
  82. Email.send({
  83. to: icode.email,
  84. from: Accounts.emailTemplates.from,
  85. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  86. text: TAPi18n.__('email-invite-register-text', params, lang),
  87. });
  88. } catch (e) {
  89. throw new Meteor.Error('email-fail', e.message);
  90. }
  91. }
  92. Meteor.methods({
  93. sendInvitation(emails, boards) {
  94. check(emails, [String]);
  95. check(boards, [String]);
  96. const user = Users.findOne(Meteor.userId());
  97. if(!user.isAdmin){
  98. throw new Meteor.Error('not-allowed');
  99. }
  100. emails.forEach((email) => {
  101. if (email && SimpleSchema.RegEx.Email.test(email)) {
  102. const code = getRandomNum(100000, 999999);
  103. InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
  104. if(!err && _id) sendInvitationEmail(_id);
  105. });
  106. }
  107. });
  108. },
  109. });
  110. }