settings.js 4.4 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. },
  30. createdAt: {
  31. type: Date,
  32. denyUpdate: true,
  33. },
  34. modifiedAt: {
  35. type: Date,
  36. },
  37. }));
  38. Settings.helpers({
  39. mailUrl () {
  40. if (!this.mailServer.host) {
  41. return null;
  42. }
  43. const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  44. if (!this.mailServer.username && !this.mailServer.password) {
  45. return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
  46. }
  47. return `${protocol}${this.mailServer.username}:${this.mailServer.password}@${this.mailServer.host}:${this.mailServer.port}/`;
  48. },
  49. });
  50. Settings.allow({
  51. update(userId) {
  52. const user = Users.findOne(userId);
  53. return user && user.isAdmin;
  54. },
  55. });
  56. Settings.before.update((userId, doc, fieldNames, modifier) => {
  57. modifier.$set = modifier.$set || {};
  58. modifier.$set.modifiedAt = new Date();
  59. });
  60. if (Meteor.isServer) {
  61. Meteor.startup(() => {
  62. const setting = Settings.findOne({});
  63. if(!setting){
  64. const now = new Date();
  65. const domain = process.env.ROOT_URL.match(/\/\/(?:www\.)?(.*)?(?:\/)?/)[1];
  66. const from = `Wekan <wekan@${domain}>`;
  67. const defaultSetting = {disableRegistration: false, mailServer: {
  68. username: '', password: '', host: '', port: '', enableTLS: false, from,
  69. }, createdAt: now, modifiedAt: now};
  70. Settings.insert(defaultSetting);
  71. }
  72. const newSetting = Settings.findOne();
  73. if (!process.env.MAIL_URL && newSetting.mailUrl())
  74. process.env.MAIL_URL = newSetting.mailUrl();
  75. Accounts.emailTemplates.from = process.env.MAIL_FROM ? process.env.MAIL_FROM : newSetting.mailServer.from;
  76. });
  77. Settings.after.update((userId, doc, fieldNames) => {
  78. // assign new values to mail-from & MAIL_URL in environment
  79. if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
  80. const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  81. if (!doc.mailServer.username && !doc.mailServer.password) {
  82. process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
  83. } else {
  84. process.env.MAIL_URL = `${protocol}${doc.mailServer.username}:${doc.mailServer.password}@${doc.mailServer.host}:${doc.mailServer.port}/`;
  85. }
  86. Accounts.emailTemplates.from = doc.mailServer.from;
  87. }
  88. });
  89. function getRandomNum (min, max) {
  90. const range = max - min;
  91. const rand = Math.random();
  92. return (min + Math.round(rand * range));
  93. }
  94. function sendInvitationEmail (_id){
  95. const icode = InvitationCodes.findOne(_id);
  96. const author = Users.findOne(Meteor.userId());
  97. try {
  98. const params = {
  99. email: icode.email,
  100. inviter: Users.findOne(icode.authorId).username,
  101. user: icode.email.split('@')[0],
  102. icode: icode.code,
  103. url: FlowRouter.url('sign-up'),
  104. };
  105. const lang = author.getLanguage();
  106. Email.send({
  107. to: icode.email,
  108. from: Accounts.emailTemplates.from,
  109. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  110. text: TAPi18n.__('email-invite-register-text', params, lang),
  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. }