settingBody.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. BlazeComponent.extendComponent({
  2. onCreated() {
  3. this.error = new ReactiveVar('');
  4. this.loading = new ReactiveVar(false);
  5. this.generalSetting = new ReactiveVar(true);
  6. this.emailSetting = new ReactiveVar(false);
  7. this.accountSetting = new ReactiveVar(false);
  8. this.announcementSetting = new ReactiveVar(false);
  9. this.layoutSetting = new ReactiveVar(false);
  10. this.webhookSetting = new ReactiveVar(false);
  11. Meteor.subscribe('setting');
  12. Meteor.subscribe('mailServer');
  13. Meteor.subscribe('accountSettings');
  14. Meteor.subscribe('announcements');
  15. Meteor.subscribe('globalwebhooks');
  16. },
  17. setError(error) {
  18. this.error.set(error);
  19. },
  20. setLoading(w) {
  21. this.loading.set(w);
  22. },
  23. checkField(selector) {
  24. const value = $(selector).val();
  25. if (!value || value.trim() === '') {
  26. $(selector)
  27. .parents('li.smtp-form')
  28. .addClass('has-error');
  29. throw Error('blank field');
  30. } else {
  31. return value;
  32. }
  33. },
  34. currentSetting() {
  35. return Settings.findOne();
  36. },
  37. boards() {
  38. return Boards.find(
  39. {
  40. archived: false,
  41. 'members.userId': Meteor.userId(),
  42. 'members.isAdmin': true,
  43. },
  44. {
  45. sort: { sort: 1 /* boards default sorting */ },
  46. },
  47. );
  48. },
  49. toggleRegistration() {
  50. this.setLoading(true);
  51. const registrationClosed = this.currentSetting().disableRegistration;
  52. Settings.update(Settings.findOne()._id, {
  53. $set: { disableRegistration: !registrationClosed },
  54. });
  55. this.setLoading(false);
  56. if (registrationClosed) {
  57. $('.invite-people').slideUp();
  58. } else {
  59. $('.invite-people').slideDown();
  60. }
  61. },
  62. toggleTLS() {
  63. $('#mail-server-tls').toggleClass('is-checked');
  64. },
  65. toggleHideLogo() {
  66. $('#hide-logo').toggleClass('is-checked');
  67. },
  68. toggleDisplayAuthenticationMethod() {
  69. $('#display-authentication-method').toggleClass('is-checked');
  70. },
  71. switchMenu(event) {
  72. const target = $(event.target);
  73. if (!target.hasClass('active')) {
  74. $('.side-menu li.active').removeClass('active');
  75. target.parent().addClass('active');
  76. const targetID = target.data('id');
  77. this.generalSetting.set('registration-setting' === targetID);
  78. this.emailSetting.set('email-setting' === targetID);
  79. this.accountSetting.set('account-setting' === targetID);
  80. this.announcementSetting.set('announcement-setting' === targetID);
  81. this.layoutSetting.set('layout-setting' === targetID);
  82. this.webhookSetting.set('webhook-setting' === targetID);
  83. }
  84. },
  85. checkBoard(event) {
  86. let target = $(event.target);
  87. if (!target.hasClass('js-toggle-board-choose')) {
  88. target = target.parent();
  89. }
  90. const checkboxId = target.attr('id');
  91. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  92. $(`#${checkboxId}`).toggleClass('is-checked');
  93. },
  94. inviteThroughEmail() {
  95. const emails = $('#email-to-invite')
  96. .val()
  97. .toLowerCase()
  98. .trim()
  99. .split('\n')
  100. .join(',')
  101. .split(',');
  102. const boardsToInvite = [];
  103. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function() {
  104. boardsToInvite.push($(this).data('id'));
  105. });
  106. const validEmails = [];
  107. emails.forEach(email => {
  108. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  109. validEmails.push(email.trim());
  110. }
  111. });
  112. if (validEmails.length) {
  113. this.setLoading(true);
  114. Meteor.call('sendInvitation', validEmails, boardsToInvite, () => {
  115. // if (!err) {
  116. // TODO - show more info to user
  117. // }
  118. this.setLoading(false);
  119. });
  120. }
  121. },
  122. saveMailServerInfo() {
  123. this.setLoading(true);
  124. $('li').removeClass('has-error');
  125. try {
  126. const host = this.checkField('#mail-server-host');
  127. const port = this.checkField('#mail-server-port');
  128. const username = $('#mail-server-username')
  129. .val()
  130. .trim();
  131. const password = $('#mail-server-password')
  132. .val()
  133. .trim();
  134. const from = this.checkField('#mail-server-from');
  135. const tls = $('#mail-server-tls.is-checked').length > 0;
  136. Settings.update(Settings.findOne()._id, {
  137. $set: {
  138. 'mailServer.host': host,
  139. 'mailServer.port': port,
  140. 'mailServer.username': username,
  141. 'mailServer.password': password,
  142. 'mailServer.enableTLS': tls,
  143. 'mailServer.from': from,
  144. },
  145. });
  146. } catch (e) {
  147. return;
  148. } finally {
  149. this.setLoading(false);
  150. }
  151. },
  152. saveLayout() {
  153. this.setLoading(true);
  154. $('li').removeClass('has-error');
  155. const productName = $('#product-name')
  156. .val()
  157. .trim();
  158. const customLoginLogoImageUrl = $('#custom-login-logo-image-url')
  159. .val()
  160. .trim();
  161. const customLoginLogoLinkUrl = $('#custom-login-logo-link-url')
  162. .val()
  163. .trim();
  164. const textBelowCustomLoginLogo = $('#text-below-custom-login-logo')
  165. .val()
  166. .trim();
  167. const customTopLeftCornerLogoImageUrl = $(
  168. '#custom-top-left-corner-logo-image-url',
  169. )
  170. .val()
  171. .trim();
  172. const customTopLeftCornerLogoLinkUrl = $(
  173. '#custom-top-left-corner-logo-link-url',
  174. )
  175. .val()
  176. .trim();
  177. const customTopLeftCornerLogoHeight = $(
  178. '#custom-top-left-corner-logo-height',
  179. )
  180. .val()
  181. .trim();
  182. const hideLogoChange = $('input[name=hideLogo]:checked').val() === 'true';
  183. const displayAuthenticationMethod =
  184. $('input[name=displayAuthenticationMethod]:checked').val() === 'true';
  185. const defaultAuthenticationMethod = $('#defaultAuthenticationMethod').val();
  186. try {
  187. Settings.update(Settings.findOne()._id, {
  188. $set: {
  189. productName,
  190. hideLogo: hideLogoChange,
  191. customLoginLogoImageUrl,
  192. customLoginLogoLinkUrl,
  193. textBelowCustomLoginLogo,
  194. customTopLeftCornerLogoImageUrl,
  195. customTopLeftCornerLogoLinkUrl,
  196. customTopLeftCornerLogoHeight,
  197. displayAuthenticationMethod,
  198. defaultAuthenticationMethod,
  199. },
  200. });
  201. } catch (e) {
  202. return;
  203. } finally {
  204. this.setLoading(false);
  205. }
  206. DocHead.setTitle(productName);
  207. },
  208. sendSMTPTestEmail() {
  209. Meteor.call('sendSMTPTestEmail', (err, ret) => {
  210. if (!err && ret) {
  211. const message = `${TAPi18n.__(ret.message)}: ${ret.email}`;
  212. alert(message);
  213. } else {
  214. const reason = err.reason || '';
  215. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  216. alert(message);
  217. }
  218. });
  219. },
  220. events() {
  221. return [
  222. {
  223. 'click a.js-toggle-registration': this.toggleRegistration,
  224. 'click a.js-toggle-tls': this.toggleTLS,
  225. 'click a.js-setting-menu': this.switchMenu,
  226. 'click a.js-toggle-board-choose': this.checkBoard,
  227. 'click button.js-email-invite': this.inviteThroughEmail,
  228. 'click button.js-save': this.saveMailServerInfo,
  229. 'click button.js-send-smtp-test-email': this.sendSMTPTestEmail,
  230. 'click a.js-toggle-hide-logo': this.toggleHideLogo,
  231. 'click button.js-save-layout': this.saveLayout,
  232. 'click a.js-toggle-display-authentication-method': this
  233. .toggleDisplayAuthenticationMethod,
  234. },
  235. ];
  236. },
  237. }).register('setting');
  238. BlazeComponent.extendComponent({
  239. saveAccountsChange() {
  240. const allowEmailChange =
  241. $('input[name=allowEmailChange]:checked').val() === 'true';
  242. const allowUserNameChange =
  243. $('input[name=allowUserNameChange]:checked').val() === 'true';
  244. const allowUserDelete =
  245. $('input[name=allowUserDelete]:checked').val() === 'true';
  246. AccountSettings.update('accounts-allowEmailChange', {
  247. $set: { booleanValue: allowEmailChange },
  248. });
  249. AccountSettings.update('accounts-allowUserNameChange', {
  250. $set: { booleanValue: allowUserNameChange },
  251. });
  252. AccountSettings.update('accounts-allowUserDelete', {
  253. $set: { booleanValue: allowUserDelete },
  254. });
  255. },
  256. allowEmailChange() {
  257. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  258. },
  259. allowUserNameChange() {
  260. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  261. },
  262. allowUserDelete() {
  263. return AccountSettings.findOne('accounts-allowUserDelete').booleanValue;
  264. },
  265. events() {
  266. return [
  267. {
  268. 'click button.js-accounts-save': this.saveAccountsChange,
  269. },
  270. ];
  271. },
  272. }).register('accountSettings');
  273. BlazeComponent.extendComponent({
  274. onCreated() {
  275. this.loading = new ReactiveVar(false);
  276. },
  277. setLoading(w) {
  278. this.loading.set(w);
  279. },
  280. currentSetting() {
  281. return Announcements.findOne();
  282. },
  283. saveMessage() {
  284. const message = $('#admin-announcement')
  285. .val()
  286. .trim();
  287. Announcements.update(Announcements.findOne()._id, {
  288. $set: { body: message },
  289. });
  290. },
  291. toggleActive() {
  292. this.setLoading(true);
  293. const isActive = this.currentSetting().enabled;
  294. Announcements.update(Announcements.findOne()._id, {
  295. $set: { enabled: !isActive },
  296. });
  297. this.setLoading(false);
  298. if (isActive) {
  299. $('.admin-announcement').slideUp();
  300. } else {
  301. $('.admin-announcement').slideDown();
  302. }
  303. },
  304. events() {
  305. return [
  306. {
  307. 'click a.js-toggle-activemessage': this.toggleActive,
  308. 'click button.js-announcement-save': this.saveMessage,
  309. },
  310. ];
  311. },
  312. }).register('announcementSettings');
  313. Template.selectAuthenticationMethod.onCreated(function() {
  314. this.authenticationMethods = new ReactiveVar([]);
  315. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  316. if (result) {
  317. // TODO : add a management of different languages
  318. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  319. this.authenticationMethods.set([
  320. { value: 'password' },
  321. // Gets only the authentication methods availables
  322. ...Object.entries(result)
  323. .filter(e => e[1])
  324. .map(e => ({ value: e[0] })),
  325. ]);
  326. }
  327. });
  328. });
  329. Template.selectAuthenticationMethod.helpers({
  330. authentications() {
  331. return Template.instance().authenticationMethods.get();
  332. },
  333. isSelected(match) {
  334. return Template.instance().data.authenticationMethod === match;
  335. },
  336. });