settings.js 14 KB

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