peopleBody.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. const orgsPerPage = 25;
  2. const teamsPerPage = 25;
  3. const usersPerPage = 25;
  4. BlazeComponent.extendComponent({
  5. mixins() {
  6. return [Mixins.InfiniteScrolling];
  7. },
  8. onCreated() {
  9. this.error = new ReactiveVar('');
  10. this.loading = new ReactiveVar(false);
  11. this.orgSetting = new ReactiveVar(true);
  12. this.teamSetting = new ReactiveVar(true);
  13. this.peopleSetting = new ReactiveVar(true);
  14. this.findOrgsOptions = new ReactiveVar({});
  15. this.findTeamsOptions = new ReactiveVar({});
  16. this.findUsersOptions = new ReactiveVar({});
  17. this.numberOrgs = new ReactiveVar(0);
  18. this.numberTeams = new ReactiveVar(0);
  19. this.numberPeople = new ReactiveVar(0);
  20. this.page = new ReactiveVar(1);
  21. this.loadNextPageLocked = false;
  22. this.callFirstWith(null, 'resetNextPeak');
  23. this.autorun(() => {
  24. const limitOrgs = this.page.get() * orgsPerPage;
  25. const limitTeams = this.page.get() * teamsPerPage;
  26. const limitUsers = this.page.get() * usersPerPage;
  27. this.subscribe('org', this.findOrgsOptions.get(), limitOrgs, () => {
  28. this.loadNextPageLocked = false;
  29. const nextPeakBefore = this.callFirstWith(null, 'getNextPeak');
  30. this.calculateNextPeak();
  31. const nextPeakAfter = this.callFirstWith(null, 'getNextPeak');
  32. if (nextPeakBefore === nextPeakAfter) {
  33. this.callFirstWith(null, 'resetNextPeak');
  34. }
  35. });
  36. this.subscribe('team', this.findTeamsOptions.get(), limitTeams, () => {
  37. this.loadNextPageLocked = false;
  38. const nextPeakBefore = this.callFirstWith(null, 'getNextPeak');
  39. this.calculateNextPeak();
  40. const nextPeakAfter = this.callFirstWith(null, 'getNextPeak');
  41. if (nextPeakBefore === nextPeakAfter) {
  42. this.callFirstWith(null, 'resetNextPeak');
  43. }
  44. });
  45. this.subscribe('people', this.findUsersOptions.get(), limitUsers, () => {
  46. this.loadNextPageLocked = false;
  47. const nextPeakBefore = this.callFirstWith(null, 'getNextPeak');
  48. this.calculateNextPeak();
  49. const nextPeakAfter = this.callFirstWith(null, 'getNextPeak');
  50. if (nextPeakBefore === nextPeakAfter) {
  51. this.callFirstWith(null, 'resetNextPeak');
  52. }
  53. });
  54. });
  55. },
  56. events() {
  57. return [
  58. {
  59. 'click #searchOrgButton'() {
  60. this.filterOrg();
  61. },
  62. 'keydown #searchOrgInput'(event) {
  63. if (event.keyCode === 13 && !event.shiftKey) {
  64. this.filterOrg();
  65. }
  66. },
  67. 'click #searchTeamButton'() {
  68. this.filterTeam();
  69. },
  70. 'keydown #searchTeamInput'(event) {
  71. if (event.keyCode === 13 && !event.shiftKey) {
  72. this.filterTeam();
  73. }
  74. },
  75. 'click #searchButton'() {
  76. this.filterPeople();
  77. },
  78. 'keydown #searchInput'(event) {
  79. if (event.keyCode === 13 && !event.shiftKey) {
  80. this.filterPeople();
  81. }
  82. },
  83. 'click #newOrgButton'() {
  84. Popup.open('newOrg');
  85. },
  86. 'click #newTeamButton'() {
  87. Popup.open('newTeam');
  88. },
  89. 'click #newUserButton'() {
  90. Popup.open('newUser');
  91. },
  92. 'click a.js-org-menu': this.switchMenu,
  93. 'click a.js-team-menu': this.switchMenu,
  94. 'click a.js-people-menu': this.switchMenu,
  95. },
  96. ];
  97. },
  98. filterPeople() {
  99. const value = $('#searchInput')
  100. .first()
  101. .val();
  102. if (value === '') {
  103. this.findUsersOptions.set({});
  104. } else {
  105. const regex = new RegExp(value, 'i');
  106. this.findUsersOptions.set({
  107. $or: [
  108. { username: regex },
  109. { 'profile.fullname': regex },
  110. { 'emails.address': regex },
  111. ],
  112. });
  113. }
  114. },
  115. loadNextPage() {
  116. if (this.loadNextPageLocked === false) {
  117. this.page.set(this.page.get() + 1);
  118. this.loadNextPageLocked = true;
  119. }
  120. },
  121. calculateNextPeak() {
  122. const element = this.find('.main-body');
  123. if (element) {
  124. const altitude = element.scrollHeight;
  125. this.callFirstWith(this, 'setNextPeak', altitude);
  126. }
  127. },
  128. reachNextPeak() {
  129. this.loadNextPage();
  130. },
  131. setError(error) {
  132. this.error.set(error);
  133. },
  134. setLoading(w) {
  135. this.loading.set(w);
  136. },
  137. orgList() {
  138. const orgs = Org.find(this.findOrgsOptions.get(), {
  139. fields: { _id: true },
  140. });
  141. this.numberOrgs.set(org.count(false));
  142. return orgs;
  143. },
  144. teamList() {
  145. const teams = Team.find(this.findTeamsOptions.get(), {
  146. fields: { _id: true },
  147. });
  148. this.numberTeams.set(team.count(false));
  149. return teams;
  150. },
  151. peopleList() {
  152. const users = Users.find(this.findUsersOptions.get(), {
  153. fields: { _id: true },
  154. });
  155. this.numberPeople.set(users.count(false));
  156. return users;
  157. },
  158. orgNumber() {
  159. return this.numberOrgs.get();
  160. },
  161. teamNumber() {
  162. return this.numberTeams.get();
  163. },
  164. peopleNumber() {
  165. return this.numberPeople.get();
  166. },
  167. switchMenu(event) {
  168. const target = $(event.target);
  169. if (!target.hasClass('active')) {
  170. $('.side-menu li.active').removeClass('active');
  171. target.parent().addClass('active');
  172. const targetID = target.data('id');
  173. this.orgSetting.set('org-setting' === targetID);
  174. this.teamSetting.set('team-setting' === targetID);
  175. this.peopleSetting.set('people-setting' === targetID);
  176. }
  177. },
  178. }).register('people');
  179. Template.orgRow.helpers({
  180. orgData() {
  181. const orgCollection = this.esSearch ? ESSearchResults : Org;
  182. return orgCollection.findOne(this.orgId);
  183. },
  184. });
  185. Template.teamRow.helpers({
  186. teamData() {
  187. const teamCollection = this.esSearch ? ESSearchResults : Team;
  188. return teamCollection.findOne(this.teamId);
  189. },
  190. });
  191. Template.peopleRow.helpers({
  192. userData() {
  193. const userCollection = this.esSearch ? ESSearchResults : Users;
  194. return userCollection.findOne(this.userId);
  195. },
  196. });
  197. Template.editUserPopup.onCreated(function() {
  198. this.authenticationMethods = new ReactiveVar([]);
  199. this.errorMessage = new ReactiveVar('');
  200. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  201. if (result) {
  202. // TODO : add a management of different languages
  203. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  204. this.authenticationMethods.set([
  205. { value: 'password' },
  206. // Gets only the authentication methods availables
  207. ...Object.entries(result)
  208. .filter(e => e[1])
  209. .map(e => ({ value: e[0] })),
  210. ]);
  211. }
  212. });
  213. });
  214. Template.editOrgPopup.helpers({
  215. org() {
  216. return Org.findOne(this.orgId);
  217. },
  218. /*
  219. isSelected(match) {
  220. const orgId = Template.instance().data.orgId;
  221. const selected = Org.findOne(orgId).authenticationMethod;
  222. return selected === match;
  223. },
  224. isLdap() {
  225. const userId = Template.instance().data.userId;
  226. const selected = Users.findOne(userId).authenticationMethod;
  227. return selected === 'ldap';
  228. },
  229. */
  230. errorMessage() {
  231. return Template.instance().errorMessage.get();
  232. },
  233. });
  234. Template.editTeamPopup.helpers({
  235. team() {
  236. return Team.findOne(this.teamId);
  237. },
  238. /*
  239. authentications() {
  240. return Template.instance().authenticationMethods.get();
  241. },
  242. isSelected(match) {
  243. const userId = Template.instance().data.userId;
  244. const selected = Users.findOne(userId).authenticationMethod;
  245. return selected === match;
  246. },
  247. isLdap() {
  248. const userId = Template.instance().data.userId;
  249. const selected = Users.findOne(userId).authenticationMethod;
  250. return selected === 'ldap';
  251. },
  252. */
  253. errorMessage() {
  254. return Template.instance().errorMessage.get();
  255. },
  256. });
  257. Template.editUserPopup.helpers({
  258. user() {
  259. return Users.findOne(this.userId);
  260. },
  261. authentications() {
  262. return Template.instance().authenticationMethods.get();
  263. },
  264. isSelected(match) {
  265. const userId = Template.instance().data.userId;
  266. const selected = Users.findOne(userId).authenticationMethod;
  267. return selected === match;
  268. },
  269. isLdap() {
  270. const userId = Template.instance().data.userId;
  271. const selected = Users.findOne(userId).authenticationMethod;
  272. return selected === 'ldap';
  273. },
  274. errorMessage() {
  275. return Template.instance().errorMessage.get();
  276. },
  277. });
  278. Template.newOrgPopup.onCreated(function() {
  279. //this.authenticationMethods = new ReactiveVar([]);
  280. this.errorMessage = new ReactiveVar('');
  281. /*
  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. });
  297. Template.newTeamPopup.onCreated(function() {
  298. //this.authenticationMethods = new ReactiveVar([]);
  299. this.errorMessage = new ReactiveVar('');
  300. /*
  301. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  302. if (result) {
  303. // TODO : add a management of different languages
  304. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  305. this.authenticationMethods.set([
  306. { value: 'password' },
  307. // Gets only the authentication methods availables
  308. ...Object.entries(result)
  309. .filter(e => e[1])
  310. .map(e => ({ value: e[0] })),
  311. ]);
  312. }
  313. });
  314. */
  315. });
  316. Template.newUserPopup.onCreated(function() {
  317. this.authenticationMethods = new ReactiveVar([]);
  318. this.errorMessage = new ReactiveVar('');
  319. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  320. if (result) {
  321. // TODO : add a management of different languages
  322. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  323. this.authenticationMethods.set([
  324. { value: 'password' },
  325. // Gets only the authentication methods availables
  326. ...Object.entries(result)
  327. .filter(e => e[1])
  328. .map(e => ({ value: e[0] })),
  329. ]);
  330. }
  331. });
  332. });
  333. Template.newUserPopup.helpers({
  334. //user() {
  335. // return Users.findOne(this.userId);
  336. //},
  337. authentications() {
  338. return Template.instance().authenticationMethods.get();
  339. },
  340. //isSelected(match) {
  341. // const userId = Template.instance().data.userId;
  342. // const selected = Users.findOne(userId).authenticationMethod;
  343. // return selected === match;
  344. //},
  345. //isLdap() {
  346. // const userId = Template.instance().data.userId;
  347. // const selected = Users.findOne(userId).authenticationMethod;
  348. // return selected === 'ldap';
  349. //},
  350. errorMessage() {
  351. return Template.instance().errorMessage.get();
  352. },
  353. });
  354. BlazeComponent.extendComponent({
  355. onCreated() {},
  356. user() {
  357. return Users.findOne(this.userId);
  358. },
  359. events() {
  360. return [
  361. {
  362. 'click a.edit-user': Popup.open('editUser'),
  363. 'click a.more-settings-user': Popup.open('settingsUser'),
  364. },
  365. ];
  366. },
  367. }).register('peopleRow');
  368. BlazeComponent.extendComponent({
  369. events() {
  370. return [
  371. {
  372. 'click a.new-user': Popup.open('newUser'),
  373. },
  374. ];
  375. },
  376. }).register('newUserRow');
  377. Template.editUserPopup.events({
  378. submit(event, templateInstance) {
  379. event.preventDefault();
  380. const user = Users.findOne(this.userId);
  381. const username = templateInstance.find('.js-profile-username').value.trim();
  382. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  383. const initials = templateInstance.find('.js-profile-initials').value.trim();
  384. const password = templateInstance.find('.js-profile-password').value;
  385. const isAdmin = templateInstance.find('.js-profile-isadmin').value.trim();
  386. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  387. const email = templateInstance.find('.js-profile-email').value.trim();
  388. const verified = templateInstance
  389. .find('.js-profile-email-verified')
  390. .value.trim();
  391. const authentication = templateInstance
  392. .find('.js-authenticationMethod')
  393. .value.trim();
  394. const isChangePassword = password.length > 0;
  395. const isChangeUserName = username !== user.username;
  396. const isChangeInitials = initials.length > 0;
  397. const isChangeEmailVerified = verified !== user.emails[0].verified;
  398. // If previously email address has not been set, it is undefined,
  399. // check for undefined, and allow adding email address.
  400. const isChangeEmail =
  401. email.toLowerCase() !==
  402. (typeof user.emails !== 'undefined'
  403. ? user.emails[0].address.toLowerCase()
  404. : false);
  405. Users.update(this.userId, {
  406. $set: {
  407. 'profile.fullname': fullname,
  408. isAdmin: isAdmin === 'true',
  409. loginDisabled: isActive === 'true',
  410. authenticationMethod: authentication,
  411. },
  412. });
  413. if (isChangePassword) {
  414. Meteor.call('setPassword', password, this.userId);
  415. }
  416. if (isChangeEmailVerified) {
  417. Meteor.call('setEmailVerified', email, verified === 'true', this.userId);
  418. }
  419. if (isChangeInitials) {
  420. Meteor.call('setInitials', initials, this.userId);
  421. }
  422. if (isChangeUserName && isChangeEmail) {
  423. Meteor.call(
  424. 'setUsernameAndEmail',
  425. username,
  426. email.toLowerCase(),
  427. this.userId,
  428. function(error) {
  429. const usernameMessageElement = templateInstance.$('.username-taken');
  430. const emailMessageElement = templateInstance.$('.email-taken');
  431. if (error) {
  432. const errorElement = error.error;
  433. if (errorElement === 'username-already-taken') {
  434. usernameMessageElement.show();
  435. emailMessageElement.hide();
  436. } else if (errorElement === 'email-already-taken') {
  437. usernameMessageElement.hide();
  438. emailMessageElement.show();
  439. }
  440. } else {
  441. usernameMessageElement.hide();
  442. emailMessageElement.hide();
  443. Popup.close();
  444. }
  445. },
  446. );
  447. } else if (isChangeUserName) {
  448. Meteor.call('setUsername', username, this.userId, function(error) {
  449. const usernameMessageElement = templateInstance.$('.username-taken');
  450. if (error) {
  451. const errorElement = error.error;
  452. if (errorElement === 'username-already-taken') {
  453. usernameMessageElement.show();
  454. }
  455. } else {
  456. usernameMessageElement.hide();
  457. Popup.close();
  458. }
  459. });
  460. } else if (isChangeEmail) {
  461. Meteor.call('setEmail', email.toLowerCase(), this.userId, function(
  462. error,
  463. ) {
  464. const emailMessageElement = templateInstance.$('.email-taken');
  465. if (error) {
  466. const errorElement = error.error;
  467. if (errorElement === 'email-already-taken') {
  468. emailMessageElement.show();
  469. }
  470. } else {
  471. emailMessageElement.hide();
  472. Popup.close();
  473. }
  474. });
  475. } else Popup.close();
  476. },
  477. });
  478. Template.newUserPopup.events({
  479. submit(event, templateInstance) {
  480. event.preventDefault();
  481. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  482. const username = templateInstance.find('.js-profile-username').value.trim();
  483. const password = templateInstance.find('.js-profile-password').value;
  484. const isAdmin = templateInstance.find('.js-profile-isadmin').value.trim();
  485. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  486. const email = templateInstance.find('.js-profile-email').value.trim();
  487. Meteor.call(
  488. 'setCreateUser',
  489. fullname,
  490. username,
  491. password,
  492. isAdmin,
  493. isActive,
  494. email.toLowerCase(),
  495. function(error) {
  496. const usernameMessageElement = templateInstance.$('.username-taken');
  497. const emailMessageElement = templateInstance.$('.email-taken');
  498. if (error) {
  499. const errorElement = error.error;
  500. if (errorElement === 'username-already-taken') {
  501. usernameMessageElement.show();
  502. emailMessageElement.hide();
  503. } else if (errorElement === 'email-already-taken') {
  504. usernameMessageElement.hide();
  505. emailMessageElement.show();
  506. }
  507. } else {
  508. usernameMessageElement.hide();
  509. emailMessageElement.hide();
  510. Popup.close();
  511. }
  512. },
  513. );
  514. Popup.close();
  515. },
  516. });
  517. Template.settingsUserPopup.events({
  518. 'click .impersonate-user'(event) {
  519. event.preventDefault();
  520. Meteor.call('impersonate', this.userId, err => {
  521. if (!err) {
  522. FlowRouter.go('/');
  523. Meteor.connection.setUserId(this.userId);
  524. }
  525. });
  526. },
  527. 'click #deleteButton'(event) {
  528. event.preventDefault();
  529. /*
  530. // Delete is not enabled yet, because it does leave empty user avatars
  531. // to boards: boards members, card members and assignees have
  532. // empty users. See:
  533. // - wekan/client/components/settings/peopleBody.jade deleteButton
  534. // - wekan/client/components/settings/peopleBody.js deleteButton
  535. // - wekan/client/components/sidebar/sidebar.js Popup.afterConfirm('removeMember'
  536. // that does now remove member from board, card members and assignees correctly,
  537. // but that should be used to remove user from all boards similarly
  538. // - wekan/models/users.js Delete is not enabled
  539. //
  540. //console.log('user id: ' + this.userId);
  541. //Popup.afterConfirm('userDelete', function(event) {
  542. //Boards.find({ members: this.userId }).forEach(board => {
  543. // console.log('board id: ' + board._id);
  544. //Cards.find({ boardId: board._id, members: this.userId }).forEach(card => {
  545. // card.unassignMember(this.userId);
  546. //});
  547. //Cards.find({ boardId: board._id, members: this.userId }).forEach(card => {
  548. // card.unassignMember(this.userId);
  549. //});
  550. //Cards.find({ boardId: board._id, assignees: this.userId }).forEach(card => {
  551. // card.unassignAssignee(this.userId);
  552. //});
  553. //Boards.findOne({ boardId: board._id }).removeMember(this.userId);
  554. //});
  555. //Users.remove(this.userId);
  556. */
  557. Popup.close();
  558. },
  559. });
  560. Template.settingsUserPopup.helpers({
  561. user() {
  562. return Users.findOne(this.userId);
  563. },
  564. authentications() {
  565. return Template.instance().authenticationMethods.get();
  566. },
  567. isSelected(match) {
  568. const userId = Template.instance().data.userId;
  569. const selected = Users.findOne(userId).authenticationMethod;
  570. return selected === match;
  571. },
  572. isLdap() {
  573. const userId = Template.instance().data.userId;
  574. const selected = Users.findOne(userId).authenticationMethod;
  575. return selected === 'ldap';
  576. },
  577. errorMessage() {
  578. return Template.instance().errorMessage.get();
  579. },
  580. });