settings.js 7.8 KB

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