settings.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. Settings = new Mongo.Collection('settings');
  2. Settings.attachSchema(
  3. new SimpleSchema({
  4. disableRegistration: {
  5. type: Boolean,
  6. },
  7. 'mailServer.username': {
  8. type: String,
  9. optional: true,
  10. },
  11. 'mailServer.password': {
  12. type: String,
  13. optional: true,
  14. },
  15. 'mailServer.host': {
  16. type: String,
  17. optional: true,
  18. },
  19. 'mailServer.port': {
  20. type: String,
  21. optional: true,
  22. },
  23. 'mailServer.enableTLS': {
  24. type: Boolean,
  25. optional: true,
  26. },
  27. 'mailServer.from': {
  28. type: String,
  29. optional: true,
  30. },
  31. productName: {
  32. type: String,
  33. optional: true,
  34. },
  35. customHTMLafterBodyStart: {
  36. type: String,
  37. optional: true,
  38. },
  39. customHTMLbeforeBodyEnd: {
  40. type: String,
  41. optional: true,
  42. },
  43. displayAuthenticationMethod: {
  44. type: Boolean,
  45. optional: true,
  46. },
  47. defaultAuthenticationMethod: {
  48. type: String,
  49. optional: false,
  50. },
  51. hideLogo: {
  52. type: Boolean,
  53. optional: true,
  54. },
  55. createdAt: {
  56. type: Date,
  57. denyUpdate: true,
  58. // eslint-disable-next-line consistent-return
  59. autoValue() {
  60. if (this.isInsert) {
  61. return new Date();
  62. } else if (this.isUpsert) {
  63. return { $setOnInsert: new Date() };
  64. } else {
  65. this.unset();
  66. }
  67. },
  68. },
  69. modifiedAt: {
  70. type: Date,
  71. // eslint-disable-next-line consistent-return
  72. autoValue() {
  73. if (this.isInsert || this.isUpsert || this.isUpdate) {
  74. return new Date();
  75. } else {
  76. this.unset();
  77. }
  78. },
  79. },
  80. }),
  81. );
  82. Settings.helpers({
  83. mailUrl() {
  84. if (!this.mailServer.host) {
  85. return null;
  86. }
  87. const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  88. if (!this.mailServer.username && !this.mailServer.password) {
  89. return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
  90. }
  91. return `${protocol}${this.mailServer.username}:${encodeURIComponent(
  92. this.mailServer.password,
  93. )}@${this.mailServer.host}:${this.mailServer.port}/`;
  94. },
  95. });
  96. Settings.allow({
  97. update(userId) {
  98. const user = Users.findOne(userId);
  99. return user && user.isAdmin;
  100. },
  101. });
  102. if (Meteor.isServer) {
  103. Meteor.startup(() => {
  104. Settings._collection._ensureIndex({ modifiedAt: -1 });
  105. const setting = Settings.findOne({});
  106. if (!setting) {
  107. const now = new Date();
  108. const domain = process.env.ROOT_URL.match(
  109. /\/\/(?:www\.)?(.*)?(?:\/)?/,
  110. )[1];
  111. const from = `Boards Support <support@${domain}>`;
  112. const defaultSetting = {
  113. disableRegistration: false,
  114. mailServer: {
  115. username: '',
  116. password: '',
  117. host: '',
  118. port: '',
  119. enableTLS: false,
  120. from,
  121. },
  122. createdAt: now,
  123. modifiedAt: now,
  124. displayAuthenticationMethod: true,
  125. defaultAuthenticationMethod: 'password',
  126. };
  127. Settings.insert(defaultSetting);
  128. }
  129. const newSetting = Settings.findOne();
  130. if (!process.env.MAIL_URL && newSetting.mailUrl())
  131. process.env.MAIL_URL = newSetting.mailUrl();
  132. Accounts.emailTemplates.from = process.env.MAIL_FROM
  133. ? process.env.MAIL_FROM
  134. : newSetting.mailServer.from;
  135. });
  136. Settings.after.update((userId, doc, fieldNames) => {
  137. // assign new values to mail-from & MAIL_URL in environment
  138. if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
  139. const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  140. if (!doc.mailServer.username && !doc.mailServer.password) {
  141. process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
  142. } else {
  143. process.env.MAIL_URL = `${protocol}${
  144. doc.mailServer.username
  145. }:${encodeURIComponent(doc.mailServer.password)}@${
  146. doc.mailServer.host
  147. }:${doc.mailServer.port}/`;
  148. }
  149. Accounts.emailTemplates.from = doc.mailServer.from;
  150. }
  151. });
  152. function getRandomNum(min, max) {
  153. const range = max - min;
  154. const rand = Math.random();
  155. return min + Math.round(rand * range);
  156. }
  157. function getEnvVar(name) {
  158. const value = process.env[name];
  159. if (value) {
  160. return value;
  161. }
  162. throw new Meteor.Error([
  163. 'var-not-exist',
  164. `The environment variable ${name} does not exist`,
  165. ]);
  166. }
  167. function sendInvitationEmail(_id) {
  168. const icode = InvitationCodes.findOne(_id);
  169. const author = Users.findOne(Meteor.userId());
  170. try {
  171. const params = {
  172. email: icode.email,
  173. inviter: Users.findOne(icode.authorId).username,
  174. user: icode.email.split('@')[0],
  175. icode: icode.code,
  176. url: FlowRouter.url('sign-up'),
  177. };
  178. const lang = author.getLanguage();
  179. Email.send({
  180. to: icode.email,
  181. from: Accounts.emailTemplates.from,
  182. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  183. text: TAPi18n.__('email-invite-register-text', params, lang),
  184. });
  185. } catch (e) {
  186. InvitationCodes.remove(_id);
  187. throw new Meteor.Error('email-fail', e.message);
  188. }
  189. }
  190. function isLdapEnabled() {
  191. return process.env.LDAP_ENABLE === 'true';
  192. }
  193. function isOauth2Enabled() {
  194. return process.env.OAUTH2_ENABLED === 'true';
  195. }
  196. function isCasEnabled() {
  197. return process.env.CAS_ENABLED === 'true';
  198. }
  199. Meteor.methods({
  200. sendInvitation(emails, boards) {
  201. check(emails, [String]);
  202. check(boards, [String]);
  203. const user = Users.findOne(Meteor.userId());
  204. if (!user.isAdmin) {
  205. throw new Meteor.Error('not-allowed');
  206. }
  207. emails.forEach(email => {
  208. if (email && SimpleSchema.RegEx.Email.test(email)) {
  209. // Checks if the email is already link to an account.
  210. const userExist = Users.findOne({ email });
  211. if (userExist) {
  212. throw new Meteor.Error(
  213. 'user-exist',
  214. `The user with the email ${email} has already an account.`,
  215. );
  216. }
  217. // Checks if the email is already link to an invitation.
  218. const invitation = InvitationCodes.findOne({ email });
  219. if (invitation) {
  220. InvitationCodes.update(invitation, {
  221. $set: { boardsToBeInvited: boards },
  222. });
  223. sendInvitationEmail(invitation._id);
  224. } else {
  225. const code = getRandomNum(100000, 999999);
  226. InvitationCodes.insert(
  227. {
  228. code,
  229. email,
  230. boardsToBeInvited: boards,
  231. createdAt: new Date(),
  232. authorId: Meteor.userId(),
  233. },
  234. function(err, _id) {
  235. if (!err && _id) {
  236. sendInvitationEmail(_id);
  237. } else {
  238. throw new Meteor.Error(
  239. 'invitation-generated-fail',
  240. err.message,
  241. );
  242. }
  243. },
  244. );
  245. }
  246. }
  247. });
  248. },
  249. sendSMTPTestEmail() {
  250. if (!Meteor.userId()) {
  251. throw new Meteor.Error('invalid-user');
  252. }
  253. const user = Meteor.user();
  254. if (!user.emails && !user.emails[0] && user.emails[0].address) {
  255. throw new Meteor.Error('email-invalid');
  256. }
  257. this.unblock();
  258. const lang = user.getLanguage();
  259. try {
  260. Email.send({
  261. to: user.emails[0].address,
  262. from: Accounts.emailTemplates.from,
  263. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  264. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  265. });
  266. } catch ({ message }) {
  267. throw new Meteor.Error(
  268. 'email-fail',
  269. `${TAPi18n.__('email-fail-text', { lng: lang })}: ${message}`,
  270. message,
  271. );
  272. }
  273. return {
  274. message: 'email-sent',
  275. email: user.emails[0].address,
  276. };
  277. },
  278. getCustomUI() {
  279. const setting = Settings.findOne({});
  280. if (!setting.productName) {
  281. return {
  282. productName: '',
  283. };
  284. } else {
  285. return {
  286. productName: `${setting.productName}`,
  287. };
  288. }
  289. },
  290. getMatomoConf() {
  291. return {
  292. address: getEnvVar('MATOMO_ADDRESS'),
  293. siteId: getEnvVar('MATOMO_SITE_ID'),
  294. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  295. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  296. };
  297. },
  298. _isLdapEnabled() {
  299. return isLdapEnabled();
  300. },
  301. _isOauth2Enabled() {
  302. return isOauth2Enabled();
  303. },
  304. _isCasEnabled() {
  305. return isCasEnabled();
  306. },
  307. // Gets all connection methods to use it in the Template
  308. getAuthenticationsEnabled() {
  309. return {
  310. ldap: isLdapEnabled(),
  311. oauth2: isOauth2Enabled(),
  312. cas: isCasEnabled(),
  313. };
  314. },
  315. getDefaultAuthenticationMethod() {
  316. return process.env.DEFAULT_AUTHENTICATION_METHOD;
  317. },
  318. });
  319. }
  320. export default Settings;