settings.js 13 KB

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