settings.js 7.6 KB

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