2
0

settings.js 13 KB

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