settings.js 14 KB

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