settings.js 15 KB

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