settings.js 9.2 KB

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