settings.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. var nodemailer = require('nodemailer');
  2. // Sandstorm context is detected using the METEOR_SETTINGS environment variable
  3. // in the package definition.
  4. const isSandstorm =
  5. Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm;
  6. Settings = new Mongo.Collection('settings');
  7. Settings.attachSchema(
  8. new SimpleSchema({
  9. disableRegistration: {
  10. type: Boolean,
  11. },
  12. 'mailServer.username': {
  13. type: String,
  14. optional: true,
  15. },
  16. 'mailServer.password': {
  17. type: String,
  18. optional: true,
  19. },
  20. 'mailServer.host': {
  21. type: String,
  22. optional: true,
  23. },
  24. 'mailServer.port': {
  25. type: String,
  26. optional: true,
  27. },
  28. 'mailServer.enableTLS': {
  29. type: Boolean,
  30. optional: true,
  31. },
  32. 'mailServer.from': {
  33. type: String,
  34. optional: true,
  35. },
  36. productName: {
  37. type: String,
  38. optional: true,
  39. },
  40. displayAuthenticationMethod: {
  41. type: Boolean,
  42. optional: true,
  43. },
  44. defaultAuthenticationMethod: {
  45. type: String,
  46. optional: false,
  47. },
  48. spinnerName: {
  49. type: String,
  50. optional: true,
  51. },
  52. hideLogo: {
  53. type: Boolean,
  54. optional: true,
  55. },
  56. customLoginLogoImageUrl: {
  57. type: String,
  58. optional: true,
  59. },
  60. customLoginLogoLinkUrl: {
  61. type: String,
  62. optional: true,
  63. },
  64. textBelowCustomLoginLogo: {
  65. type: String,
  66. optional: true,
  67. },
  68. automaticLinkedUrlSchemes: {
  69. type: String,
  70. optional: true,
  71. },
  72. customTopLeftCornerLogoImageUrl: {
  73. type: String,
  74. optional: true,
  75. },
  76. customTopLeftCornerLogoLinkUrl: {
  77. type: String,
  78. optional: true,
  79. },
  80. customTopLeftCornerLogoHeight: {
  81. type: String,
  82. optional: true,
  83. },
  84. oidcBtnText: {
  85. type: String,
  86. optional: true,
  87. },
  88. createdAt: {
  89. type: Date,
  90. denyUpdate: true,
  91. // eslint-disable-next-line consistent-return
  92. autoValue() {
  93. if (this.isInsert) {
  94. return new Date();
  95. } else if (this.isUpsert) {
  96. return { $setOnInsert: new Date() };
  97. } else {
  98. this.unset();
  99. }
  100. },
  101. },
  102. modifiedAt: {
  103. type: Date,
  104. // eslint-disable-next-line consistent-return
  105. autoValue() {
  106. if (this.isInsert || this.isUpsert || this.isUpdate) {
  107. return new Date();
  108. } else {
  109. this.unset();
  110. }
  111. },
  112. },
  113. }),
  114. );
  115. Settings.helpers({
  116. mailUrl() {
  117. if (!this.mailServer.host) {
  118. return null;
  119. }
  120. const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  121. if (!this.mailServer.username && !this.mailServer.password) {
  122. return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
  123. }
  124. return `${protocol}${this.mailServer.username}:${encodeURIComponent(
  125. this.mailServer.password,
  126. )}@${this.mailServer.host}:${this.mailServer.port}/`;
  127. },
  128. });
  129. Settings.allow({
  130. update(userId) {
  131. const user = Users.findOne(userId);
  132. return user && user.isAdmin;
  133. },
  134. });
  135. if (Meteor.isServer) {
  136. Meteor.startup(() => {
  137. Settings._collection._ensureIndex({ modifiedAt: -1 });
  138. const setting = Settings.findOne({});
  139. if (!setting) {
  140. const now = new Date();
  141. const domain = process.env.ROOT_URL.match(
  142. /\/\/(?:www\.)?(.*)?(?:\/)?/,
  143. )[1];
  144. const from = `Boards Support <support@${domain}>`;
  145. const defaultSetting = {
  146. disableRegistration: false,
  147. mailServer: {
  148. username: '',
  149. password: '',
  150. host: '',
  151. port: '',
  152. enableTLS: false,
  153. from,
  154. },
  155. createdAt: now,
  156. modifiedAt: now,
  157. displayAuthenticationMethod: true,
  158. defaultAuthenticationMethod: 'password',
  159. };
  160. Settings.insert(defaultSetting);
  161. }
  162. if (isSandstorm) {
  163. // At Sandstorm, Admin Panel has SMTP settings
  164. const newSetting = Settings.findOne();
  165. if (!process.env.MAIL_URL && newSetting.mailUrl())
  166. process.env.MAIL_URL = newSetting.mailUrl();
  167. Accounts.emailTemplates.from = process.env.MAIL_FROM
  168. ? process.env.MAIL_FROM
  169. : newSetting.mailServer.from;
  170. } else {
  171. // Not running on Sandstorm, so using environment variables
  172. Accounts.emailTemplates.from = process.env.MAIL_FROM;
  173. }
  174. });
  175. if (isSandstorm) {
  176. // At Sandstorm Wekan Admin Panel, save SMTP settings.
  177. Settings.after.update((userId, doc, fieldNames) => {
  178. // assign new values to mail-from & MAIL_URL in environment
  179. if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
  180. const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  181. if (!doc.mailServer.username && !doc.mailServer.password) {
  182. process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
  183. } else {
  184. process.env.MAIL_URL = `${protocol}${
  185. doc.mailServer.username
  186. }:${encodeURIComponent(doc.mailServer.password)}@${
  187. doc.mailServer.host
  188. }:${doc.mailServer.port}/`;
  189. }
  190. Accounts.emailTemplates.from = doc.mailServer.from;
  191. }
  192. });
  193. }
  194. function getRandomNum(min, max) {
  195. const range = max - min;
  196. const rand = Math.random();
  197. return min + Math.round(rand * range);
  198. }
  199. function getEnvVar(name) {
  200. const value = process.env[name];
  201. if (value) {
  202. return value;
  203. }
  204. throw new Meteor.Error([
  205. 'var-not-exist',
  206. `The environment variable ${name} does not exist`,
  207. ]);
  208. }
  209. function sendInvitationEmail(_id) {
  210. const icode = InvitationCodes.findOne(_id);
  211. const author = Users.findOne(Meteor.userId());
  212. try {
  213. const params = {
  214. email: icode.email,
  215. inviter: Users.findOne(icode.authorId).username,
  216. user: icode.email.split('@')[0],
  217. icode: icode.code,
  218. url: FlowRouter.url('sign-up'),
  219. };
  220. const lang = author.getLanguage();
  221. if (process.env.MAIL_SERVICE !== '') {
  222. let transporter = nodemailer.createTransport({
  223. service: process.env.MAIL_SERVICE,
  224. auth: {
  225. user: process.env.MAIL_SERVICE_USER,
  226. pass: process.env.MAIL_SERVICE_PASSWORD
  227. },
  228. })
  229. let info = transporter.sendMail({
  230. to: icode.email,
  231. from: Accounts.emailTemplates.from,
  232. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  233. text: TAPi18n.__('email-invite-register-text', params, lang),
  234. })
  235. } else {
  236. Email.send({
  237. to: icode.email,
  238. from: Accounts.emailTemplates.from,
  239. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  240. text: TAPi18n.__('email-invite-register-text', params, lang),
  241. });
  242. }
  243. } catch (e) {
  244. InvitationCodes.remove(_id);
  245. throw new Meteor.Error('email-fail', e.message);
  246. }
  247. }
  248. function isLdapEnabled() {
  249. return (
  250. process.env.LDAP_ENABLE === 'true' || process.env.LDAP_ENABLE === true
  251. );
  252. }
  253. function isOauth2Enabled() {
  254. return (
  255. process.env.OAUTH2_ENABLED === 'true' ||
  256. process.env.OAUTH2_ENABLED === true
  257. );
  258. }
  259. function isCasEnabled() {
  260. return (
  261. process.env.CAS_ENABLED === 'true' || process.env.CAS_ENABLED === true
  262. );
  263. }
  264. function isApiEnabled() {
  265. return process.env.WITH_API === 'true' || process.env.WITH_API === true;
  266. }
  267. Meteor.methods({
  268. sendInvitation(emails, boards) {
  269. check(emails, [String]);
  270. check(boards, [String]);
  271. const user = Users.findOne(Meteor.userId());
  272. if (!user.isAdmin) {
  273. throw new Meteor.Error('not-allowed');
  274. }
  275. emails.forEach(email => {
  276. if (email && SimpleSchema.RegEx.Email.test(email)) {
  277. // Checks if the email is already link to an account.
  278. const userExist = Users.findOne({ email });
  279. if (userExist) {
  280. throw new Meteor.Error(
  281. 'user-exist',
  282. `The user with the email ${email} has already an account.`,
  283. );
  284. }
  285. // Checks if the email is already link to an invitation.
  286. const invitation = InvitationCodes.findOne({ email });
  287. if (invitation) {
  288. InvitationCodes.update(invitation, {
  289. $set: { boardsToBeInvited: boards },
  290. });
  291. sendInvitationEmail(invitation._id);
  292. } else {
  293. const code = getRandomNum(100000, 999999);
  294. InvitationCodes.insert(
  295. {
  296. code,
  297. email,
  298. boardsToBeInvited: boards,
  299. createdAt: new Date(),
  300. authorId: Meteor.userId(),
  301. },
  302. function(err, _id) {
  303. if (!err && _id) {
  304. sendInvitationEmail(_id);
  305. } else {
  306. throw new Meteor.Error(
  307. 'invitation-generated-fail',
  308. err.message,
  309. );
  310. }
  311. },
  312. );
  313. }
  314. }
  315. });
  316. },
  317. sendSMTPTestEmail() {
  318. if (!Meteor.userId()) {
  319. throw new Meteor.Error('invalid-user');
  320. }
  321. const user = Meteor.user();
  322. if (!user.emails || !user.emails[0] || !user.emails[0].address) {
  323. throw new Meteor.Error('email-invalid');
  324. }
  325. this.unblock();
  326. const lang = user.getLanguage();
  327. try {
  328. if (process.env.MAIL_SERVICE !== '') {
  329. let transporter = nodemailer.createTransport({
  330. service: process.env.MAIL_SERVICE,
  331. auth: {
  332. user: process.env.MAIL_SERVICE_USER,
  333. pass: process.env.MAIL_SERVICE_PASSWORD
  334. },
  335. })
  336. let info = transporter.sendMail({
  337. to: user.emails[0].address,
  338. from: Accounts.emailTemplates.from,
  339. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  340. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  341. })
  342. } else {
  343. Email.send({
  344. to: user.emails[0].address,
  345. from: Accounts.emailTemplates.from,
  346. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  347. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  348. });
  349. }
  350. } catch ({ message }) {
  351. throw new Meteor.Error(
  352. 'email-fail',
  353. `${TAPi18n.__('email-fail-text', { lng: lang })}: ${message}`,
  354. message,
  355. );
  356. }
  357. return {
  358. message: 'email-sent',
  359. email: user.emails[0].address,
  360. };
  361. },
  362. getCustomUI() {
  363. const setting = Settings.findOne({});
  364. if (!setting.productName) {
  365. return {
  366. productName: '',
  367. };
  368. } else {
  369. return {
  370. productName: `${setting.productName}`,
  371. };
  372. }
  373. },
  374. getMatomoConf() {
  375. return {
  376. address: getEnvVar('MATOMO_ADDRESS'),
  377. siteId: getEnvVar('MATOMO_SITE_ID'),
  378. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  379. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  380. };
  381. },
  382. _isLdapEnabled() {
  383. return isLdapEnabled();
  384. },
  385. _isOauth2Enabled() {
  386. return isOauth2Enabled();
  387. },
  388. _isCasEnabled() {
  389. return isCasEnabled();
  390. },
  391. _isApiEnabled() {
  392. return isApiEnabled();
  393. },
  394. // Gets all connection methods to use it in the Template
  395. getAuthenticationsEnabled() {
  396. return {
  397. ldap: isLdapEnabled(),
  398. oauth2: isOauth2Enabled(),
  399. cas: isCasEnabled(),
  400. };
  401. },
  402. getDefaultAuthenticationMethod() {
  403. return process.env.DEFAULT_AUTHENTICATION_METHOD;
  404. },
  405. isPasswordLoginDisabled() {
  406. return process.env.PASSWORD_LOGIN_ENABLED === 'false';
  407. },
  408. });
  409. }
  410. export default Settings;