2
0

settingBody.js 13 KB

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