settingBody.js 9.0 KB

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