settingBody.js 13 KB

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