2
0

settings.js 9.0 KB

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