peopleBody.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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', this.findUsersOptions.get(), 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. {
  31. 'click #searchButton'() {
  32. this.filterPeople();
  33. },
  34. 'keydown #searchInput'(event) {
  35. if (event.keyCode === 13 && !event.shiftKey) {
  36. this.filterPeople();
  37. }
  38. },
  39. 'click #newUserButton'() {
  40. Popup.open('newUser');
  41. },
  42. },
  43. ];
  44. },
  45. filterPeople() {
  46. const value = $('#searchInput')
  47. .first()
  48. .val();
  49. if (value === '') {
  50. this.findUsersOptions.set({});
  51. } else {
  52. const regex = new RegExp(value, 'i');
  53. this.findUsersOptions.set({
  54. $or: [
  55. { username: regex },
  56. { 'profile.fullname': regex },
  57. { 'emails.address': regex },
  58. ],
  59. });
  60. }
  61. },
  62. loadNextPage() {
  63. if (this.loadNextPageLocked === false) {
  64. this.page.set(this.page.get() + 1);
  65. this.loadNextPageLocked = true;
  66. }
  67. },
  68. calculateNextPeak() {
  69. const element = this.find('.main-body');
  70. if (element) {
  71. const altitude = element.scrollHeight;
  72. this.callFirstWith(this, 'setNextPeak', altitude);
  73. }
  74. },
  75. reachNextPeak() {
  76. this.loadNextPage();
  77. },
  78. setError(error) {
  79. this.error.set(error);
  80. },
  81. setLoading(w) {
  82. this.loading.set(w);
  83. },
  84. peopleList() {
  85. const users = Users.find(this.findUsersOptions.get(), {
  86. fields: { _id: true },
  87. });
  88. this.number.set(users.count(false));
  89. return users;
  90. },
  91. peopleNumber() {
  92. return this.number.get();
  93. },
  94. }).register('people');
  95. Template.peopleRow.helpers({
  96. userData() {
  97. const userCollection = this.esSearch ? ESSearchResults : Users;
  98. return userCollection.findOne(this.userId);
  99. },
  100. });
  101. Template.editUserPopup.onCreated(function() {
  102. this.authenticationMethods = new ReactiveVar([]);
  103. this.errorMessage = new ReactiveVar('');
  104. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  105. if (result) {
  106. // TODO : add a management of different languages
  107. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  108. this.authenticationMethods.set([
  109. { value: 'password' },
  110. // Gets only the authentication methods availables
  111. ...Object.entries(result)
  112. .filter(e => e[1])
  113. .map(e => ({ value: e[0] })),
  114. ]);
  115. }
  116. });
  117. });
  118. Template.editUserPopup.helpers({
  119. user() {
  120. return Users.findOne(this.userId);
  121. },
  122. authentications() {
  123. return Template.instance().authenticationMethods.get();
  124. },
  125. isSelected(match) {
  126. const userId = Template.instance().data.userId;
  127. const selected = Users.findOne(userId).authenticationMethod;
  128. return selected === match;
  129. },
  130. isLdap() {
  131. const userId = Template.instance().data.userId;
  132. const selected = Users.findOne(userId).authenticationMethod;
  133. return selected === 'ldap';
  134. },
  135. errorMessage() {
  136. return Template.instance().errorMessage.get();
  137. },
  138. });
  139. Template.newUserPopup.onCreated(function() {
  140. this.authenticationMethods = new ReactiveVar([]);
  141. this.errorMessage = new ReactiveVar('');
  142. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  143. if (result) {
  144. // TODO : add a management of different languages
  145. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  146. this.authenticationMethods.set([
  147. { value: 'password' },
  148. // Gets only the authentication methods availables
  149. ...Object.entries(result)
  150. .filter(e => e[1])
  151. .map(e => ({ value: e[0] })),
  152. ]);
  153. }
  154. });
  155. });
  156. Template.newUserPopup.helpers({
  157. //user() {
  158. // return Users.findOne(this.userId);
  159. //},
  160. authentications() {
  161. return Template.instance().authenticationMethods.get();
  162. },
  163. //isSelected(match) {
  164. // const userId = Template.instance().data.userId;
  165. // const selected = Users.findOne(userId).authenticationMethod;
  166. // return selected === match;
  167. //},
  168. //isLdap() {
  169. // const userId = Template.instance().data.userId;
  170. // const selected = Users.findOne(userId).authenticationMethod;
  171. // return selected === 'ldap';
  172. //},
  173. errorMessage() {
  174. return Template.instance().errorMessage.get();
  175. },
  176. });
  177. BlazeComponent.extendComponent({
  178. onCreated() {},
  179. user() {
  180. return Users.findOne(this.userId);
  181. },
  182. events() {
  183. return [
  184. {
  185. 'click a.edit-user': Popup.open('editUser'),
  186. },
  187. ];
  188. },
  189. }).register('peopleRow');
  190. BlazeComponent.extendComponent({
  191. events() {
  192. return [
  193. {
  194. 'click a.new-user': Popup.open('newUser'),
  195. },
  196. ];
  197. },
  198. }).register('newUserRow');
  199. Template.editUserPopup.events({
  200. submit(event, templateInstance) {
  201. event.preventDefault();
  202. const user = Users.findOne(this.userId);
  203. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  204. const username = templateInstance.find('.js-profile-username').value.trim();
  205. const password = templateInstance.find('.js-profile-password').value;
  206. const isAdmin = templateInstance.find('.js-profile-isadmin').value.trim();
  207. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  208. const email = templateInstance.find('.js-profile-email').value.trim();
  209. const authentication = templateInstance
  210. .find('.js-authenticationMethod')
  211. .value.trim();
  212. const isChangePassword = password.length > 0;
  213. const isChangeUserName = username !== user.username;
  214. const isChangeEmail =
  215. email.toLowerCase() !== user.emails[0].address.toLowerCase();
  216. Users.update(this.userId, {
  217. $set: {
  218. 'profile.fullname': fullname,
  219. isAdmin: isAdmin === 'true',
  220. loginDisabled: isActive === 'true',
  221. authenticationMethod: authentication,
  222. },
  223. });
  224. if (isChangePassword) {
  225. Meteor.call('setPassword', password, this.userId);
  226. }
  227. if (isChangeUserName && isChangeEmail) {
  228. Meteor.call(
  229. 'setUsernameAndEmail',
  230. username,
  231. email.toLowerCase(),
  232. this.userId,
  233. function(error) {
  234. const usernameMessageElement = templateInstance.$('.username-taken');
  235. const emailMessageElement = templateInstance.$('.email-taken');
  236. if (error) {
  237. const errorElement = error.error;
  238. if (errorElement === 'username-already-taken') {
  239. usernameMessageElement.show();
  240. emailMessageElement.hide();
  241. } else if (errorElement === 'email-already-taken') {
  242. usernameMessageElement.hide();
  243. emailMessageElement.show();
  244. }
  245. } else {
  246. usernameMessageElement.hide();
  247. emailMessageElement.hide();
  248. Popup.close();
  249. }
  250. },
  251. );
  252. } else if (isChangeUserName) {
  253. Meteor.call('setUsername', username, this.userId, function(error) {
  254. const usernameMessageElement = templateInstance.$('.username-taken');
  255. if (error) {
  256. const errorElement = error.error;
  257. if (errorElement === 'username-already-taken') {
  258. usernameMessageElement.show();
  259. }
  260. } else {
  261. usernameMessageElement.hide();
  262. Popup.close();
  263. }
  264. });
  265. } else if (isChangeEmail) {
  266. Meteor.call('setEmail', email.toLowerCase(), this.userId, function(
  267. error,
  268. ) {
  269. const emailMessageElement = templateInstance.$('.email-taken');
  270. if (error) {
  271. const errorElement = error.error;
  272. if (errorElement === 'email-already-taken') {
  273. emailMessageElement.show();
  274. }
  275. } else {
  276. emailMessageElement.hide();
  277. Popup.close();
  278. }
  279. });
  280. } else Popup.close();
  281. },
  282. 'click #deleteButton': Popup.afterConfirm('userDelete', function() {
  283. Users.remove(this.userId);
  284. Popup.close();
  285. }),
  286. });
  287. Template.newUserPopup.events({
  288. submit(event, templateInstance) {
  289. event.preventDefault();
  290. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  291. const username = templateInstance.find('.js-profile-username').value.trim();
  292. const password = templateInstance.find('.js-profile-password').value;
  293. const isAdmin = templateInstance.find('.js-profile-isadmin').value.trim();
  294. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  295. const email = templateInstance.find('.js-profile-email').value.trim();
  296. Meteor.call(
  297. 'setCreateUser',
  298. fullname,
  299. username,
  300. password,
  301. isAdmin,
  302. isActive,
  303. email.toLowerCase(),
  304. function(error) {
  305. const usernameMessageElement = templateInstance.$('.username-taken');
  306. const emailMessageElement = templateInstance.$('.email-taken');
  307. if (error) {
  308. const errorElement = error.error;
  309. if (errorElement === 'username-already-taken') {
  310. usernameMessageElement.show();
  311. emailMessageElement.hide();
  312. } else if (errorElement === 'email-already-taken') {
  313. usernameMessageElement.hide();
  314. emailMessageElement.show();
  315. }
  316. } else {
  317. usernameMessageElement.hide();
  318. emailMessageElement.hide();
  319. Popup.close();
  320. }
  321. },
  322. );
  323. Popup.close();
  324. },
  325. });