settingBody.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import { ALLOWED_WAIT_SPINNERS } from '/config/const';
  4. BlazeComponent.extendComponent({
  5. onCreated() {
  6. this.error = new ReactiveVar('');
  7. this.loading = new ReactiveVar(false);
  8. this.forgotPasswordSetting = new ReactiveVar(true);
  9. this.generalSetting = new ReactiveVar(true);
  10. this.emailSetting = new ReactiveVar(false);
  11. this.accountSetting = new ReactiveVar(false);
  12. this.tableVisibilityModeSetting = new ReactiveVar(false);
  13. this.announcementSetting = new ReactiveVar(false);
  14. this.layoutSetting = new ReactiveVar(false);
  15. this.webhookSetting = new ReactiveVar(false);
  16. Meteor.subscribe('setting');
  17. Meteor.subscribe('mailServer');
  18. Meteor.subscribe('accountSettings');
  19. Meteor.subscribe('tableVisibilityModeSettings');
  20. Meteor.subscribe('announcements');
  21. Meteor.subscribe('globalwebhooks');
  22. },
  23. setError(error) {
  24. this.error.set(error);
  25. },
  26. setLoading(w) {
  27. this.loading.set(w);
  28. },
  29. checkField(selector) {
  30. const value = $(selector).val();
  31. if (!value || value.trim() === '') {
  32. $(selector)
  33. .parents('li.smtp-form')
  34. .addClass('has-error');
  35. throw Error('blank field');
  36. } else {
  37. return value;
  38. }
  39. },
  40. boards() {
  41. const ret = ReactiveCache.getBoards(
  42. {
  43. archived: false,
  44. 'members.userId': Meteor.userId(),
  45. 'members.isAdmin': true,
  46. },
  47. {
  48. sort: { sort: 1 /* boards default sorting */ },
  49. },
  50. );
  51. return ret;
  52. },
  53. toggleForgotPassword() {
  54. this.setLoading(true);
  55. const forgotPasswordClosed = ReactiveCache.getCurrentSetting().disableForgotPassword;
  56. Settings.update(ReactiveCache.getCurrentSetting()._id, {
  57. $set: { disableForgotPassword: !forgotPasswordClosed },
  58. });
  59. this.setLoading(false);
  60. },
  61. toggleRegistration() {
  62. this.setLoading(true);
  63. const registrationClosed = ReactiveCache.getCurrentSetting().disableRegistration;
  64. Settings.update(ReactiveCache.getCurrentSetting()._id, {
  65. $set: { disableRegistration: !registrationClosed },
  66. });
  67. this.setLoading(false);
  68. if (registrationClosed) {
  69. $('.invite-people').slideUp();
  70. } else {
  71. $('.invite-people').slideDown();
  72. }
  73. },
  74. toggleTLS() {
  75. $('#mail-server-tls').toggleClass('is-checked');
  76. },
  77. toggleHideLogo() {
  78. $('#hide-logo').toggleClass('is-checked');
  79. },
  80. toggleHideCardCounterList() {
  81. $('#hide-card-counter-list').toggleClass('is-checked');
  82. },
  83. toggleHideBoardMemberList() {
  84. $('#hide-board-member-list').toggleClass('is-checked');
  85. },
  86. toggleDisplayAuthenticationMethod() {
  87. $('#display-authentication-method').toggleClass('is-checked');
  88. },
  89. switchMenu(event) {
  90. const target = $(event.target);
  91. if (!target.hasClass('active')) {
  92. $('.side-menu li.active').removeClass('active');
  93. target.parent().addClass('active');
  94. const targetID = target.data('id');
  95. this.forgotPasswordSetting.set('forgot-password-setting' === targetID);
  96. this.generalSetting.set('registration-setting' === targetID);
  97. this.emailSetting.set('email-setting' === targetID);
  98. this.accountSetting.set('account-setting' === targetID);
  99. this.announcementSetting.set('announcement-setting' === targetID);
  100. this.layoutSetting.set('layout-setting' === targetID);
  101. this.webhookSetting.set('webhook-setting' === targetID);
  102. this.tableVisibilityModeSetting.set('tableVisibilityMode-setting' === targetID);
  103. }
  104. },
  105. checkBoard(event) {
  106. let target = $(event.target);
  107. if (!target.hasClass('js-toggle-board-choose')) {
  108. target = target.parent();
  109. }
  110. const checkboxId = target.attr('id');
  111. $(`#${checkboxId} .materialCheckBox`).toggleClass('is-checked');
  112. $(`#${checkboxId}`).toggleClass('is-checked');
  113. },
  114. inviteThroughEmail() {
  115. const emails = $('#email-to-invite')
  116. .val()
  117. .toLowerCase()
  118. .trim()
  119. .split('\n')
  120. .join(',')
  121. .split(',');
  122. const boardsToInvite = [];
  123. $('.js-toggle-board-choose .materialCheckBox.is-checked').each(function() {
  124. boardsToInvite.push($(this).data('id'));
  125. });
  126. const validEmails = [];
  127. emails.forEach(email => {
  128. if (email && SimpleSchema.RegEx.Email.test(email.trim())) {
  129. validEmails.push(email.trim());
  130. }
  131. });
  132. if (validEmails.length) {
  133. this.setLoading(true);
  134. Meteor.call('sendInvitation', validEmails, boardsToInvite, () => {
  135. // if (!err) {
  136. // TODO - show more info to user
  137. // }
  138. this.setLoading(false);
  139. });
  140. }
  141. },
  142. saveMailServerInfo() {
  143. this.setLoading(true);
  144. $('li').removeClass('has-error');
  145. try {
  146. const host = this.checkField('#mail-server-host');
  147. const port = this.checkField('#mail-server-port');
  148. const username = $('#mail-server-username')
  149. .val()
  150. .trim();
  151. const password = $('#mail-server-password')
  152. .val()
  153. .trim();
  154. const from = this.checkField('#mail-server-from');
  155. const tls = $('#mail-server-tls.is-checked').length > 0;
  156. Settings.update(ReactiveCache.getCurrentSetting()._id, {
  157. $set: {
  158. 'mailServer.host': host,
  159. 'mailServer.port': port,
  160. 'mailServer.username': username,
  161. 'mailServer.password': password,
  162. 'mailServer.enableTLS': tls,
  163. 'mailServer.from': from,
  164. },
  165. });
  166. } catch (e) {
  167. return;
  168. } finally {
  169. this.setLoading(false);
  170. }
  171. },
  172. saveLayout() {
  173. this.setLoading(true);
  174. $('li').removeClass('has-error');
  175. const productName = $('#product-name')
  176. .val()
  177. .trim();
  178. const customLoginLogoImageUrl = $('#custom-login-logo-image-url')
  179. .val()
  180. .trim();
  181. const customLoginLogoLinkUrl = $('#custom-login-logo-link-url')
  182. .val()
  183. .trim();
  184. const customHelpLinkUrl = $('#custom-help-link-url')
  185. .val()
  186. .trim();
  187. const textBelowCustomLoginLogo = $('#text-below-custom-login-logo')
  188. .val()
  189. .trim();
  190. const automaticLinkedUrlSchemes = $('#automatic-linked-url-schemes')
  191. .val()
  192. .trim();
  193. const customTopLeftCornerLogoImageUrl = $(
  194. '#custom-top-left-corner-logo-image-url',
  195. )
  196. .val()
  197. .trim();
  198. const customTopLeftCornerLogoLinkUrl = $(
  199. '#custom-top-left-corner-logo-link-url',
  200. )
  201. .val()
  202. .trim();
  203. const customTopLeftCornerLogoHeight = $(
  204. '#custom-top-left-corner-logo-height',
  205. )
  206. .val()
  207. .trim();
  208. const oidcBtnText = $(
  209. '#oidcBtnTextvalue',
  210. )
  211. .val()
  212. .trim();
  213. const mailDomainName = $(
  214. '#mailDomainNamevalue',
  215. )
  216. .val()
  217. .trim();
  218. const legalNotice = $(
  219. '#legalNoticevalue',
  220. )
  221. .val()
  222. .trim();
  223. const hideLogoChange = $('input[name=hideLogo]:checked').val() === 'true';
  224. const hideCardCounterListChange = $('input[name=hideCardCounterList]:checked').val() === 'true';
  225. const hideBoardMemberListChange = $('input[name=hideBoardMemberList]:checked').val() === 'true';
  226. const displayAuthenticationMethod =
  227. $('input[name=displayAuthenticationMethod]:checked').val() === 'true';
  228. const defaultAuthenticationMethod = $('#defaultAuthenticationMethod').val();
  229. const spinnerName = $('#spinnerName').val();
  230. try {
  231. Settings.update(ReactiveCache.getCurrentSetting()._id, {
  232. $set: {
  233. productName,
  234. hideLogo: hideLogoChange,
  235. hideCardCounterList: hideCardCounterListChange,
  236. hideBoardMemberList: hideBoardMemberListChange,
  237. customLoginLogoImageUrl,
  238. customLoginLogoLinkUrl,
  239. customHelpLinkUrl,
  240. textBelowCustomLoginLogo,
  241. customTopLeftCornerLogoImageUrl,
  242. customTopLeftCornerLogoLinkUrl,
  243. customTopLeftCornerLogoHeight,
  244. displayAuthenticationMethod,
  245. defaultAuthenticationMethod,
  246. automaticLinkedUrlSchemes,
  247. spinnerName,
  248. oidcBtnText,
  249. mailDomainName,
  250. legalNotice,
  251. },
  252. });
  253. } catch (e) {
  254. return;
  255. } finally {
  256. this.setLoading(false);
  257. }
  258. DocHead.setTitle(productName);
  259. },
  260. sendSMTPTestEmail() {
  261. Meteor.call('sendSMTPTestEmail', (err, ret) => {
  262. if (!err && ret) {
  263. const message = `${TAPi18n.__(ret.message)}: ${ret.email}`;
  264. alert(message);
  265. } else {
  266. const reason = err.reason || '';
  267. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  268. alert(message);
  269. }
  270. });
  271. },
  272. events() {
  273. return [
  274. {
  275. 'click a.js-toggle-forgot-password': this.toggleForgotPassword,
  276. 'click a.js-toggle-registration': this.toggleRegistration,
  277. 'click a.js-toggle-tls': this.toggleTLS,
  278. 'click a.js-setting-menu': this.switchMenu,
  279. 'click a.js-toggle-board-choose': this.checkBoard,
  280. 'click button.js-email-invite': this.inviteThroughEmail,
  281. 'click button.js-save': this.saveMailServerInfo,
  282. 'click button.js-send-smtp-test-email': this.sendSMTPTestEmail,
  283. 'click a.js-toggle-hide-logo': this.toggleHideLogo,
  284. 'click a.js-toggle-hide-card-counter-list': this.toggleHideCardCounterList,
  285. 'click a.js-toggle-hide-board-member-list': this.toggleHideBoardMemberList,
  286. 'click button.js-save-layout': this.saveLayout,
  287. 'click a.js-toggle-display-authentication-method': this
  288. .toggleDisplayAuthenticationMethod,
  289. },
  290. ];
  291. },
  292. }).register('setting');
  293. BlazeComponent.extendComponent({
  294. saveAccountsChange() {
  295. const allowEmailChange =
  296. $('input[name=allowEmailChange]:checked').val() === 'true';
  297. const allowUserNameChange =
  298. $('input[name=allowUserNameChange]:checked').val() === 'true';
  299. const allowUserDelete =
  300. $('input[name=allowUserDelete]:checked').val() === 'true';
  301. AccountSettings.update('accounts-allowEmailChange', {
  302. $set: { booleanValue: allowEmailChange },
  303. });
  304. AccountSettings.update('accounts-allowUserNameChange', {
  305. $set: { booleanValue: allowUserNameChange },
  306. });
  307. AccountSettings.update('accounts-allowUserDelete', {
  308. $set: { booleanValue: allowUserDelete },
  309. });
  310. },
  311. allowEmailChange() {
  312. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  313. },
  314. allowUserNameChange() {
  315. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  316. },
  317. allowUserDelete() {
  318. return AccountSettings.findOne('accounts-allowUserDelete').booleanValue;
  319. },
  320. allBoardsHideActivities() {
  321. Meteor.call('setAllBoardsHideActivities', (err, ret) => {
  322. if (!err && ret) {
  323. if (ret === true) {
  324. const message = `${TAPi18n.__(
  325. 'now-activities-of-all-boards-are-hidden',
  326. )}`;
  327. alert(message);
  328. }
  329. } else {
  330. const reason = err.reason || '';
  331. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  332. alert(message);
  333. }
  334. });
  335. },
  336. events() {
  337. return [
  338. {
  339. 'click button.js-accounts-save': this.saveAccountsChange,
  340. },
  341. {
  342. 'click button.js-all-boards-hide-activities': this.allBoardsHideActivities,
  343. },
  344. ];
  345. },
  346. }).register('accountSettings');
  347. BlazeComponent.extendComponent({
  348. saveTableVisibilityChange() {
  349. const allowPrivateOnly =
  350. $('input[name=allowPrivateOnly]:checked').val() === 'true';
  351. TableVisibilityModeSettings.update('tableVisibilityMode-allowPrivateOnly', {
  352. $set: { booleanValue: allowPrivateOnly },
  353. });
  354. },
  355. allowPrivateOnly() {
  356. return TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly').booleanValue;
  357. },
  358. allBoardsHideActivities() {
  359. Meteor.call('setAllBoardsHideActivities', (err, ret) => {
  360. if (!err && ret) {
  361. if (ret === true) {
  362. const message = `${TAPi18n.__(
  363. 'now-activities-of-all-boards-are-hidden',
  364. )}`;
  365. alert(message);
  366. }
  367. } else {
  368. const reason = err.reason || '';
  369. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  370. alert(message);
  371. }
  372. });
  373. },
  374. events() {
  375. return [
  376. {
  377. 'click button.js-tableVisibilityMode-save': this.saveTableVisibilityChange,
  378. },
  379. {
  380. 'click button.js-all-boards-hide-activities': this.allBoardsHideActivities,
  381. },
  382. ];
  383. },
  384. }).register('tableVisibilityModeSettings');
  385. BlazeComponent.extendComponent({
  386. onCreated() {
  387. this.loading = new ReactiveVar(false);
  388. },
  389. setLoading(w) {
  390. this.loading.set(w);
  391. },
  392. currentAnnouncements() {
  393. return Announcements.findOne();
  394. },
  395. saveMessage() {
  396. const message = $('#admin-announcement')
  397. .val()
  398. .trim();
  399. Announcements.update(Announcements.findOne()._id, {
  400. $set: { body: message },
  401. });
  402. },
  403. toggleActive() {
  404. this.setLoading(true);
  405. const announcements = this.currentAnnouncements();
  406. const isActive = announcements.enabled;
  407. Announcements.update(announcements._id, {
  408. $set: { enabled: !isActive },
  409. });
  410. this.setLoading(false);
  411. if (isActive) {
  412. $('.admin-announcement').slideUp();
  413. } else {
  414. $('.admin-announcement').slideDown();
  415. }
  416. },
  417. events() {
  418. return [
  419. {
  420. 'click a.js-toggle-activemessage': this.toggleActive,
  421. 'click button.js-announcement-save': this.saveMessage,
  422. },
  423. ];
  424. },
  425. }).register('announcementSettings');
  426. Template.selectAuthenticationMethod.onCreated(function() {
  427. this.authenticationMethods = new ReactiveVar([]);
  428. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  429. if (result) {
  430. // TODO : add a management of different languages
  431. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  432. this.authenticationMethods.set([
  433. { value: 'password' },
  434. // Gets only the authentication methods availables
  435. ...Object.entries(result)
  436. .filter(e => e[1])
  437. .map(e => ({ value: e[0] })),
  438. ]);
  439. }
  440. });
  441. });
  442. Template.selectAuthenticationMethod.helpers({
  443. authentications() {
  444. return Template.instance().authenticationMethods.get();
  445. },
  446. isSelected(match) {
  447. return Template.instance().data.authenticationMethod === match;
  448. },
  449. });
  450. Template.selectSpinnerName.helpers({
  451. spinners() {
  452. return ALLOWED_WAIT_SPINNERS;
  453. },
  454. isSelected(match) {
  455. return Template.instance().data.spinnerName === match;
  456. },
  457. });