settingBody.js 12 KB

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