settingBody.js 13 KB

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