settings.js 14 KB

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