settings.js 9.5 KB

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