settingBody.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 mailDomainName = $(
  195. '#mailDomainNamevalue',
  196. )
  197. .val()
  198. .trim();
  199. const legalNotice = $(
  200. '#legalNoticevalue',
  201. )
  202. .val()
  203. .trim();
  204. const hideLogoChange = $('input[name=hideLogo]:checked').val() === 'true';
  205. const displayAuthenticationMethod =
  206. $('input[name=displayAuthenticationMethod]:checked').val() === 'true';
  207. const defaultAuthenticationMethod = $('#defaultAuthenticationMethod').val();
  208. const spinnerName = $('#spinnerName').val();
  209. try {
  210. Settings.update(Settings.findOne()._id, {
  211. $set: {
  212. productName,
  213. hideLogo: hideLogoChange,
  214. customLoginLogoImageUrl,
  215. customLoginLogoLinkUrl,
  216. textBelowCustomLoginLogo,
  217. customTopLeftCornerLogoImageUrl,
  218. customTopLeftCornerLogoLinkUrl,
  219. customTopLeftCornerLogoHeight,
  220. displayAuthenticationMethod,
  221. defaultAuthenticationMethod,
  222. automaticLinkedUrlSchemes,
  223. spinnerName,
  224. oidcBtnText,
  225. mailDomainName,
  226. legalNotice,
  227. },
  228. });
  229. } catch (e) {
  230. return;
  231. } finally {
  232. this.setLoading(false);
  233. }
  234. DocHead.setTitle(productName);
  235. },
  236. sendSMTPTestEmail() {
  237. Meteor.call('sendSMTPTestEmail', (err, ret) => {
  238. if (!err && ret) {
  239. const message = `${TAPi18n.__(ret.message)}: ${ret.email}`;
  240. alert(message);
  241. } else {
  242. const reason = err.reason || '';
  243. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  244. alert(message);
  245. }
  246. });
  247. },
  248. events() {
  249. return [
  250. {
  251. 'click a.js-toggle-registration': this.toggleRegistration,
  252. 'click a.js-toggle-tls': this.toggleTLS,
  253. 'click a.js-setting-menu': this.switchMenu,
  254. 'click a.js-toggle-board-choose': this.checkBoard,
  255. 'click button.js-email-invite': this.inviteThroughEmail,
  256. 'click button.js-save': this.saveMailServerInfo,
  257. 'click button.js-send-smtp-test-email': this.sendSMTPTestEmail,
  258. 'click a.js-toggle-hide-logo': this.toggleHideLogo,
  259. 'click button.js-save-layout': this.saveLayout,
  260. 'click a.js-toggle-display-authentication-method': this
  261. .toggleDisplayAuthenticationMethod,
  262. },
  263. ];
  264. },
  265. }).register('setting');
  266. BlazeComponent.extendComponent({
  267. saveAccountsChange() {
  268. const allowEmailChange =
  269. $('input[name=allowEmailChange]:checked').val() === 'true';
  270. const allowUserNameChange =
  271. $('input[name=allowUserNameChange]:checked').val() === 'true';
  272. const allowUserDelete =
  273. $('input[name=allowUserDelete]:checked').val() === 'true';
  274. AccountSettings.update('accounts-allowEmailChange', {
  275. $set: { booleanValue: allowEmailChange },
  276. });
  277. AccountSettings.update('accounts-allowUserNameChange', {
  278. $set: { booleanValue: allowUserNameChange },
  279. });
  280. AccountSettings.update('accounts-allowUserDelete', {
  281. $set: { booleanValue: allowUserDelete },
  282. });
  283. },
  284. allowEmailChange() {
  285. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  286. },
  287. allowUserNameChange() {
  288. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  289. },
  290. allowUserDelete() {
  291. return AccountSettings.findOne('accounts-allowUserDelete').booleanValue;
  292. },
  293. allHideSystemMessages() {
  294. Meteor.call('setAllUsersHideSystemMessages', (err, ret) => {
  295. if (!err && ret) {
  296. if (ret === true) {
  297. const message = `${TAPi18n.__(
  298. 'now-system-messages-of-all-users-are-hidden',
  299. )}`;
  300. alert(message);
  301. }
  302. } else {
  303. const reason = err.reason || '';
  304. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  305. alert(message);
  306. }
  307. });
  308. },
  309. events() {
  310. return [
  311. {
  312. 'click button.js-accounts-save': this.saveAccountsChange,
  313. },
  314. {
  315. 'click button.js-all-hide-system-messages': this.allHideSystemMessages,
  316. },
  317. ];
  318. },
  319. }).register('accountSettings');
  320. BlazeComponent.extendComponent({
  321. saveTableVisibilityChange() {
  322. const allowPrivateOnly =
  323. $('input[name=allowPrivateOnly]:checked').val() === 'true';
  324. TableVisibilityModeSettings.update('tableVisibilityMode-allowPrivateOnly', {
  325. $set: { booleanValue: allowPrivateOnly },
  326. });
  327. },
  328. allowPrivateOnly() {
  329. return TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly').booleanValue;
  330. },
  331. allHideSystemMessages() {
  332. Meteor.call('setAllUsersHideSystemMessages', (err, ret) => {
  333. if (!err && ret) {
  334. if (ret === true) {
  335. const message = `${TAPi18n.__(
  336. 'now-system-messages-of-all-users-are-hidden',
  337. )}`;
  338. alert(message);
  339. }
  340. } else {
  341. const reason = err.reason || '';
  342. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  343. alert(message);
  344. }
  345. });
  346. },
  347. events() {
  348. return [
  349. {
  350. 'click button.js-tableVisibilityMode-save': this.saveTableVisibilityChange,
  351. },
  352. {
  353. 'click button.js-all-hide-system-messages': this.allHideSystemMessages,
  354. },
  355. ];
  356. },
  357. }).register('tableVisibilityModeSettings');
  358. BlazeComponent.extendComponent({
  359. onCreated() {
  360. this.loading = new ReactiveVar(false);
  361. },
  362. setLoading(w) {
  363. this.loading.set(w);
  364. },
  365. currentSetting() {
  366. return Announcements.findOne();
  367. },
  368. saveMessage() {
  369. const message = $('#admin-announcement')
  370. .val()
  371. .trim();
  372. Announcements.update(Announcements.findOne()._id, {
  373. $set: { body: message },
  374. });
  375. },
  376. toggleActive() {
  377. this.setLoading(true);
  378. const isActive = this.currentSetting().enabled;
  379. Announcements.update(Announcements.findOne()._id, {
  380. $set: { enabled: !isActive },
  381. });
  382. this.setLoading(false);
  383. if (isActive) {
  384. $('.admin-announcement').slideUp();
  385. } else {
  386. $('.admin-announcement').slideDown();
  387. }
  388. },
  389. events() {
  390. return [
  391. {
  392. 'click a.js-toggle-activemessage': this.toggleActive,
  393. 'click button.js-announcement-save': this.saveMessage,
  394. },
  395. ];
  396. },
  397. }).register('announcementSettings');
  398. Template.selectAuthenticationMethod.onCreated(function() {
  399. this.authenticationMethods = new ReactiveVar([]);
  400. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  401. if (result) {
  402. // TODO : add a management of different languages
  403. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  404. this.authenticationMethods.set([
  405. { value: 'password' },
  406. // Gets only the authentication methods availables
  407. ...Object.entries(result)
  408. .filter(e => e[1])
  409. .map(e => ({ value: e[0] })),
  410. ]);
  411. }
  412. });
  413. });
  414. Template.selectAuthenticationMethod.helpers({
  415. authentications() {
  416. return Template.instance().authenticationMethods.get();
  417. },
  418. isSelected(match) {
  419. return Template.instance().data.authenticationMethod === match;
  420. },
  421. });
  422. Template.selectSpinnerName.helpers({
  423. spinners() {
  424. return ALLOWED_WAIT_SPINNERS;
  425. },
  426. isSelected(match) {
  427. return Template.instance().data.spinnerName === match;
  428. },
  429. });