settings.js 10 KB

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