settings.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 fullName = Users.findOne(icode.authorId)
  214. && Users.findOne(icode.authorId).profile
  215. && Users.findOne(icode.authorId).profile !== undefined ? Users.findOne(icode.authorId).profile.fullname : "";
  216. const params = {
  217. email: icode.email,
  218. inviter: fullName != "" ? fullName + " (" + Users.findOne(icode.authorId).username + " )" : Users.findOne(icode.authorId).username,
  219. user: icode.email.split('@')[0],
  220. icode: icode.code,
  221. url: FlowRouter.url('sign-up'),
  222. };
  223. const lang = author.getLanguage();
  224. /*
  225. if (process.env.MAIL_SERVICE !== '') {
  226. let transporter = nodemailer.createTransport({
  227. service: process.env.MAIL_SERVICE,
  228. auth: {
  229. user: process.env.MAIL_SERVICE_USER,
  230. pass: process.env.MAIL_SERVICE_PASSWORD
  231. },
  232. })
  233. let info = transporter.sendMail({
  234. to: icode.email,
  235. from: Accounts.emailTemplates.from,
  236. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  237. text: TAPi18n.__('email-invite-register-text', params, lang),
  238. })
  239. } else {
  240. Email.send({
  241. to: icode.email,
  242. from: Accounts.emailTemplates.from,
  243. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  244. text: TAPi18n.__('email-invite-register-text', params, lang),
  245. });
  246. }
  247. */
  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. } catch (e) {
  255. InvitationCodes.remove(_id);
  256. throw new Meteor.Error('email-fail', e.message);
  257. }
  258. }
  259. function isLdapEnabled() {
  260. return (
  261. process.env.LDAP_ENABLE === 'true' || process.env.LDAP_ENABLE === true
  262. );
  263. }
  264. function isOauth2Enabled() {
  265. return (
  266. process.env.OAUTH2_ENABLED === 'true' ||
  267. process.env.OAUTH2_ENABLED === true
  268. );
  269. }
  270. function isCasEnabled() {
  271. return (
  272. process.env.CAS_ENABLED === 'true' || process.env.CAS_ENABLED === true
  273. );
  274. }
  275. function isApiEnabled() {
  276. return process.env.WITH_API === 'true' || process.env.WITH_API === true;
  277. }
  278. Meteor.methods({
  279. sendInvitation(emails, boards) {
  280. check(emails, [String]);
  281. check(boards, [String]);
  282. const user = Users.findOne(Meteor.userId());
  283. if (!user.isAdmin) {
  284. throw new Meteor.Error('not-allowed');
  285. }
  286. emails.forEach(email => {
  287. if (email && SimpleSchema.RegEx.Email.test(email)) {
  288. // Checks if the email is already link to an account.
  289. const userExist = Users.findOne({ email });
  290. if (userExist) {
  291. throw new Meteor.Error(
  292. 'user-exist',
  293. `The user with the email ${email} has already an account.`,
  294. );
  295. }
  296. // Checks if the email is already link to an invitation.
  297. const invitation = InvitationCodes.findOne({ email });
  298. if (invitation) {
  299. InvitationCodes.update(invitation, {
  300. $set: { boardsToBeInvited: boards },
  301. });
  302. sendInvitationEmail(invitation._id);
  303. } else {
  304. const code = getRandomNum(100000, 999999);
  305. InvitationCodes.insert(
  306. {
  307. code,
  308. email,
  309. boardsToBeInvited: boards,
  310. createdAt: new Date(),
  311. authorId: Meteor.userId(),
  312. },
  313. function(err, _id) {
  314. if (!err && _id) {
  315. sendInvitationEmail(_id);
  316. } else {
  317. throw new Meteor.Error(
  318. 'invitation-generated-fail',
  319. err.message,
  320. );
  321. }
  322. },
  323. );
  324. }
  325. }
  326. });
  327. },
  328. sendSMTPTestEmail() {
  329. if (!Meteor.userId()) {
  330. throw new Meteor.Error('invalid-user');
  331. }
  332. const user = Meteor.user();
  333. if (!user.emails || !user.emails[0] || !user.emails[0].address) {
  334. throw new Meteor.Error('email-invalid');
  335. }
  336. this.unblock();
  337. const lang = user.getLanguage();
  338. try {
  339. /*
  340. if (process.env.MAIL_SERVICE !== '') {
  341. let transporter = nodemailer.createTransport({
  342. service: process.env.MAIL_SERVICE,
  343. auth: {
  344. user: process.env.MAIL_SERVICE_USER,
  345. pass: process.env.MAIL_SERVICE_PASSWORD
  346. },
  347. })
  348. let info = transporter.sendMail({
  349. to: user.emails[0].address,
  350. from: Accounts.emailTemplates.from,
  351. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  352. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  353. })
  354. } else {
  355. Email.send({
  356. to: user.emails[0].address,
  357. from: Accounts.emailTemplates.from,
  358. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  359. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  360. });
  361. }
  362. */
  363. Email.send({
  364. to: user.emails[0].address,
  365. from: Accounts.emailTemplates.from,
  366. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  367. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  368. });
  369. } catch ({ message }) {
  370. throw new Meteor.Error(
  371. 'email-fail',
  372. `${TAPi18n.__('email-fail-text', { lng: lang })}: ${message}`,
  373. message,
  374. );
  375. }
  376. return {
  377. message: 'email-sent',
  378. email: user.emails[0].address,
  379. };
  380. },
  381. getCustomUI() {
  382. const setting = Settings.findOne({});
  383. if (!setting.productName) {
  384. return {
  385. productName: '',
  386. };
  387. } else {
  388. return {
  389. productName: `${setting.productName}`,
  390. };
  391. }
  392. },
  393. getMatomoConf() {
  394. return {
  395. address: getEnvVar('MATOMO_ADDRESS'),
  396. siteId: getEnvVar('MATOMO_SITE_ID'),
  397. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  398. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  399. };
  400. },
  401. _isLdapEnabled() {
  402. return isLdapEnabled();
  403. },
  404. _isOauth2Enabled() {
  405. return isOauth2Enabled();
  406. },
  407. _isCasEnabled() {
  408. return isCasEnabled();
  409. },
  410. _isApiEnabled() {
  411. return isApiEnabled();
  412. },
  413. // Gets all connection methods to use it in the Template
  414. getAuthenticationsEnabled() {
  415. return {
  416. ldap: isLdapEnabled(),
  417. oauth2: isOauth2Enabled(),
  418. cas: isCasEnabled(),
  419. };
  420. },
  421. getDefaultAuthenticationMethod() {
  422. return process.env.DEFAULT_AUTHENTICATION_METHOD;
  423. },
  424. isPasswordLoginDisabled() {
  425. return process.env.PASSWORD_LOGIN_ENABLED === 'false';
  426. },
  427. });
  428. }
  429. export default Settings;