settingBody.js 11 KB

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