peopleBody.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const usersPerPage = 25;
  2. BlazeComponent.extendComponent({
  3. mixins() {
  4. return [Mixins.InfiniteScrolling];
  5. },
  6. onCreated() {
  7. this.error = new ReactiveVar('');
  8. this.loading = new ReactiveVar(false);
  9. this.people = new ReactiveVar(true);
  10. this.page = new ReactiveVar(1);
  11. this.loadNextPageLocked = false;
  12. this.callFirstWith(null, 'resetNextPeak');
  13. this.autorun(() => {
  14. const limit = this.page.get() * usersPerPage;
  15. this.subscribe('people', limit, () => {
  16. this.loadNextPageLocked = false;
  17. const nextPeakBefore = this.callFirstWith(null, 'getNextPeak');
  18. this.calculateNextPeak();
  19. const nextPeakAfter = this.callFirstWith(null, 'getNextPeak');
  20. if (nextPeakBefore === nextPeakAfter) {
  21. this.callFirstWith(null, 'resetNextPeak');
  22. }
  23. });
  24. });
  25. },
  26. loadNextPage() {
  27. if (this.loadNextPageLocked === false) {
  28. this.page.set(this.page.get() + 1);
  29. this.loadNextPageLocked = true;
  30. }
  31. },
  32. calculateNextPeak() {
  33. const element = this.find('.main-body');
  34. if (element) {
  35. const altitude = element.scrollHeight;
  36. this.callFirstWith(this, 'setNextPeak', altitude);
  37. }
  38. },
  39. reachNextPeak() {
  40. this.loadNextPage();
  41. },
  42. setError(error) {
  43. this.error.set(error);
  44. },
  45. setLoading(w) {
  46. this.loading.set(w);
  47. },
  48. peopleList() {
  49. return Users.find({}, {
  50. fields: {_id: true},
  51. });
  52. },
  53. }).register('people');
  54. Template.peopleRow.helpers({
  55. userData() {
  56. const userCollection = this.esSearch ? ESSearchResults : Users;
  57. return userCollection.findOne(this.userId);
  58. },
  59. });
  60. Template.editUserPopup.onCreated(function() {
  61. this.authenticationMethods = new ReactiveVar([]);
  62. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  63. if (result) {
  64. // TODO : add a management of different languages
  65. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  66. this.authenticationMethods.set([
  67. {value: 'password'},
  68. // Gets only the authentication methods availables
  69. ...Object.entries(result).filter((e) => e[1]).map((e) => ({value: e[0]})),
  70. ]);
  71. }
  72. });
  73. });
  74. Template.editUserPopup.helpers({
  75. user() {
  76. return Users.findOne(this.userId);
  77. },
  78. authentications() {
  79. return Template.instance().authenticationMethods.get();
  80. },
  81. isSelected(match) {
  82. const userId = Template.instance().data.userId;
  83. const selected = Users.findOne(userId).authenticationMethod;
  84. return selected === match;
  85. },
  86. isLdap() {
  87. const userId = Template.instance().data.userId;
  88. const selected = Users.findOne(userId).authenticationMethod;
  89. return selected === 'ldap';
  90. },
  91. });
  92. BlazeComponent.extendComponent({
  93. onCreated() {
  94. },
  95. user() {
  96. return Users.findOne(this.userId);
  97. },
  98. events() {
  99. return [{
  100. 'click a.edit-user': Popup.open('editUser'),
  101. }];
  102. },
  103. }).register('peopleRow');
  104. Template.editUserPopup.events({
  105. submit(evt, tpl) {
  106. evt.preventDefault();
  107. const user = Users.findOne(this.userId);
  108. const fullname = tpl.find('.js-profile-fullname').value.trim();
  109. const username = tpl.find('.js-profile-username').value.trim();
  110. const password = tpl.find('.js-profile-password').value;
  111. const isAdmin = tpl.find('.js-profile-isadmin').value.trim();
  112. const isActive = tpl.find('.js-profile-isactive').value.trim();
  113. const email = tpl.find('.js-profile-email').value.trim();
  114. const authentication = tpl.find('.js-authenticationMethod').value.trim();
  115. const isChangePassword = password.length > 0;
  116. const isChangeUserName = username !== user.username;
  117. const isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase();
  118. Users.update(this.userId, {
  119. $set: {
  120. 'profile.fullname': fullname,
  121. 'isAdmin': isAdmin === 'true',
  122. 'loginDisabled': isActive === 'true',
  123. 'authenticationMethod': authentication,
  124. },
  125. });
  126. if(isChangePassword){
  127. Meteor.call('setPassword', password, this.userId);
  128. }
  129. if (isChangeUserName && isChangeEmail) {
  130. Meteor.call('setUsernameAndEmail', username, email.toLowerCase(), this.userId, function (error) {
  131. const usernameMessageElement = tpl.$('.username-taken');
  132. const emailMessageElement = tpl.$('.email-taken');
  133. if (error) {
  134. const errorElement = error.error;
  135. if (errorElement === 'username-already-taken') {
  136. usernameMessageElement.show();
  137. emailMessageElement.hide();
  138. } else if (errorElement === 'email-already-taken') {
  139. usernameMessageElement.hide();
  140. emailMessageElement.show();
  141. }
  142. } else {
  143. usernameMessageElement.hide();
  144. emailMessageElement.hide();
  145. Popup.close();
  146. }
  147. });
  148. } else if (isChangeUserName) {
  149. Meteor.call('setUsername', username, this.userId, function (error) {
  150. const usernameMessageElement = tpl.$('.username-taken');
  151. if (error) {
  152. const errorElement = error.error;
  153. if (errorElement === 'username-already-taken') {
  154. usernameMessageElement.show();
  155. }
  156. } else {
  157. usernameMessageElement.hide();
  158. Popup.close();
  159. }
  160. });
  161. } else if (isChangeEmail) {
  162. Meteor.call('setEmail', email.toLowerCase(), this.userId, function (error) {
  163. const emailMessageElement = tpl.$('.email-taken');
  164. if (error) {
  165. const errorElement = error.error;
  166. if (errorElement === 'email-already-taken') {
  167. emailMessageElement.show();
  168. }
  169. } else {
  170. emailMessageElement.hide();
  171. Popup.close();
  172. }
  173. });
  174. } else Popup.close();
  175. },
  176. });