settings.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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}:${encodeURIComponent(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}:${encodeURIComponent(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 getEnvVar(name){
  95. const value = process.env[name];
  96. if (value){
  97. return value;
  98. }
  99. throw new Meteor.Error(['var-not-exist', `The environment variable ${name} does not exist`]);
  100. }
  101. function sendInvitationEmail (_id){
  102. const icode = InvitationCodes.findOne(_id);
  103. const author = Users.findOne(Meteor.userId());
  104. try {
  105. const params = {
  106. email: icode.email,
  107. inviter: Users.findOne(icode.authorId).username,
  108. user: icode.email.split('@')[0],
  109. icode: icode.code,
  110. url: FlowRouter.url('sign-up'),
  111. };
  112. const lang = author.getLanguage();
  113. Email.send({
  114. to: icode.email,
  115. from: Accounts.emailTemplates.from,
  116. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  117. text: TAPi18n.__('email-invite-register-text', params, lang),
  118. });
  119. } catch (e) {
  120. InvitationCodes.remove(_id);
  121. throw new Meteor.Error('email-fail', e.message);
  122. }
  123. }
  124. function isLdapEnabled() {
  125. return process.env.LDAP_ENABLE === 'true';
  126. }
  127. function isOauth2Enabled() {
  128. return process.env.OAUTH2_ENABLED === 'true';
  129. }
  130. function isCasEnabled() {
  131. return process.env.CAS_ENABLED === 'true';
  132. }
  133. Meteor.methods({
  134. sendInvitation(emails, boards) {
  135. check(emails, [String]);
  136. check(boards, [String]);
  137. const user = Users.findOne(Meteor.userId());
  138. if(!user.isAdmin){
  139. throw new Meteor.Error('not-allowed');
  140. }
  141. emails.forEach((email) => {
  142. if (email && SimpleSchema.RegEx.Email.test(email)) {
  143. // Checks if the email is already link to an account.
  144. const userExist = Users.findOne({email});
  145. if (userExist){
  146. throw new Meteor.Error('user-exist', `The user with the email ${email} has already an account.`);
  147. }
  148. // Checks if the email is already link to an invitation.
  149. const invitation = InvitationCodes.findOne({email});
  150. if (invitation){
  151. InvitationCodes.update(invitation, {$set : {boardsToBeInvited: boards}});
  152. sendInvitationEmail(invitation._id);
  153. }else {
  154. const code = getRandomNum(100000, 999999);
  155. InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
  156. if (!err && _id) {
  157. sendInvitationEmail(_id);
  158. } else {
  159. throw new Meteor.Error('invitation-generated-fail', err.message);
  160. }
  161. });
  162. }
  163. }
  164. });
  165. },
  166. sendSMTPTestEmail() {
  167. if (!Meteor.userId()) {
  168. throw new Meteor.Error('invalid-user');
  169. }
  170. const user = Meteor.user();
  171. if (!user.emails && !user.emails[0] && user.emails[0].address) {
  172. throw new Meteor.Error('email-invalid');
  173. }
  174. this.unblock();
  175. const lang = user.getLanguage();
  176. try {
  177. Email.send({
  178. to: user.emails[0].address,
  179. from: Accounts.emailTemplates.from,
  180. subject: TAPi18n.__('email-smtp-test-subject', {lng: lang}),
  181. text: TAPi18n.__('email-smtp-test-text', {lng: lang}),
  182. });
  183. } catch ({message}) {
  184. throw new Meteor.Error('email-fail', `${TAPi18n.__('email-fail-text', {lng: lang})}: ${ message }`, message);
  185. }
  186. return {
  187. message: 'email-sent',
  188. email: user.emails[0].address,
  189. };
  190. },
  191. getMatomoConf(){
  192. return {
  193. address: getEnvVar('MATOMO_ADDRESS'),
  194. siteId: getEnvVar('MATOMO_SITE_ID'),
  195. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  196. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  197. };
  198. },
  199. _isLdapEnabled() {
  200. return isLdapEnabled();
  201. },
  202. _isOauth2Enabled() {
  203. return isOauth2Enabled();
  204. },
  205. _isCasEnabled() {
  206. return isCasEnabled();
  207. },
  208. // Gets all connection methods to use it in the Template
  209. getAuthenticationsEnabled() {
  210. return {
  211. ldap: isLdapEnabled(),
  212. oauth2: isOauth2Enabled(),
  213. cas: isCasEnabled(),
  214. };
  215. },
  216. });
  217. }