settings.js 15 KB

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