settings.js 10 KB

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