settings.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. import { TAPi18n } from '/imports/i18n';
  2. //var nodemailer = require('nodemailer');
  3. // Sandstorm context is detected using the METEOR_SETTINGS environment variable
  4. // in the package definition.
  5. const isSandstorm =
  6. Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm;
  7. Settings = new Mongo.Collection('settings');
  8. Settings.attachSchema(
  9. new SimpleSchema({
  10. disableRegistration: {
  11. type: Boolean,
  12. optional: true,
  13. defaultValue: false,
  14. },
  15. disableForgotPassword: {
  16. type: Boolean,
  17. optional: true,
  18. defaultValue: false,
  19. },
  20. 'mailServer.username': {
  21. type: String,
  22. optional: true,
  23. },
  24. 'mailServer.password': {
  25. type: String,
  26. optional: true,
  27. },
  28. 'mailServer.host': {
  29. type: String,
  30. optional: true,
  31. },
  32. 'mailServer.port': {
  33. type: String,
  34. optional: true,
  35. },
  36. 'mailServer.enableTLS': {
  37. type: Boolean,
  38. optional: true,
  39. },
  40. 'mailServer.from': {
  41. type: String,
  42. optional: true,
  43. },
  44. productName: {
  45. type: String,
  46. optional: true,
  47. },
  48. displayAuthenticationMethod: {
  49. type: Boolean,
  50. optional: true,
  51. },
  52. defaultAuthenticationMethod: {
  53. type: String,
  54. optional: false,
  55. },
  56. spinnerName: {
  57. type: String,
  58. optional: true,
  59. },
  60. hideLogo: {
  61. type: Boolean,
  62. optional: true,
  63. },
  64. hideCardCounterList: {
  65. type: Boolean,
  66. optional: true,
  67. },
  68. hideBoardMemberList: {
  69. type: Boolean,
  70. optional: true,
  71. },
  72. customLoginLogoImageUrl: {
  73. type: String,
  74. optional: true,
  75. },
  76. customLoginLogoLinkUrl: {
  77. type: String,
  78. optional: true,
  79. },
  80. customHelpLinkUrl: {
  81. type: String,
  82. optional: true,
  83. },
  84. textBelowCustomLoginLogo: {
  85. type: String,
  86. optional: true,
  87. },
  88. automaticLinkedUrlSchemes: {
  89. type: String,
  90. optional: true,
  91. },
  92. customTopLeftCornerLogoImageUrl: {
  93. type: String,
  94. optional: true,
  95. },
  96. customTopLeftCornerLogoLinkUrl: {
  97. type: String,
  98. optional: true,
  99. },
  100. customTopLeftCornerLogoHeight: {
  101. type: String,
  102. optional: true,
  103. },
  104. oidcBtnText: {
  105. type: String,
  106. optional: true,
  107. },
  108. mailDomainName: {
  109. type: String,
  110. optional: true,
  111. },
  112. legalNotice: {
  113. type: String,
  114. optional: true,
  115. },
  116. createdAt: {
  117. type: Date,
  118. denyUpdate: true,
  119. // eslint-disable-next-line consistent-return
  120. autoValue() {
  121. if (this.isInsert) {
  122. return new Date();
  123. } else if (this.isUpsert) {
  124. return { $setOnInsert: new Date() };
  125. } else {
  126. this.unset();
  127. }
  128. },
  129. },
  130. modifiedAt: {
  131. type: Date,
  132. // eslint-disable-next-line consistent-return
  133. autoValue() {
  134. if (this.isInsert || this.isUpsert || this.isUpdate) {
  135. return new Date();
  136. } else {
  137. this.unset();
  138. }
  139. },
  140. },
  141. }),
  142. );
  143. Settings.helpers({
  144. mailUrl() {
  145. if (!this.mailServer.host) {
  146. return null;
  147. }
  148. const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  149. if (!this.mailServer.username && !this.mailServer.password) {
  150. return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
  151. }
  152. return `${protocol}${this.mailServer.username}:${encodeURIComponent(
  153. this.mailServer.password,
  154. )}@${this.mailServer.host}:${this.mailServer.port}/`;
  155. },
  156. });
  157. Settings.allow({
  158. update(userId) {
  159. const user = Users.findOne(userId);
  160. return user && user.isAdmin;
  161. },
  162. });
  163. if (Meteor.isServer) {
  164. Meteor.startup(() => {
  165. Settings._collection.createIndex({ modifiedAt: -1 });
  166. const setting = Settings.findOne({});
  167. if (!setting) {
  168. const now = new Date();
  169. const domain = process.env.ROOT_URL.match(
  170. /\/\/(?:www\.)?(.*)?(?:\/)?/,
  171. )[1];
  172. const from = `Boards Support <support@${domain}>`;
  173. const defaultSetting = {
  174. disableRegistration: false,
  175. mailServer: {
  176. username: '',
  177. password: '',
  178. host: '',
  179. port: '',
  180. enableTLS: false,
  181. from,
  182. },
  183. createdAt: now,
  184. modifiedAt: now,
  185. displayAuthenticationMethod: true,
  186. defaultAuthenticationMethod: 'password',
  187. };
  188. Settings.insert(defaultSetting);
  189. }
  190. if (isSandstorm) {
  191. // At Sandstorm, Admin Panel has SMTP settings
  192. const newSetting = Settings.findOne();
  193. if (!process.env.MAIL_URL && newSetting.mailUrl())
  194. process.env.MAIL_URL = newSetting.mailUrl();
  195. Accounts.emailTemplates.from = process.env.MAIL_FROM
  196. ? process.env.MAIL_FROM
  197. : newSetting.mailServer.from;
  198. } else {
  199. // Not running on Sandstorm, so using environment variables
  200. Accounts.emailTemplates.from = process.env.MAIL_FROM;
  201. }
  202. });
  203. if (isSandstorm) {
  204. // At Sandstorm Wekan Admin Panel, save SMTP settings.
  205. Settings.after.update((userId, doc, fieldNames) => {
  206. // assign new values to mail-from & MAIL_URL in environment
  207. if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
  208. const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
  209. if (!doc.mailServer.username && !doc.mailServer.password) {
  210. process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
  211. } else {
  212. process.env.MAIL_URL = `${protocol}${
  213. doc.mailServer.username
  214. }:${encodeURIComponent(doc.mailServer.password)}@${
  215. doc.mailServer.host
  216. }:${doc.mailServer.port}/`;
  217. }
  218. Accounts.emailTemplates.from = doc.mailServer.from;
  219. }
  220. });
  221. }
  222. function getRandomNum(min, max) {
  223. const range = max - min;
  224. const rand = Math.random();
  225. return min + Math.round(rand * range);
  226. }
  227. function getEnvVar(name) {
  228. const value = process.env[name];
  229. if (value) {
  230. return value;
  231. }
  232. throw new Meteor.Error([
  233. 'var-not-exist',
  234. `The environment variable ${name} does not exist`,
  235. ]);
  236. }
  237. function loadOidcConfig(service){
  238. check(service, String);
  239. var config = ServiceConfiguration.configurations.findOne({service: service});
  240. return config;
  241. }
  242. function sendInvitationEmail(_id) {
  243. const icode = InvitationCodes.findOne(_id);
  244. const author = Users.findOne(Meteor.userId());
  245. try {
  246. const fullName = Users.findOne(icode.authorId)
  247. && Users.findOne(icode.authorId).profile
  248. && Users.findOne(icode.authorId).profile !== undefined
  249. && Users.findOne(icode.authorId).profile.fullname ? Users.findOne(icode.authorId).profile.fullname : "";
  250. const params = {
  251. email: icode.email,
  252. inviter: fullName != "" ? fullName + " (" + Users.findOne(icode.authorId).username + " )" : Users.findOne(icode.authorId).username,
  253. user: icode.email.split('@')[0],
  254. icode: icode.code,
  255. url: FlowRouter.url('sign-up'),
  256. };
  257. const lang = author.getLanguage();
  258. /*
  259. if (process.env.MAIL_SERVICE !== '') {
  260. let transporter = nodemailer.createTransport({
  261. service: process.env.MAIL_SERVICE,
  262. auth: {
  263. user: process.env.MAIL_SERVICE_USER,
  264. pass: process.env.MAIL_SERVICE_PASSWORD
  265. },
  266. })
  267. let info = transporter.sendMail({
  268. to: icode.email,
  269. from: Accounts.emailTemplates.from,
  270. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  271. text: TAPi18n.__('email-invite-register-text', params, lang),
  272. })
  273. } else {
  274. Email.send({
  275. to: icode.email,
  276. from: Accounts.emailTemplates.from,
  277. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  278. text: TAPi18n.__('email-invite-register-text', params, lang),
  279. });
  280. }
  281. */
  282. Email.send({
  283. to: icode.email,
  284. from: Accounts.emailTemplates.from,
  285. subject: TAPi18n.__('email-invite-register-subject', params, lang),
  286. text: TAPi18n.__('email-invite-register-text', params, lang),
  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 = Settings.findOne({});
  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 = Users.findOne(Meteor.userId());
  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 = Users.findOne({ 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 = InvitationCodes.findOne({ 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 = Meteor.user();
  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 = Settings.findOne({});
  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 = Settings.findOne({});
  447. if (setting.disableRegistration === true) {
  448. return true;
  449. } else {
  450. return false;
  451. }
  452. },
  453. isDisableForgotPassword() {
  454. const setting = Settings.findOne({});
  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. isPasswordLoginDisabled() {
  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;