settings.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (!this.mailServer.host) {
  38. return null;
  39. }
  40. if (!this.mailServer.username && !this.mailServer.password) {
  41. return `smtp://${this.mailServer.host}:${this.mailServer.port}/`;
  42. }
  43. return `smtp://${this.mailServer.username}:${this.mailServer.password}@${this.mailServer.host}:${this.mailServer.port}/`;
  44. },
  45. });
  46. Settings.allow({
  47. update(userId) {
  48. const user = Users.findOne(userId);
  49. return user && user.isAdmin;
  50. },
  51. });
  52. Settings.before.update((userId, doc, fieldNames, modifier) => {
  53. modifier.$set = modifier.$set || {};
  54. modifier.$set.modifiedAt = new Date();
  55. });
  56. if (Meteor.isServer) {
  57. Meteor.startup(() => {
  58. const setting = Settings.findOne({});
  59. if(!setting){
  60. const now = new Date();
  61. const defaultSetting = {disableRegistration: false, mailServer: {
  62. username: '', password:'', host: '', port:'', from: '',
  63. }, createdAt: now, modifiedAt: now};
  64. Settings.insert(defaultSetting);
  65. }
  66. const newSetting = Settings.findOne();
  67. process.env.MAIL_URL = newSetting.mailUrl();
  68. Accounts.emailTemplates.from = newSetting.mailServer.from;
  69. });
  70. Settings.after.update((userId, doc, fieldNames) => {
  71. // assign new values to mail-from & MAIL_URL in environment
  72. if (_.contains(fieldNames, 'mailServer') && _.contains(fieldNames, 'host')) {
  73. if (!doc.mailServer.username && !doc.mailServer.password) {
  74. process.env.MAIL_URL = `smtp://${doc.mailServer.host}:${doc.mailServer.port}/`;
  75. } else {
  76. process.env.MAIL_URL = `smtp://${doc.mailServer.username}:${doc.mailServer.password}@${doc.mailServer.host}:${doc.mailServer.port}/`;
  77. }
  78. Accounts.emailTemplates.from = doc.mailServer.from;
  79. }
  80. });
  81. function getRandomNum (min, max) {
  82. const range = max - min;
  83. const rand = Math.random();
  84. return (min + Math.round(rand * range));
  85. }
  86. function sendInvitationEmail (_id){
  87. const icode = InvitationCodes.findOne(_id);
  88. const author = Users.findOne(Meteor.userId());
  89. try {
  90. const params = {
  91. email: icode.email,
  92. inviter: Users.findOne(icode.authorId).username,
  93. user: icode.email.split('@')[0],
  94. icode: icode.code,
  95. url: FlowRouter.url('sign-up'),
  96. };
  97. const lang = author.getLanguage();
  98. if (Settings.findOne().mailUrl()) {
  99. Email.send({
  100. to: icode.email,
  101. from: Accounts.emailTemplates.from,
  102. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  103. text: TAPi18n.__('email-invite-register-text', params, lang),
  104. });
  105. }
  106. } catch (e) {
  107. InvitationCodes.remove(_id);
  108. throw new Meteor.Error('email-fail', e.message);
  109. }
  110. }
  111. Meteor.methods({
  112. sendInvitation(emails, boards) {
  113. check(emails, [String]);
  114. check(boards, [String]);
  115. const user = Users.findOne(Meteor.userId());
  116. if(!user.isAdmin){
  117. throw new Meteor.Error('not-allowed');
  118. }
  119. emails.forEach((email) => {
  120. if (email && SimpleSchema.RegEx.Email.test(email)) {
  121. const code = getRandomNum(100000, 999999);
  122. InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
  123. if (!err && _id) {
  124. sendInvitationEmail(_id);
  125. } else {
  126. throw new Meteor.Error('invitation-generated-fail', err.message);
  127. }
  128. });
  129. }
  130. });
  131. },
  132. });
  133. }