2
0

settings.js 10 KB

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