settings.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. productName: {
  31. type: String,
  32. optional: true,
  33. },
  34. createdAt: {
  35. type: Date,
  36. denyUpdate: true,
  37. },
  38. modifiedAt: {
  39. type: Date,
  40. },
  41. }));
  42. Settings.helpers({
  43. mailUrl () {
  44. if (!this.mailServer.host) {
  45. return null;
  46. }
  47. const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  48. if (!this.mailServer.username && !this.mailServer.password) {
  49. return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
  50. }
  51. return `${protocol}${this.mailServer.username}:${encodeURIComponent(this.mailServer.password)}@${this.mailServer.host}:${this.mailServer.port}/`;
  52. },
  53. });
  54. Settings.allow({
  55. update(userId) {
  56. const user = Users.findOne(userId);
  57. return user && user.isAdmin;
  58. },
  59. });
  60. Settings.before.update((userId, doc, fieldNames, modifier) => {
  61. modifier.$set = modifier.$set || {};
  62. modifier.$set.modifiedAt = new Date();
  63. });
  64. if (Meteor.isServer) {
  65. Meteor.startup(() => {
  66. const setting = Settings.findOne({});
  67. if(!setting){
  68. const now = new Date();
  69. const domain = process.env.ROOT_URL.match(/\/\/(?:www\.)?(.*)?(?:\/)?/)[1];
  70. const from = `Wekan <wekan@${domain}>`;
  71. const defaultSetting = {disableRegistration: false, mailServer: {
  72. username: '', password: '', host: '', port: '', enableTLS: false, from,
  73. }, createdAt: now, modifiedAt: now};
  74. Settings.insert(defaultSetting);
  75. }
  76. const newSetting = Settings.findOne();
  77. if (!process.env.MAIL_URL && newSetting.mailUrl())
  78. process.env.MAIL_URL = newSetting.mailUrl();
  79. Accounts.emailTemplates.from = process.env.MAIL_FROM ? process.env.MAIL_FROM : newSetting.mailServer.from;
  80. });
  81. Settings.after.update((userId, doc, fieldNames) => {
  82. // assign new values to mail-from & MAIL_URL in environment
  83. if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
  84. const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  85. if (!doc.mailServer.username && !doc.mailServer.password) {
  86. process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
  87. } else {
  88. process.env.MAIL_URL = `${protocol}${doc.mailServer.username}:${encodeURIComponent(doc.mailServer.password)}@${doc.mailServer.host}:${doc.mailServer.port}/`;
  89. }
  90. Accounts.emailTemplates.from = doc.mailServer.from;
  91. }
  92. });
  93. function getRandomNum (min, max) {
  94. const range = max - min;
  95. const rand = Math.random();
  96. return (min + Math.round(rand * range));
  97. }
  98. function getEnvVar(name){
  99. const value = process.env[name];
  100. if (value){
  101. return value;
  102. }
  103. throw new Meteor.Error(['var-not-exist', `The environment variable ${name} does not exist`]);
  104. }
  105. function sendInvitationEmail (_id){
  106. const icode = InvitationCodes.findOne(_id);
  107. const author = Users.findOne(Meteor.userId());
  108. try {
  109. const params = {
  110. email: icode.email,
  111. inviter: Users.findOne(icode.authorId).username,
  112. user: icode.email.split('@')[0],
  113. icode: icode.code,
  114. url: FlowRouter.url('sign-up'),
  115. };
  116. const lang = author.getLanguage();
  117. Email.send({
  118. to: icode.email,
  119. from: Accounts.emailTemplates.from,
  120. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  121. text: TAPi18n.__('email-invite-register-text', params, lang),
  122. });
  123. } catch (e) {
  124. InvitationCodes.remove(_id);
  125. throw new Meteor.Error('email-fail', e.message);
  126. }
  127. }
  128. function isLdapEnabled() {
  129. return process.env.LDAP_ENABLE === 'true';
  130. }
  131. function isOauth2Enabled() {
  132. return process.env.OAUTH2_ENABLED === 'true';
  133. }
  134. function isCasEnabled() {
  135. return process.env.CAS_ENABLED === 'true';
  136. }
  137. Meteor.methods({
  138. sendInvitation(emails, boards) {
  139. check(emails, [String]);
  140. check(boards, [String]);
  141. const user = Users.findOne(Meteor.userId());
  142. if(!user.isAdmin){
  143. throw new Meteor.Error('not-allowed');
  144. }
  145. emails.forEach((email) => {
  146. if (email && SimpleSchema.RegEx.Email.test(email)) {
  147. // Checks if the email is already link to an account.
  148. const userExist = Users.findOne({email});
  149. if (userExist){
  150. throw new Meteor.Error('user-exist', `The user with the email ${email} has already an account.`);
  151. }
  152. // Checks if the email is already link to an invitation.
  153. const invitation = InvitationCodes.findOne({email});
  154. if (invitation){
  155. InvitationCodes.update(invitation, {$set : {boardsToBeInvited: boards}});
  156. sendInvitationEmail(invitation._id);
  157. }else {
  158. const code = getRandomNum(100000, 999999);
  159. InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
  160. if (!err && _id) {
  161. sendInvitationEmail(_id);
  162. } else {
  163. throw new Meteor.Error('invitation-generated-fail', err.message);
  164. }
  165. });
  166. }
  167. }
  168. });
  169. },
  170. sendSMTPTestEmail() {
  171. if (!Meteor.userId()) {
  172. throw new Meteor.Error('invalid-user');
  173. }
  174. const user = Meteor.user();
  175. if (!user.emails && !user.emails[0] && user.emails[0].address) {
  176. throw new Meteor.Error('email-invalid');
  177. }
  178. this.unblock();
  179. const lang = user.getLanguage();
  180. try {
  181. Email.send({
  182. to: user.emails[0].address,
  183. from: Accounts.emailTemplates.from,
  184. subject: TAPi18n.__('email-smtp-test-subject', {lng: lang}),
  185. text: TAPi18n.__('email-smtp-test-text', {lng: lang}),
  186. });
  187. } catch ({message}) {
  188. throw new Meteor.Error('email-fail', `${TAPi18n.__('email-fail-text', {lng: lang})}: ${ message }`, message);
  189. }
  190. return {
  191. message: 'email-sent',
  192. email: user.emails[0].address,
  193. };
  194. },
  195. getMatomoConf(){
  196. return {
  197. address: getEnvVar('MATOMO_ADDRESS'),
  198. siteId: getEnvVar('MATOMO_SITE_ID'),
  199. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  200. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  201. };
  202. },
  203. _isLdapEnabled() {
  204. return isLdapEnabled();
  205. },
  206. _isOauth2Enabled() {
  207. return isOauth2Enabled();
  208. },
  209. _isCasEnabled() {
  210. return isCasEnabled();
  211. },
  212. // Gets all connection methods to use it in the Template
  213. getAuthenticationsEnabled() {
  214. return {
  215. ldap: isLdapEnabled(),
  216. oauth2: isOauth2Enabled(),
  217. cas: isCasEnabled(),
  218. };
  219. },
  220. });
  221. }