settingBody.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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: ['title'],
  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 hideLogoChange = $('input[name=hideLogo]:checked').val() === 'true';
  159. const displayAuthenticationMethod =
  160. $('input[name=displayAuthenticationMethod]:checked').val() === 'true';
  161. const defaultAuthenticationMethod = $('#defaultAuthenticationMethod').val();
  162. const customHTMLafterBodyStart = $('#customHTMLafterBodyStart')
  163. .val()
  164. .trim();
  165. const customHTMLbeforeBodyEnd = $('#customHTMLbeforeBodyEnd')
  166. .val()
  167. .trim();
  168. try {
  169. Settings.update(Settings.findOne()._id, {
  170. $set: {
  171. productName,
  172. hideLogo: hideLogoChange,
  173. customHTMLafterBodyStart,
  174. customHTMLbeforeBodyEnd,
  175. displayAuthenticationMethod,
  176. defaultAuthenticationMethod,
  177. },
  178. });
  179. } catch (e) {
  180. return;
  181. } finally {
  182. this.setLoading(false);
  183. }
  184. DocHead.setTitle(productName);
  185. },
  186. sendSMTPTestEmail() {
  187. Meteor.call('sendSMTPTestEmail', (err, ret) => {
  188. if (!err && ret) {
  189. const message = `${TAPi18n.__(ret.message)}: ${ret.email}`;
  190. alert(message);
  191. } else {
  192. const reason = err.reason || '';
  193. const message = `${TAPi18n.__(err.error)}\n${reason}`;
  194. alert(message);
  195. }
  196. });
  197. },
  198. events() {
  199. return [
  200. {
  201. 'click a.js-toggle-registration': this.toggleRegistration,
  202. 'click a.js-toggle-tls': this.toggleTLS,
  203. 'click a.js-setting-menu': this.switchMenu,
  204. 'click a.js-toggle-board-choose': this.checkBoard,
  205. 'click button.js-email-invite': this.inviteThroughEmail,
  206. 'click button.js-save': this.saveMailServerInfo,
  207. 'click button.js-send-smtp-test-email': this.sendSMTPTestEmail,
  208. 'click a.js-toggle-hide-logo': this.toggleHideLogo,
  209. 'click button.js-save-layout': this.saveLayout,
  210. 'click a.js-toggle-display-authentication-method': this
  211. .toggleDisplayAuthenticationMethod,
  212. },
  213. ];
  214. },
  215. }).register('setting');
  216. BlazeComponent.extendComponent({
  217. saveAccountsChange() {
  218. const allowEmailChange =
  219. $('input[name=allowEmailChange]:checked').val() === 'true';
  220. const allowUserNameChange =
  221. $('input[name=allowUserNameChange]:checked').val() === 'true';
  222. const allowUserDelete =
  223. $('input[name=allowUserDelete]:checked').val() === 'true';
  224. AccountSettings.update('accounts-allowEmailChange', {
  225. $set: { booleanValue: allowEmailChange },
  226. });
  227. AccountSettings.update('accounts-allowUserNameChange', {
  228. $set: { booleanValue: allowUserNameChange },
  229. });
  230. AccountSettings.update('accounts-allowUserDelete', {
  231. $set: { booleanValue: allowUserDelete },
  232. });
  233. },
  234. allowEmailChange() {
  235. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  236. },
  237. allowUserNameChange() {
  238. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  239. },
  240. allowUserDelete() {
  241. return AccountSettings.findOne('accounts-allowUserDelete').booleanValue;
  242. },
  243. events() {
  244. return [
  245. {
  246. 'click button.js-accounts-save': this.saveAccountsChange,
  247. },
  248. ];
  249. },
  250. }).register('accountSettings');
  251. BlazeComponent.extendComponent({
  252. onCreated() {
  253. this.loading = new ReactiveVar(false);
  254. },
  255. setLoading(w) {
  256. this.loading.set(w);
  257. },
  258. currentSetting() {
  259. return Announcements.findOne();
  260. },
  261. saveMessage() {
  262. const message = $('#admin-announcement')
  263. .val()
  264. .trim();
  265. Announcements.update(Announcements.findOne()._id, {
  266. $set: { body: message },
  267. });
  268. },
  269. toggleActive() {
  270. this.setLoading(true);
  271. const isActive = this.currentSetting().enabled;
  272. Announcements.update(Announcements.findOne()._id, {
  273. $set: { enabled: !isActive },
  274. });
  275. this.setLoading(false);
  276. if (isActive) {
  277. $('.admin-announcement').slideUp();
  278. } else {
  279. $('.admin-announcement').slideDown();
  280. }
  281. },
  282. events() {
  283. return [
  284. {
  285. 'click a.js-toggle-activemessage': this.toggleActive,
  286. 'click button.js-announcement-save': this.saveMessage,
  287. },
  288. ];
  289. },
  290. }).register('announcementSettings');
  291. Template.selectAuthenticationMethod.onCreated(function() {
  292. this.authenticationMethods = new ReactiveVar([]);
  293. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  294. if (result) {
  295. // TODO : add a management of different languages
  296. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  297. this.authenticationMethods.set([
  298. { value: 'password' },
  299. // Gets only the authentication methods availables
  300. ...Object.entries(result)
  301. .filter(e => e[1])
  302. .map(e => ({ value: e[0] })),
  303. ]);
  304. }
  305. });
  306. });
  307. Template.selectAuthenticationMethod.helpers({
  308. authentications() {
  309. return Template.instance().authenticationMethods.get();
  310. },
  311. isSelected(match) {
  312. return Template.instance().data.authenticationMethod === match;
  313. },
  314. });