settings.js 8.8 KB

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