settings.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. mailDomainName: {
  89. type: String,
  90. optional: true,
  91. },
  92. legalNotice: {
  93. type: String,
  94. optional: true,
  95. },
  96. createdAt: {
  97. type: Date,
  98. denyUpdate: true,
  99. // eslint-disable-next-line consistent-return
  100. autoValue() {
  101. if (this.isInsert) {
  102. return new Date();
  103. } else if (this.isUpsert) {
  104. return { $setOnInsert: new Date() };
  105. } else {
  106. this.unset();
  107. }
  108. },
  109. },
  110. modifiedAt: {
  111. type: Date,
  112. // eslint-disable-next-line consistent-return
  113. autoValue() {
  114. if (this.isInsert || this.isUpsert || this.isUpdate) {
  115. return new Date();
  116. } else {
  117. this.unset();
  118. }
  119. },
  120. },
  121. }),
  122. );
  123. Settings.helpers({
  124. mailUrl() {
  125. if (!this.mailServer.host) {
  126. return null;
  127. }
  128. const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  129. if (!this.mailServer.username && !this.mailServer.password) {
  130. return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
  131. }
  132. return `${protocol}${this.mailServer.username}:${encodeURIComponent(
  133. this.mailServer.password,
  134. )}@${this.mailServer.host}:${this.mailServer.port}/`;
  135. },
  136. });
  137. Settings.allow({
  138. update(userId) {
  139. const user = Users.findOne(userId);
  140. return user && user.isAdmin;
  141. },
  142. });
  143. if (Meteor.isServer) {
  144. Meteor.startup(() => {
  145. Settings._collection._ensureIndex({ modifiedAt: -1 });
  146. const setting = Settings.findOne({});
  147. if (!setting) {
  148. const now = new Date();
  149. const domain = process.env.ROOT_URL.match(
  150. /\/\/(?:www\.)?(.*)?(?:\/)?/,
  151. )[1];
  152. const from = `Boards Support <support@${domain}>`;
  153. const defaultSetting = {
  154. disableRegistration: false,
  155. mailServer: {
  156. username: '',
  157. password: '',
  158. host: '',
  159. port: '',
  160. enableTLS: false,
  161. from,
  162. },
  163. createdAt: now,
  164. modifiedAt: now,
  165. displayAuthenticationMethod: true,
  166. defaultAuthenticationMethod: 'password',
  167. };
  168. Settings.insert(defaultSetting);
  169. }
  170. if (isSandstorm) {
  171. // At Sandstorm, Admin Panel has SMTP settings
  172. const newSetting = Settings.findOne();
  173. if (!process.env.MAIL_URL && newSetting.mailUrl())
  174. process.env.MAIL_URL = newSetting.mailUrl();
  175. Accounts.emailTemplates.from = process.env.MAIL_FROM
  176. ? process.env.MAIL_FROM
  177. : newSetting.mailServer.from;
  178. } else {
  179. // Not running on Sandstorm, so using environment variables
  180. Accounts.emailTemplates.from = process.env.MAIL_FROM;
  181. }
  182. });
  183. if (isSandstorm) {
  184. // At Sandstorm Wekan Admin Panel, save SMTP settings.
  185. Settings.after.update((userId, doc, fieldNames) => {
  186. // assign new values to mail-from & MAIL_URL in environment
  187. if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
  188. const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  189. if (!doc.mailServer.username && !doc.mailServer.password) {
  190. process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
  191. } else {
  192. process.env.MAIL_URL = `${protocol}${
  193. doc.mailServer.username
  194. }:${encodeURIComponent(doc.mailServer.password)}@${
  195. doc.mailServer.host
  196. }:${doc.mailServer.port}/`;
  197. }
  198. Accounts.emailTemplates.from = doc.mailServer.from;
  199. }
  200. });
  201. }
  202. function getRandomNum(min, max) {
  203. const range = max - min;
  204. const rand = Math.random();
  205. return min + Math.round(rand * range);
  206. }
  207. function getEnvVar(name) {
  208. const value = process.env[name];
  209. if (value) {
  210. return value;
  211. }
  212. throw new Meteor.Error([
  213. 'var-not-exist',
  214. `The environment variable ${name} does not exist`,
  215. ]);
  216. }
  217. function sendInvitationEmail(_id) {
  218. const icode = InvitationCodes.findOne(_id);
  219. const author = Users.findOne(Meteor.userId());
  220. try {
  221. const fullName = Users.findOne(icode.authorId)
  222. && Users.findOne(icode.authorId).profile
  223. && Users.findOne(icode.authorId).profile !== undefined ? Users.findOne(icode.authorId).profile.fullname : "";
  224. const params = {
  225. email: icode.email,
  226. inviter: fullName != "" ? fullName + " (" + Users.findOne(icode.authorId).username + " )" : Users.findOne(icode.authorId).username,
  227. user: icode.email.split('@')[0],
  228. icode: icode.code,
  229. url: FlowRouter.url('sign-up'),
  230. };
  231. const lang = author.getLanguage();
  232. /*
  233. if (process.env.MAIL_SERVICE !== '') {
  234. let transporter = nodemailer.createTransport({
  235. service: process.env.MAIL_SERVICE,
  236. auth: {
  237. user: process.env.MAIL_SERVICE_USER,
  238. pass: process.env.MAIL_SERVICE_PASSWORD
  239. },
  240. })
  241. let info = transporter.sendMail({
  242. to: icode.email,
  243. from: Accounts.emailTemplates.from,
  244. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  245. text: TAPi18n.__('email-invite-register-text', params, lang),
  246. })
  247. } else {
  248. Email.send({
  249. to: icode.email,
  250. from: Accounts.emailTemplates.from,
  251. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  252. text: TAPi18n.__('email-invite-register-text', params, lang),
  253. });
  254. }
  255. */
  256. Email.send({
  257. to: icode.email,
  258. from: Accounts.emailTemplates.from,
  259. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  260. text: TAPi18n.__('email-invite-register-text', params, lang),
  261. });
  262. } catch (e) {
  263. InvitationCodes.remove(_id);
  264. throw new Meteor.Error('email-fail', e.message);
  265. }
  266. }
  267. function isLdapEnabled() {
  268. return (
  269. process.env.LDAP_ENABLE === 'true' || process.env.LDAP_ENABLE === true
  270. );
  271. }
  272. function isOauth2Enabled() {
  273. return (
  274. process.env.OAUTH2_ENABLED === 'true' ||
  275. process.env.OAUTH2_ENABLED === true
  276. );
  277. }
  278. function isCasEnabled() {
  279. return (
  280. process.env.CAS_ENABLED === 'true' || process.env.CAS_ENABLED === true
  281. );
  282. }
  283. function isApiEnabled() {
  284. return process.env.WITH_API === 'true' || process.env.WITH_API === true;
  285. }
  286. Meteor.methods({
  287. sendInvitation(emails, boards) {
  288. let rc = 0;
  289. check(emails, [String]);
  290. check(boards, [String]);
  291. const user = Users.findOne(Meteor.userId());
  292. if (!user.isAdmin) {
  293. rc = -1;
  294. throw new Meteor.Error('not-allowed');
  295. }
  296. emails.forEach(email => {
  297. if (email && SimpleSchema.RegEx.Email.test(email)) {
  298. // Checks if the email is already link to an account.
  299. const userExist = Users.findOne({ email });
  300. if (userExist) {
  301. rc = -1;
  302. throw new Meteor.Error(
  303. 'user-exist',
  304. `The user with the email ${email} has already an account.`,
  305. );
  306. }
  307. // Checks if the email is already link to an invitation.
  308. const invitation = InvitationCodes.findOne({ email });
  309. if (invitation) {
  310. InvitationCodes.update(invitation, {
  311. $set: { boardsToBeInvited: boards },
  312. });
  313. sendInvitationEmail(invitation._id);
  314. } else {
  315. const code = getRandomNum(100000, 999999);
  316. InvitationCodes.insert(
  317. {
  318. code,
  319. email,
  320. boardsToBeInvited: boards,
  321. createdAt: new Date(),
  322. authorId: Meteor.userId(),
  323. },
  324. function(err, _id) {
  325. if (!err && _id) {
  326. sendInvitationEmail(_id);
  327. } else {
  328. rc = -1;
  329. throw new Meteor.Error(
  330. 'invitation-generated-fail',
  331. err.message,
  332. );
  333. }
  334. },
  335. );
  336. }
  337. }
  338. });
  339. return rc;
  340. },
  341. sendSMTPTestEmail() {
  342. if (!Meteor.userId()) {
  343. throw new Meteor.Error('invalid-user');
  344. }
  345. const user = Meteor.user();
  346. if (!user.emails || !user.emails[0] || !user.emails[0].address) {
  347. throw new Meteor.Error('email-invalid');
  348. }
  349. this.unblock();
  350. const lang = user.getLanguage();
  351. try {
  352. /*
  353. if (process.env.MAIL_SERVICE !== '') {
  354. let transporter = nodemailer.createTransport({
  355. service: process.env.MAIL_SERVICE,
  356. auth: {
  357. user: process.env.MAIL_SERVICE_USER,
  358. pass: process.env.MAIL_SERVICE_PASSWORD
  359. },
  360. })
  361. let info = transporter.sendMail({
  362. to: user.emails[0].address,
  363. from: Accounts.emailTemplates.from,
  364. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  365. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  366. })
  367. } else {
  368. Email.send({
  369. to: user.emails[0].address,
  370. from: Accounts.emailTemplates.from,
  371. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  372. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  373. });
  374. }
  375. */
  376. Email.send({
  377. to: user.emails[0].address,
  378. from: Accounts.emailTemplates.from,
  379. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  380. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  381. });
  382. } catch ({ message }) {
  383. throw new Meteor.Error(
  384. 'email-fail',
  385. `${TAPi18n.__('email-fail-text', { lng: lang })}: ${message}`,
  386. message,
  387. );
  388. }
  389. return {
  390. message: 'email-sent',
  391. email: user.emails[0].address,
  392. };
  393. },
  394. getCustomUI() {
  395. const setting = Settings.findOne({});
  396. if (!setting.productName) {
  397. return {
  398. productName: '',
  399. };
  400. } else {
  401. return {
  402. productName: `${setting.productName}`,
  403. };
  404. }
  405. },
  406. getMatomoConf() {
  407. return {
  408. address: getEnvVar('MATOMO_ADDRESS'),
  409. siteId: getEnvVar('MATOMO_SITE_ID'),
  410. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  411. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  412. };
  413. },
  414. _isLdapEnabled() {
  415. return isLdapEnabled();
  416. },
  417. _isOauth2Enabled() {
  418. return isOauth2Enabled();
  419. },
  420. _isCasEnabled() {
  421. return isCasEnabled();
  422. },
  423. _isApiEnabled() {
  424. return isApiEnabled();
  425. },
  426. // Gets all connection methods to use it in the Template
  427. getAuthenticationsEnabled() {
  428. return {
  429. ldap: isLdapEnabled(),
  430. oauth2: isOauth2Enabled(),
  431. cas: isCasEnabled(),
  432. };
  433. },
  434. getDefaultAuthenticationMethod() {
  435. return process.env.DEFAULT_AUTHENTICATION_METHOD;
  436. },
  437. isPasswordLoginDisabled() {
  438. return process.env.PASSWORD_LOGIN_ENABLED === 'false';
  439. },
  440. });
  441. }
  442. export default Settings;