2
0

settings.js 13 KB

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