settingBody.js 9.2 KB

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