settings.js 14 KB

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