settings.js 9.6 KB

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