settingBody.js 13 KB

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