settings.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. createdAt: {
  131. type: Date,
  132. denyUpdate: true,
  133. // eslint-disable-next-line consistent-return
  134. autoValue() {
  135. if (this.isInsert) {
  136. return new Date();
  137. } else if (this.isUpsert) {
  138. return { $setOnInsert: new Date() };
  139. } else {
  140. this.unset();
  141. }
  142. },
  143. },
  144. modifiedAt: {
  145. type: Date,
  146. // eslint-disable-next-line consistent-return
  147. autoValue() {
  148. if (this.isInsert || this.isUpsert || this.isUpdate) {
  149. return new Date();
  150. } else {
  151. this.unset();
  152. }
  153. },
  154. },
  155. }),
  156. );
  157. Settings.helpers({
  158. mailUrl() {
  159. if (!this.mailServer.host) {
  160. return null;
  161. }
  162. const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  163. if (!this.mailServer.username && !this.mailServer.password) {
  164. return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
  165. }
  166. return `${protocol}${this.mailServer.username}:${encodeURIComponent(
  167. this.mailServer.password,
  168. )}@${this.mailServer.host}:${this.mailServer.port}/`;
  169. },
  170. });
  171. Settings.allow({
  172. update(userId) {
  173. const user = ReactiveCache.getUser(userId);
  174. return user && user.isAdmin;
  175. },
  176. });
  177. if (Meteor.isServer) {
  178. Meteor.startup(() => {
  179. Settings._collection.createIndex({ modifiedAt: -1 });
  180. const setting = ReactiveCache.getCurrentSetting();
  181. if (!setting) {
  182. const now = new Date();
  183. const domain = process.env.ROOT_URL.match(
  184. /\/\/(?:www\.)?(.*)?(?:\/)?/,
  185. )[1];
  186. const from = `Boards Support <support@${domain}>`;
  187. const defaultSetting = {
  188. disableRegistration: false,
  189. mailServer: {
  190. username: '',
  191. password: '',
  192. host: '',
  193. port: '',
  194. enableTLS: false,
  195. from,
  196. },
  197. createdAt: now,
  198. modifiedAt: now,
  199. displayAuthenticationMethod: true,
  200. defaultAuthenticationMethod: 'password',
  201. };
  202. Settings.insert(defaultSetting);
  203. }
  204. if (isSandstorm) {
  205. // At Sandstorm, Admin Panel has SMTP settings
  206. const newSetting = ReactiveCache.getCurrentSetting();
  207. if (!process.env.MAIL_URL && newSetting.mailUrl())
  208. process.env.MAIL_URL = newSetting.mailUrl();
  209. Accounts.emailTemplates.from = process.env.MAIL_FROM
  210. ? process.env.MAIL_FROM
  211. : newSetting.mailServer.from;
  212. } else {
  213. // Not running on Sandstorm, so using environment variables
  214. Accounts.emailTemplates.from = process.env.MAIL_FROM;
  215. }
  216. });
  217. if (isSandstorm) {
  218. // At Sandstorm Wekan Admin Panel, save SMTP settings.
  219. Settings.after.update((userId, doc, fieldNames) => {
  220. // assign new values to mail-from & MAIL_URL in environment
  221. if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
  222. const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  223. if (!doc.mailServer.username && !doc.mailServer.password) {
  224. process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
  225. } else {
  226. process.env.MAIL_URL = `${protocol}${
  227. doc.mailServer.username
  228. }:${encodeURIComponent(doc.mailServer.password)}@${
  229. doc.mailServer.host
  230. }:${doc.mailServer.port}/`;
  231. }
  232. Accounts.emailTemplates.from = doc.mailServer.from;
  233. }
  234. });
  235. }
  236. function getRandomNum(min, max) {
  237. const range = max - min;
  238. const rand = Math.random();
  239. return min + Math.round(rand * range);
  240. }
  241. function getEnvVar(name) {
  242. const value = process.env[name];
  243. if (value) {
  244. return value;
  245. }
  246. throw new Meteor.Error([
  247. 'var-not-exist',
  248. `The environment variable ${name} does not exist`,
  249. ]);
  250. }
  251. function loadOidcConfig(service){
  252. check(service, String);
  253. var config = ServiceConfiguration.configurations.findOne({service: service});
  254. return config;
  255. }
  256. function sendInvitationEmail(_id) {
  257. const icode = ReactiveCache.getInvitationCode(_id);
  258. const author = ReactiveCache.getCurrentUser();
  259. try {
  260. const fullName = ReactiveCache.getUser(icode.authorId)?.profile?.fullname || "";
  261. const params = {
  262. email: icode.email,
  263. inviter: fullName != "" ? fullName + " (" + ReactiveCache.getUser(icode.authorId).username + " )" : ReactiveCache.getUser(icode.authorId).username,
  264. user: icode.email.split('@')[0],
  265. icode: icode.code,
  266. url: FlowRouter.url('sign-up'),
  267. };
  268. const lang = author.getLanguage();
  269. // Use EmailLocalization utility to handle email in the proper language
  270. if (typeof EmailLocalization !== 'undefined') {
  271. EmailLocalization.sendEmail({
  272. to: icode.email,
  273. from: Accounts.emailTemplates.from,
  274. subject: 'email-invite-register-subject',
  275. text: 'email-invite-register-text',
  276. params: params,
  277. language: lang
  278. });
  279. } else {
  280. // Fallback if EmailLocalization is not available
  281. Email.send({
  282. to: icode.email,
  283. from: Accounts.emailTemplates.from,
  284. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  285. text: TAPi18n.__('email-invite-register-text', params, lang),
  286. });
  287. }
  288. } catch (e) {
  289. InvitationCodes.remove(_id);
  290. throw new Meteor.Error('email-fail', e.message);
  291. }
  292. }
  293. function isNonAdminAllowedToSendMail(currentUser){
  294. const currSett = ReactiveCache.getCurrentSetting();
  295. let isAllowed = false;
  296. if(currSett && currSett != undefined && currSett.disableRegistration && currSett.mailDomainName !== undefined && currSett.mailDomainName != ""){
  297. for(let i = 0; i < currentUser.emails.length; i++) {
  298. if(currentUser.emails[i].address.endsWith(currSett.mailDomainName)){
  299. isAllowed = true;
  300. break;
  301. }
  302. }
  303. }
  304. return isAllowed;
  305. }
  306. function isLdapEnabled() {
  307. return (
  308. process.env.LDAP_ENABLE === 'true' || process.env.LDAP_ENABLE === true
  309. );
  310. }
  311. function isOauth2Enabled() {
  312. return (
  313. process.env.OAUTH2_ENABLED === 'true' ||
  314. process.env.OAUTH2_ENABLED === true
  315. );
  316. }
  317. function isCasEnabled() {
  318. return (
  319. process.env.CAS_ENABLED === 'true' || process.env.CAS_ENABLED === true
  320. );
  321. }
  322. function isApiEnabled() {
  323. return process.env.WITH_API === 'true' || process.env.WITH_API === true;
  324. }
  325. Meteor.methods({
  326. sendInvitation(emails, boards) {
  327. let rc = 0;
  328. check(emails, [String]);
  329. check(boards, [String]);
  330. const user = ReactiveCache.getCurrentUser();
  331. if (!user.isAdmin && !isNonAdminAllowedToSendMail(user)) {
  332. rc = -1;
  333. throw new Meteor.Error('not-allowed');
  334. }
  335. emails.forEach(email => {
  336. if (email && SimpleSchema.RegEx.Email.test(email)) {
  337. // Checks if the email is already link to an account.
  338. const userExist = ReactiveCache.getUser({ email });
  339. if (userExist) {
  340. rc = -1;
  341. throw new Meteor.Error(
  342. 'user-exist',
  343. `The user with the email ${email} has already an account.`,
  344. );
  345. }
  346. // Checks if the email is already link to an invitation.
  347. const invitation = ReactiveCache.getInvitationCode({ email });
  348. if (invitation) {
  349. InvitationCodes.update(invitation, {
  350. $set: { boardsToBeInvited: boards },
  351. });
  352. sendInvitationEmail(invitation._id);
  353. } else {
  354. const code = getRandomNum(100000, 999999);
  355. InvitationCodes.insert(
  356. {
  357. code,
  358. email,
  359. boardsToBeInvited: boards,
  360. createdAt: new Date(),
  361. authorId: Meteor.userId(),
  362. },
  363. function(err, _id) {
  364. if (!err && _id) {
  365. sendInvitationEmail(_id);
  366. } else {
  367. rc = -1;
  368. throw new Meteor.Error(
  369. 'invitation-generated-fail',
  370. err.message,
  371. );
  372. }
  373. },
  374. );
  375. }
  376. }
  377. });
  378. return rc;
  379. },
  380. sendSMTPTestEmail() {
  381. if (!Meteor.userId()) {
  382. throw new Meteor.Error('invalid-user');
  383. }
  384. const user = ReactiveCache.getCurrentUser();
  385. if (!user.emails || !user.emails[0] || !user.emails[0].address) {
  386. throw new Meteor.Error('email-invalid');
  387. }
  388. this.unblock();
  389. const lang = user.getLanguage();
  390. try {
  391. /*
  392. if (process.env.MAIL_SERVICE !== '') {
  393. let transporter = nodemailer.createTransport({
  394. service: process.env.MAIL_SERVICE,
  395. auth: {
  396. user: process.env.MAIL_SERVICE_USER,
  397. pass: process.env.MAIL_SERVICE_PASSWORD
  398. },
  399. })
  400. let info = transporter.sendMail({
  401. to: user.emails[0].address,
  402. from: Accounts.emailTemplates.from,
  403. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  404. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  405. })
  406. } else {
  407. Email.send({
  408. to: user.emails[0].address,
  409. from: Accounts.emailTemplates.from,
  410. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  411. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  412. });
  413. }
  414. */
  415. Email.send({
  416. to: user.emails[0].address,
  417. from: Accounts.emailTemplates.from,
  418. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  419. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  420. });
  421. } catch ({ message }) {
  422. throw new Meteor.Error(
  423. 'email-fail',
  424. `${TAPi18n.__('email-fail-text', { lng: lang })}: ${message}`,
  425. message,
  426. );
  427. }
  428. return {
  429. message: 'email-sent',
  430. email: user.emails[0].address,
  431. };
  432. },
  433. getCustomUI() {
  434. const setting = ReactiveCache.getCurrentSetting();
  435. if (!setting.productName) {
  436. return {
  437. productName: '',
  438. };
  439. } else {
  440. return {
  441. productName: `${setting.productName}`,
  442. };
  443. }
  444. },
  445. isDisableRegistration() {
  446. const setting = ReactiveCache.getCurrentSetting();
  447. if (setting.disableRegistration === true) {
  448. return true;
  449. } else {
  450. return false;
  451. }
  452. },
  453. isDisableForgotPassword() {
  454. const setting = ReactiveCache.getCurrentSetting();
  455. if (setting.disableForgotPassword === true) {
  456. return true;
  457. } else {
  458. return false;
  459. }
  460. },
  461. getMatomoConf() {
  462. return {
  463. address: getEnvVar('MATOMO_ADDRESS'),
  464. siteId: getEnvVar('MATOMO_SITE_ID'),
  465. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  466. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  467. };
  468. },
  469. _isLdapEnabled() {
  470. return isLdapEnabled();
  471. },
  472. _isOauth2Enabled() {
  473. return isOauth2Enabled();
  474. },
  475. _isCasEnabled() {
  476. return isCasEnabled();
  477. },
  478. _isApiEnabled() {
  479. return isApiEnabled();
  480. },
  481. // Gets all connection methods to use it in the Template
  482. getAuthenticationsEnabled() {
  483. return {
  484. ldap: isLdapEnabled(),
  485. oauth2: isOauth2Enabled(),
  486. cas: isCasEnabled(),
  487. };
  488. },
  489. getOauthServerUrl(){
  490. return process.env.OAUTH2_SERVER_URL;
  491. },
  492. getOauthDashboardUrl(){
  493. return process.env.DASHBOARD_URL;
  494. },
  495. getDefaultAuthenticationMethod() {
  496. return process.env.DEFAULT_AUTHENTICATION_METHOD;
  497. },
  498. isPasswordLoginEnabled() {
  499. return !(process.env.PASSWORD_LOGIN_ENABLED === 'false');
  500. },
  501. isOidcRedirectionEnabled(){
  502. return process.env.OIDC_REDIRECTION_ENABLED === 'true' && Object.keys(loadOidcConfig("oidc")).length > 0;
  503. },
  504. getServiceConfiguration(service){
  505. return loadOidcConfig(service);
  506. }
  507. });
  508. }
  509. export default Settings;