peopleBody.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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(orgs.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. errorMessage() {
  219. return Template.instance().errorMessage.get();
  220. },
  221. });
  222. Template.editTeamPopup.helpers({
  223. team() {
  224. return Team.findOne(this.teamId);
  225. },
  226. errorMessage() {
  227. return Template.instance().errorMessage.get();
  228. },
  229. });
  230. Template.editUserPopup.helpers({
  231. user() {
  232. return Users.findOne(this.userId);
  233. },
  234. authentications() {
  235. return Template.instance().authenticationMethods.get();
  236. },
  237. isSelected(match) {
  238. const userId = Template.instance().data.userId;
  239. const selected = Users.findOne(userId).authenticationMethod;
  240. return selected === match;
  241. },
  242. isLdap() {
  243. const userId = Template.instance().data.userId;
  244. const selected = Users.findOne(userId).authenticationMethod;
  245. return selected === 'ldap';
  246. },
  247. errorMessage() {
  248. return Template.instance().errorMessage.get();
  249. },
  250. });
  251. Template.newOrgPopup.onCreated(function() {
  252. this.errorMessage = new ReactiveVar('');
  253. });
  254. Template.newTeamPopup.onCreated(function() {
  255. this.errorMessage = new ReactiveVar('');
  256. });
  257. Template.newUserPopup.onCreated(function() {
  258. this.authenticationMethods = new ReactiveVar([]);
  259. this.errorMessage = new ReactiveVar('');
  260. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  261. if (result) {
  262. // TODO : add a management of different languages
  263. // (ex {value: ldap, text: TAPi18n.__('ldap', {}, T9n.getLanguage() || 'en')})
  264. this.authenticationMethods.set([
  265. { value: 'password' },
  266. // Gets only the authentication methods availables
  267. ...Object.entries(result)
  268. .filter(e => e[1])
  269. .map(e => ({ value: e[0] })),
  270. ]);
  271. }
  272. });
  273. });
  274. Template.newOrgPopup.helpers({
  275. org() {
  276. return Org.findOne(this.orgId);
  277. },
  278. errorMessage() {
  279. return Template.instance().errorMessage.get();
  280. },
  281. });
  282. Template.newTeamPopup.helpers({
  283. team() {
  284. return Team.findOne(this.teamId);
  285. },
  286. errorMessage() {
  287. return Template.instance().errorMessage.get();
  288. },
  289. });
  290. Template.newUserPopup.helpers({
  291. user() {
  292. return Users.findOne(this.userId);
  293. },
  294. authentications() {
  295. return Template.instance().authenticationMethods.get();
  296. },
  297. isSelected(match) {
  298. const userId = Template.instance().data.userId;
  299. const selected = Users.findOne(userId).authenticationMethod;
  300. return selected === match;
  301. },
  302. isLdap() {
  303. const userId = Template.instance().data.userId;
  304. const selected = Users.findOne(userId).authenticationMethod;
  305. return selected === 'ldap';
  306. },
  307. errorMessage() {
  308. return Template.instance().errorMessage.get();
  309. },
  310. });
  311. BlazeComponent.extendComponent({
  312. onCreated() {},
  313. org() {
  314. return Org.findOne(this.orgId);
  315. },
  316. events() {
  317. return [
  318. {
  319. 'click a.edit-org': Popup.open('editOrg'),
  320. 'click a.more-settings-org': Popup.open('settingsOrg'),
  321. },
  322. ];
  323. },
  324. }).register('orgRow');
  325. BlazeComponent.extendComponent({
  326. onCreated() {},
  327. team() {
  328. return Team.findOne(this.teamId);
  329. },
  330. events() {
  331. return [
  332. {
  333. 'click a.edit-team': Popup.open('editTeam'),
  334. 'click a.more-settings-team': Popup.open('settingsTeam'),
  335. },
  336. ];
  337. },
  338. }).register('teamRow');
  339. BlazeComponent.extendComponent({
  340. onCreated() {},
  341. user() {
  342. return Users.findOne(this.userId);
  343. },
  344. events() {
  345. return [
  346. {
  347. 'click a.edit-user': Popup.open('editUser'),
  348. 'click a.more-settings-user': Popup.open('settingsUser'),
  349. },
  350. ];
  351. },
  352. }).register('peopleRow');
  353. BlazeComponent.extendComponent({
  354. events() {
  355. return [
  356. {
  357. 'click a.new-org': Popup.open('newOrg'),
  358. },
  359. ];
  360. },
  361. }).register('newOrgRow');
  362. BlazeComponent.extendComponent({
  363. events() {
  364. return [
  365. {
  366. 'click a.new-team': Popup.open('newTeam'),
  367. },
  368. ];
  369. },
  370. }).register('newTeamRow');
  371. BlazeComponent.extendComponent({
  372. events() {
  373. return [
  374. {
  375. 'click a.new-user': Popup.open('newUser'),
  376. },
  377. ];
  378. },
  379. }).register('newUserRow');
  380. Template.editUserPopup.events({
  381. submit(event, templateInstance) {
  382. event.preventDefault();
  383. const user = Users.findOne(this.userId);
  384. const username = templateInstance.find('.js-profile-username').value.trim();
  385. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  386. const initials = templateInstance.find('.js-profile-initials').value.trim();
  387. const password = templateInstance.find('.js-profile-password').value;
  388. const isAdmin = templateInstance.find('.js-profile-isadmin').value.trim();
  389. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  390. const email = templateInstance.find('.js-profile-email').value.trim();
  391. const verified = templateInstance
  392. .find('.js-profile-email-verified')
  393. .value.trim();
  394. const authentication = templateInstance
  395. .find('.js-authenticationMethod')
  396. .value.trim();
  397. const importUsernames = templateInstance
  398. .find('.js-import-usernames')
  399. .value.trim();
  400. const isChangePassword = password.length > 0;
  401. const isChangeUserName = username !== user.username;
  402. const isChangeInitials = initials.length > 0;
  403. const isChangeEmailVerified = verified !== user.emails[0].verified;
  404. // If previously email address has not been set, it is undefined,
  405. // check for undefined, and allow adding email address.
  406. const isChangeEmail =
  407. email.toLowerCase() !==
  408. (typeof user.emails !== 'undefined'
  409. ? user.emails[0].address.toLowerCase()
  410. : false);
  411. Users.update(this.userId, {
  412. $set: {
  413. 'profile.fullname': fullname,
  414. isAdmin: isAdmin === 'true',
  415. loginDisabled: isActive === 'true',
  416. authenticationMethod: authentication,
  417. importUsernames: Users.parseImportUsernames(importUsernames),
  418. },
  419. });
  420. if (isChangePassword) {
  421. Meteor.call('setPassword', password, this.userId);
  422. }
  423. if (isChangeEmailVerified) {
  424. Meteor.call('setEmailVerified', email, verified === 'true', this.userId);
  425. }
  426. if (isChangeInitials) {
  427. Meteor.call('setInitials', initials, this.userId);
  428. }
  429. if (isChangeUserName && isChangeEmail) {
  430. Meteor.call(
  431. 'setUsernameAndEmail',
  432. username,
  433. email.toLowerCase(),
  434. this.userId,
  435. function(error) {
  436. const usernameMessageElement = templateInstance.$('.username-taken');
  437. const emailMessageElement = templateInstance.$('.email-taken');
  438. if (error) {
  439. const errorElement = error.error;
  440. if (errorElement === 'username-already-taken') {
  441. usernameMessageElement.show();
  442. emailMessageElement.hide();
  443. } else if (errorElement === 'email-already-taken') {
  444. usernameMessageElement.hide();
  445. emailMessageElement.show();
  446. }
  447. } else {
  448. usernameMessageElement.hide();
  449. emailMessageElement.hide();
  450. Popup.close();
  451. }
  452. },
  453. );
  454. } else if (isChangeUserName) {
  455. Meteor.call('setUsername', username, this.userId, function(error) {
  456. const usernameMessageElement = templateInstance.$('.username-taken');
  457. if (error) {
  458. const errorElement = error.error;
  459. if (errorElement === 'username-already-taken') {
  460. usernameMessageElement.show();
  461. }
  462. } else {
  463. usernameMessageElement.hide();
  464. Popup.close();
  465. }
  466. });
  467. } else if (isChangeEmail) {
  468. Meteor.call('setEmail', email.toLowerCase(), this.userId, function(
  469. error,
  470. ) {
  471. const emailMessageElement = templateInstance.$('.email-taken');
  472. if (error) {
  473. const errorElement = error.error;
  474. if (errorElement === 'email-already-taken') {
  475. emailMessageElement.show();
  476. }
  477. } else {
  478. emailMessageElement.hide();
  479. Popup.close();
  480. }
  481. });
  482. } else Popup.close();
  483. },
  484. });
  485. Template.newOrgPopup.events({
  486. submit(event, templateInstance) {
  487. event.preventDefault();
  488. const displayName = templateInstance.find('.js-displayName').value.trim();
  489. const desc = templateInstance.find('.js-desc').value.trim();
  490. const name = templateInstance.find('.js-name').value.trim();
  491. const teams = templateInstance.find('.js-teams').value.trim();
  492. const website = templateInstance.find('.js-website').value.trim();
  493. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  494. Meteor.call(
  495. 'setCreateOrg',
  496. displayName,
  497. desc,
  498. name,
  499. teams,
  500. website,
  501. isActive,
  502. email.toLowerCase(),
  503. function(error) {
  504. const nameMessageElement = templateInstance.$('.name-taken');
  505. if (error) {
  506. const errorElement = error.error;
  507. if (errorElement === 'name-already-taken') {
  508. nameMessageElement.show();
  509. emailMessageElement.hide();
  510. } else if (errorElement === 'email-already-taken') {
  511. usernameMessageElement.hide();
  512. emailMessageElement.show();
  513. }
  514. } else {
  515. usernameMessageElement.hide();
  516. emailMessageElement.hide();
  517. Popup.close();
  518. }
  519. },
  520. );
  521. Popup.close();
  522. },
  523. });
  524. Template.newUserPopup.events({
  525. submit(event, templateInstance) {
  526. event.preventDefault();
  527. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  528. const username = templateInstance.find('.js-profile-username').value.trim();
  529. const password = templateInstance.find('.js-profile-password').value;
  530. const isAdmin = templateInstance.find('.js-profile-isadmin').value.trim();
  531. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  532. const email = templateInstance.find('.js-profile-email').value.trim();
  533. const importUsernames = templateInstance
  534. .find('.js-import-usernames')
  535. .value.trim();
  536. Meteor.call(
  537. 'setCreateUser',
  538. fullname,
  539. username,
  540. password,
  541. isAdmin,
  542. isActive,
  543. email.toLowerCase(),
  544. importUsernames,
  545. function(error) {
  546. const usernameMessageElement = templateInstance.$('.username-taken');
  547. const emailMessageElement = templateInstance.$('.email-taken');
  548. if (error) {
  549. const errorElement = error.error;
  550. if (errorElement === 'username-already-taken') {
  551. usernameMessageElement.show();
  552. emailMessageElement.hide();
  553. } else if (errorElement === 'email-already-taken') {
  554. usernameMessageElement.hide();
  555. emailMessageElement.show();
  556. }
  557. } else {
  558. usernameMessageElement.hide();
  559. emailMessageElement.hide();
  560. Popup.close();
  561. }
  562. },
  563. );
  564. Popup.close();
  565. },
  566. });
  567. Template.settingsUserPopup.events({
  568. 'click .impersonate-user'(event) {
  569. event.preventDefault();
  570. Meteor.call('impersonate', this.userId, err => {
  571. if (!err) {
  572. FlowRouter.go('/');
  573. Meteor.connection.setUserId(this.userId);
  574. }
  575. });
  576. },
  577. 'click #deleteButton'(event) {
  578. event.preventDefault();
  579. /*
  580. // Delete is not enabled yet, because it does leave empty user avatars
  581. // to boards: boards members, card members and assignees have
  582. // empty users. See:
  583. // - wekan/client/components/settings/peopleBody.jade deleteButton
  584. // - wekan/client/components/settings/peopleBody.js deleteButton
  585. // - wekan/client/components/sidebar/sidebar.js Popup.afterConfirm('removeMember'
  586. // that does now remove member from board, card members and assignees correctly,
  587. // but that should be used to remove user from all boards similarly
  588. // - wekan/models/users.js Delete is not enabled
  589. //
  590. //console.log('user id: ' + this.userId);
  591. //Popup.afterConfirm('userDelete', function(event) {
  592. //Boards.find({ members: this.userId }).forEach(board => {
  593. // console.log('board id: ' + board._id);
  594. //Cards.find({ boardId: board._id, members: this.userId }).forEach(card => {
  595. // card.unassignMember(this.userId);
  596. //});
  597. //Cards.find({ boardId: board._id, members: this.userId }).forEach(card => {
  598. // card.unassignMember(this.userId);
  599. //});
  600. //Cards.find({ boardId: board._id, assignees: this.userId }).forEach(card => {
  601. // card.unassignAssignee(this.userId);
  602. //});
  603. //Boards.findOne({ boardId: board._id }).removeMember(this.userId);
  604. //});
  605. //Users.remove(this.userId);
  606. */
  607. Popup.close();
  608. },
  609. });
  610. Template.settingsUserPopup.helpers({
  611. user() {
  612. return Users.findOne(this.userId);
  613. },
  614. authentications() {
  615. return Template.instance().authenticationMethods.get();
  616. },
  617. isSelected(match) {
  618. const userId = Template.instance().data.userId;
  619. const selected = Users.findOne(userId).authenticationMethod;
  620. return selected === match;
  621. },
  622. isLdap() {
  623. const userId = Template.instance().data.userId;
  624. const selected = Users.findOne(userId).authenticationMethod;
  625. return selected === 'ldap';
  626. },
  627. errorMessage() {
  628. return Template.instance().errorMessage.get();
  629. },
  630. });