settings.js 3.8 KB

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