peopleBody.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. this.errorMessage = new ReactiveVar('');
  97. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  98. if (result) {
  99. // TODO : add a management of different languages
  100. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  101. this.authenticationMethods.set([
  102. {value: 'password'},
  103. // Gets only the authentication methods availables
  104. ...Object.entries(result).filter((e) => e[1]).map((e) => ({value: e[0]})),
  105. ]);
  106. }
  107. });
  108. });
  109. Template.editUserPopup.helpers({
  110. user() {
  111. return Users.findOne(this.userId);
  112. },
  113. authentications() {
  114. return Template.instance().authenticationMethods.get();
  115. },
  116. isSelected(match) {
  117. const userId = Template.instance().data.userId;
  118. const selected = Users.findOne(userId).authenticationMethod;
  119. return selected === match;
  120. },
  121. isLdap() {
  122. const userId = Template.instance().data.userId;
  123. const selected = Users.findOne(userId).authenticationMethod;
  124. return selected === 'ldap';
  125. },
  126. errorMessage() {
  127. return Template.instance().errorMessage.get();
  128. },
  129. });
  130. BlazeComponent.extendComponent({
  131. onCreated() {
  132. },
  133. user() {
  134. return Users.findOne(this.userId);
  135. },
  136. events() {
  137. return [{
  138. 'click a.edit-user': Popup.open('editUser'),
  139. }];
  140. },
  141. }).register('peopleRow');
  142. Template.editUserPopup.events({
  143. submit(evt, tpl) {
  144. evt.preventDefault();
  145. const user = Users.findOne(this.userId);
  146. const fullname = tpl.find('.js-profile-fullname').value.trim();
  147. const username = tpl.find('.js-profile-username').value.trim();
  148. const password = tpl.find('.js-profile-password').value;
  149. const isAdmin = tpl.find('.js-profile-isadmin').value.trim();
  150. const isActive = tpl.find('.js-profile-isactive').value.trim();
  151. const email = tpl.find('.js-profile-email').value.trim();
  152. const authentication = tpl.find('.js-authenticationMethod').value.trim();
  153. const isChangePassword = password.length > 0;
  154. const isChangeUserName = username !== user.username;
  155. const isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase();
  156. Users.update(this.userId, {
  157. $set: {
  158. 'profile.fullname': fullname,
  159. 'isAdmin': isAdmin === 'true',
  160. 'loginDisabled': isActive === 'true',
  161. 'authenticationMethod': authentication,
  162. },
  163. });
  164. if(isChangePassword){
  165. Meteor.call('setPassword', password, this.userId);
  166. }
  167. if (isChangeUserName && isChangeEmail) {
  168. Meteor.call('setUsernameAndEmail', username, email.toLowerCase(), this.userId, function (error) {
  169. const usernameMessageElement = tpl.$('.username-taken');
  170. const emailMessageElement = tpl.$('.email-taken');
  171. if (error) {
  172. const errorElement = error.error;
  173. if (errorElement === 'username-already-taken') {
  174. usernameMessageElement.show();
  175. emailMessageElement.hide();
  176. } else if (errorElement === 'email-already-taken') {
  177. usernameMessageElement.hide();
  178. emailMessageElement.show();
  179. }
  180. } else {
  181. usernameMessageElement.hide();
  182. emailMessageElement.hide();
  183. Popup.close();
  184. }
  185. });
  186. } else if (isChangeUserName) {
  187. Meteor.call('setUsername', username, this.userId, function (error) {
  188. const usernameMessageElement = tpl.$('.username-taken');
  189. if (error) {
  190. const errorElement = error.error;
  191. if (errorElement === 'username-already-taken') {
  192. usernameMessageElement.show();
  193. }
  194. } else {
  195. usernameMessageElement.hide();
  196. Popup.close();
  197. }
  198. });
  199. } else if (isChangeEmail) {
  200. Meteor.call('setEmail', email.toLowerCase(), this.userId, function (error) {
  201. const emailMessageElement = tpl.$('.email-taken');
  202. if (error) {
  203. const errorElement = error.error;
  204. if (errorElement === 'email-already-taken') {
  205. emailMessageElement.show();
  206. }
  207. } else {
  208. emailMessageElement.hide();
  209. Popup.close();
  210. }
  211. });
  212. } else Popup.close();
  213. },
  214. 'click #deleteButton'() {
  215. Users.remove(this.userId);
  216. Popup.close();
  217. },
  218. });