settingBody.js 9.9 KB

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