settings.js 9.0 KB

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