settings.js 4.3 KB

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