settings.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /*
  270. if (process.env.MAIL_SERVICE !== '') {
  271. let transporter = nodemailer.createTransport({
  272. service: process.env.MAIL_SERVICE,
  273. auth: {
  274. user: process.env.MAIL_SERVICE_USER,
  275. pass: process.env.MAIL_SERVICE_PASSWORD
  276. },
  277. })
  278. let info = transporter.sendMail({
  279. to: icode.email,
  280. from: Accounts.emailTemplates.from,
  281. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  282. text: TAPi18n.__('email-invite-register-text', params, lang),
  283. })
  284. } else {
  285. Email.send({
  286. to: icode.email,
  287. from: Accounts.emailTemplates.from,
  288. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  289. text: TAPi18n.__('email-invite-register-text', params, lang),
  290. });
  291. }
  292. */
  293. Email.send({
  294. to: icode.email,
  295. from: Accounts.emailTemplates.from,
  296. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  297. text: TAPi18n.__('email-invite-register-text', params, lang),
  298. });
  299. } catch (e) {
  300. InvitationCodes.remove(_id);
  301. throw new Meteor.Error('email-fail', e.message);
  302. }
  303. }
  304. function isNonAdminAllowedToSendMail(currentUser){
  305. const currSett = ReactiveCache.getCurrentSetting();
  306. let isAllowed = false;
  307. if(currSett && currSett != undefined && currSett.disableRegistration && currSett.mailDomainName !== undefined && currSett.mailDomainName != ""){
  308. for(let i = 0; i < currentUser.emails.length; i++) {
  309. if(currentUser.emails[i].address.endsWith(currSett.mailDomainName)){
  310. isAllowed = true;
  311. break;
  312. }
  313. }
  314. }
  315. return isAllowed;
  316. }
  317. function isLdapEnabled() {
  318. return (
  319. process.env.LDAP_ENABLE === 'true' || process.env.LDAP_ENABLE === true
  320. );
  321. }
  322. function isOauth2Enabled() {
  323. return (
  324. process.env.OAUTH2_ENABLED === 'true' ||
  325. process.env.OAUTH2_ENABLED === true
  326. );
  327. }
  328. function isCasEnabled() {
  329. return (
  330. process.env.CAS_ENABLED === 'true' || process.env.CAS_ENABLED === true
  331. );
  332. }
  333. function isApiEnabled() {
  334. return process.env.WITH_API === 'true' || process.env.WITH_API === true;
  335. }
  336. Meteor.methods({
  337. sendInvitation(emails, boards) {
  338. let rc = 0;
  339. check(emails, [String]);
  340. check(boards, [String]);
  341. const user = ReactiveCache.getCurrentUser();
  342. if (!user.isAdmin && !isNonAdminAllowedToSendMail(user)) {
  343. rc = -1;
  344. throw new Meteor.Error('not-allowed');
  345. }
  346. emails.forEach(email => {
  347. if (email && SimpleSchema.RegEx.Email.test(email)) {
  348. // Checks if the email is already link to an account.
  349. const userExist = ReactiveCache.getUser({ email });
  350. if (userExist) {
  351. rc = -1;
  352. throw new Meteor.Error(
  353. 'user-exist',
  354. `The user with the email ${email} has already an account.`,
  355. );
  356. }
  357. // Checks if the email is already link to an invitation.
  358. const invitation = ReactiveCache.getInvitationCode({ email });
  359. if (invitation) {
  360. InvitationCodes.update(invitation, {
  361. $set: { boardsToBeInvited: boards },
  362. });
  363. sendInvitationEmail(invitation._id);
  364. } else {
  365. const code = getRandomNum(100000, 999999);
  366. InvitationCodes.insert(
  367. {
  368. code,
  369. email,
  370. boardsToBeInvited: boards,
  371. createdAt: new Date(),
  372. authorId: Meteor.userId(),
  373. },
  374. function(err, _id) {
  375. if (!err && _id) {
  376. sendInvitationEmail(_id);
  377. } else {
  378. rc = -1;
  379. throw new Meteor.Error(
  380. 'invitation-generated-fail',
  381. err.message,
  382. );
  383. }
  384. },
  385. );
  386. }
  387. }
  388. });
  389. return rc;
  390. },
  391. sendSMTPTestEmail() {
  392. if (!Meteor.userId()) {
  393. throw new Meteor.Error('invalid-user');
  394. }
  395. const user = ReactiveCache.getCurrentUser();
  396. if (!user.emails || !user.emails[0] || !user.emails[0].address) {
  397. throw new Meteor.Error('email-invalid');
  398. }
  399. this.unblock();
  400. const lang = user.getLanguage();
  401. try {
  402. /*
  403. if (process.env.MAIL_SERVICE !== '') {
  404. let transporter = nodemailer.createTransport({
  405. service: process.env.MAIL_SERVICE,
  406. auth: {
  407. user: process.env.MAIL_SERVICE_USER,
  408. pass: process.env.MAIL_SERVICE_PASSWORD
  409. },
  410. })
  411. let info = transporter.sendMail({
  412. to: user.emails[0].address,
  413. from: Accounts.emailTemplates.from,
  414. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  415. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  416. })
  417. } else {
  418. Email.send({
  419. to: user.emails[0].address,
  420. from: Accounts.emailTemplates.from,
  421. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  422. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  423. });
  424. }
  425. */
  426. Email.send({
  427. to: user.emails[0].address,
  428. from: Accounts.emailTemplates.from,
  429. subject: TAPi18n.__('email-smtp-test-subject', { lng: lang }),
  430. text: TAPi18n.__('email-smtp-test-text', { lng: lang }),
  431. });
  432. } catch ({ message }) {
  433. throw new Meteor.Error(
  434. 'email-fail',
  435. `${TAPi18n.__('email-fail-text', { lng: lang })}: ${message}`,
  436. message,
  437. );
  438. }
  439. return {
  440. message: 'email-sent',
  441. email: user.emails[0].address,
  442. };
  443. },
  444. getCustomUI() {
  445. const setting = ReactiveCache.getCurrentSetting();
  446. if (!setting.productName) {
  447. return {
  448. productName: '',
  449. };
  450. } else {
  451. return {
  452. productName: `${setting.productName}`,
  453. };
  454. }
  455. },
  456. isDisableRegistration() {
  457. const setting = ReactiveCache.getCurrentSetting();
  458. if (setting.disableRegistration === true) {
  459. return true;
  460. } else {
  461. return false;
  462. }
  463. },
  464. isDisableForgotPassword() {
  465. const setting = ReactiveCache.getCurrentSetting();
  466. if (setting.disableForgotPassword === true) {
  467. return true;
  468. } else {
  469. return false;
  470. }
  471. },
  472. getMatomoConf() {
  473. return {
  474. address: getEnvVar('MATOMO_ADDRESS'),
  475. siteId: getEnvVar('MATOMO_SITE_ID'),
  476. doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
  477. withUserName: process.env.MATOMO_WITH_USERNAME || false,
  478. };
  479. },
  480. _isLdapEnabled() {
  481. return isLdapEnabled();
  482. },
  483. _isOauth2Enabled() {
  484. return isOauth2Enabled();
  485. },
  486. _isCasEnabled() {
  487. return isCasEnabled();
  488. },
  489. _isApiEnabled() {
  490. return isApiEnabled();
  491. },
  492. // Gets all connection methods to use it in the Template
  493. getAuthenticationsEnabled() {
  494. return {
  495. ldap: isLdapEnabled(),
  496. oauth2: isOauth2Enabled(),
  497. cas: isCasEnabled(),
  498. };
  499. },
  500. getOauthServerUrl(){
  501. return process.env.OAUTH2_SERVER_URL;
  502. },
  503. getOauthDashboardUrl(){
  504. return process.env.DASHBOARD_URL;
  505. },
  506. getDefaultAuthenticationMethod() {
  507. return process.env.DEFAULT_AUTHENTICATION_METHOD;
  508. },
  509. isPasswordLoginEnabled() {
  510. return !(process.env.PASSWORD_LOGIN_ENABLED === 'false');
  511. },
  512. isOidcRedirectionEnabled(){
  513. return process.env.OIDC_REDIRECTION_ENABLED === 'true' && Object.keys(loadOidcConfig("oidc")).length > 0;
  514. },
  515. getServiceConfiguration(service){
  516. return loadOidcConfig(service);
  517. }
  518. });
  519. }
  520. export default Settings;