settingBody.js 10 KB

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