settingBody.js 13 KB

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