settings.js 7.4 KB

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