peopleBody.js 6.5 KB

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