settings.js 15 KB

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