peopleBody.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. 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 isChangePassword = password.length > 0;
  398. const isChangeUserName = username !== user.username;
  399. const isChangeInitials = initials.length > 0;
  400. const isChangeEmailVerified = verified !== user.emails[0].verified;
  401. // If previously email address has not been set, it is undefined,
  402. // check for undefined, and allow adding email address.
  403. const isChangeEmail =
  404. email.toLowerCase() !==
  405. (typeof user.emails !== 'undefined'
  406. ? user.emails[0].address.toLowerCase()
  407. : false);
  408. Users.update(this.userId, {
  409. $set: {
  410. 'profile.fullname': fullname,
  411. isAdmin: isAdmin === 'true',
  412. loginDisabled: isActive === 'true',
  413. authenticationMethod: authentication,
  414. },
  415. });
  416. if (isChangePassword) {
  417. Meteor.call('setPassword', password, this.userId);
  418. }
  419. if (isChangeEmailVerified) {
  420. Meteor.call('setEmailVerified', email, verified === 'true', this.userId);
  421. }
  422. if (isChangeInitials) {
  423. Meteor.call('setInitials', initials, this.userId);
  424. }
  425. if (isChangeUserName && isChangeEmail) {
  426. Meteor.call(
  427. 'setUsernameAndEmail',
  428. username,
  429. email.toLowerCase(),
  430. this.userId,
  431. function(error) {
  432. const usernameMessageElement = templateInstance.$('.username-taken');
  433. const emailMessageElement = templateInstance.$('.email-taken');
  434. if (error) {
  435. const errorElement = error.error;
  436. if (errorElement === 'username-already-taken') {
  437. usernameMessageElement.show();
  438. emailMessageElement.hide();
  439. } else if (errorElement === 'email-already-taken') {
  440. usernameMessageElement.hide();
  441. emailMessageElement.show();
  442. }
  443. } else {
  444. usernameMessageElement.hide();
  445. emailMessageElement.hide();
  446. Popup.close();
  447. }
  448. },
  449. );
  450. } else if (isChangeUserName) {
  451. Meteor.call('setUsername', username, this.userId, function(error) {
  452. const usernameMessageElement = templateInstance.$('.username-taken');
  453. if (error) {
  454. const errorElement = error.error;
  455. if (errorElement === 'username-already-taken') {
  456. usernameMessageElement.show();
  457. }
  458. } else {
  459. usernameMessageElement.hide();
  460. Popup.close();
  461. }
  462. });
  463. } else if (isChangeEmail) {
  464. Meteor.call('setEmail', email.toLowerCase(), this.userId, function(
  465. error,
  466. ) {
  467. const emailMessageElement = templateInstance.$('.email-taken');
  468. if (error) {
  469. const errorElement = error.error;
  470. if (errorElement === 'email-already-taken') {
  471. emailMessageElement.show();
  472. }
  473. } else {
  474. emailMessageElement.hide();
  475. Popup.close();
  476. }
  477. });
  478. } else Popup.close();
  479. },
  480. });
  481. Template.newOrgPopup.events({
  482. submit(event, templateInstance) {
  483. event.preventDefault();
  484. const displayName = templateInstance.find('.js-displayName').value.trim();
  485. const desc = templateInstance.find('.js-desc').value.trim();
  486. const name = templateInstance.find('.js-name').value.trim();
  487. const teams = templateInstance.find('.js-teams').value.trim();
  488. const website = templateInstance.find('.js-website').value.trim();
  489. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  490. Meteor.call(
  491. 'setCreateOrg',
  492. displayName,
  493. desc,
  494. name,
  495. teams,
  496. website,
  497. isActive,
  498. email.toLowerCase(),
  499. function(error) {
  500. const nameMessageElement = templateInstance.$('.name-taken');
  501. if (error) {
  502. const errorElement = error.error;
  503. if (errorElement === 'name-already-taken') {
  504. nameMessageElement.show();
  505. emailMessageElement.hide();
  506. } else if (errorElement === 'email-already-taken') {
  507. usernameMessageElement.hide();
  508. emailMessageElement.show();
  509. }
  510. } else {
  511. usernameMessageElement.hide();
  512. emailMessageElement.hide();
  513. Popup.close();
  514. }
  515. },
  516. );
  517. Popup.close();
  518. },
  519. });
  520. Template.newUserPopup.events({
  521. submit(event, templateInstance) {
  522. event.preventDefault();
  523. const fullname = templateInstance.find('.js-profile-fullname').value.trim();
  524. const username = templateInstance.find('.js-profile-username').value.trim();
  525. const password = templateInstance.find('.js-profile-password').value;
  526. const isAdmin = templateInstance.find('.js-profile-isadmin').value.trim();
  527. const isActive = templateInstance.find('.js-profile-isactive').value.trim();
  528. const email = templateInstance.find('.js-profile-email').value.trim();
  529. Meteor.call(
  530. 'setCreateUser',
  531. fullname,
  532. username,
  533. password,
  534. isAdmin,
  535. isActive,
  536. email.toLowerCase(),
  537. function(error) {
  538. const usernameMessageElement = templateInstance.$('.username-taken');
  539. const emailMessageElement = templateInstance.$('.email-taken');
  540. if (error) {
  541. const errorElement = error.error;
  542. if (errorElement === 'username-already-taken') {
  543. usernameMessageElement.show();
  544. emailMessageElement.hide();
  545. } else if (errorElement === 'email-already-taken') {
  546. usernameMessageElement.hide();
  547. emailMessageElement.show();
  548. }
  549. } else {
  550. usernameMessageElement.hide();
  551. emailMessageElement.hide();
  552. Popup.close();
  553. }
  554. },
  555. );
  556. Popup.close();
  557. },
  558. });
  559. Template.settingsUserPopup.events({
  560. 'click .impersonate-user'(event) {
  561. event.preventDefault();
  562. Meteor.call('impersonate', this.userId, err => {
  563. if (!err) {
  564. FlowRouter.go('/');
  565. Meteor.connection.setUserId(this.userId);
  566. }
  567. });
  568. },
  569. 'click #deleteButton'(event) {
  570. event.preventDefault();
  571. /*
  572. // Delete is not enabled yet, because it does leave empty user avatars
  573. // to boards: boards members, card members and assignees have
  574. // empty users. See:
  575. // - wekan/client/components/settings/peopleBody.jade deleteButton
  576. // - wekan/client/components/settings/peopleBody.js deleteButton
  577. // - wekan/client/components/sidebar/sidebar.js Popup.afterConfirm('removeMember'
  578. // that does now remove member from board, card members and assignees correctly,
  579. // but that should be used to remove user from all boards similarly
  580. // - wekan/models/users.js Delete is not enabled
  581. //
  582. //console.log('user id: ' + this.userId);
  583. //Popup.afterConfirm('userDelete', function(event) {
  584. //Boards.find({ members: this.userId }).forEach(board => {
  585. // console.log('board id: ' + board._id);
  586. //Cards.find({ boardId: board._id, members: this.userId }).forEach(card => {
  587. // card.unassignMember(this.userId);
  588. //});
  589. //Cards.find({ boardId: board._id, members: this.userId }).forEach(card => {
  590. // card.unassignMember(this.userId);
  591. //});
  592. //Cards.find({ boardId: board._id, assignees: this.userId }).forEach(card => {
  593. // card.unassignAssignee(this.userId);
  594. //});
  595. //Boards.findOne({ boardId: board._id }).removeMember(this.userId);
  596. //});
  597. //Users.remove(this.userId);
  598. */
  599. Popup.close();
  600. },
  601. });
  602. Template.settingsUserPopup.helpers({
  603. user() {
  604. return Users.findOne(this.userId);
  605. },
  606. authentications() {
  607. return Template.instance().authenticationMethods.get();
  608. },
  609. isSelected(match) {
  610. const userId = Template.instance().data.userId;
  611. const selected = Users.findOne(userId).authenticationMethod;
  612. return selected === match;
  613. },
  614. isLdap() {
  615. const userId = Template.instance().data.userId;
  616. const selected = Users.findOne(userId).authenticationMethod;
  617. return selected === 'ldap';
  618. },
  619. errorMessage() {
  620. return Template.instance().errorMessage.get();
  621. },
  622. });