settings.js 9.7 KB

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