autogen-change-password.controller.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2018 The PWM Project
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. import {IHelpDeskService, IRandomPasswordResponse, ISuccessResponse} from '../../services/helpdesk.service';
  23. import {IPromise, IQService} from 'angular';
  24. import {IChangePasswordSuccess} from './success-change-password.controller';
  25. const RANDOM_MAPPING_SIZE = 20;
  26. require('./autogen-change-password.component.scss');
  27. export default class AutogenChangePasswordController {
  28. fetchingRandoms: boolean;
  29. passwordSuggestions: string[];
  30. static $inject = [ '$q', 'HelpDeskService', 'IasDialogService', 'personUserKey' ];
  31. constructor(private $q: IQService,
  32. private HelpDeskService: IHelpDeskService,
  33. private IasDialogService: any,
  34. private personUserKey: string) {
  35. this.passwordSuggestions = Array<string>(20).fill('');
  36. this.populatePasswordSuggestions();
  37. }
  38. generateRandomMapping(): number[] {
  39. let map: number[] = [];
  40. for (let i = 0; i < RANDOM_MAPPING_SIZE; i++) {
  41. map.push(i);
  42. }
  43. let randomComparatorFunction = () => 0.5 - Math.random();
  44. map.sort(randomComparatorFunction);
  45. map.sort(randomComparatorFunction);
  46. return map;
  47. }
  48. onChoosePasswordSuggestion(index: number) {
  49. let chosenPassword = this.passwordSuggestions[index];
  50. this.HelpDeskService.setPassword(this.personUserKey, false, chosenPassword)
  51. .then((result: ISuccessResponse) => {
  52. // Send the password and success message to the parent element via the close() method.
  53. let data: IChangePasswordSuccess = { password: chosenPassword, successMessage: result.successMessage };
  54. this.IasDialogService.close(data);
  55. });
  56. }
  57. passwordSuggestionFactory(index: number): any {
  58. return () => {
  59. return this.HelpDeskService.getRandomPassword(this.personUserKey).then(
  60. (result: IRandomPasswordResponse) => {
  61. this.passwordSuggestions[index] = result.password;
  62. }
  63. );
  64. };
  65. }
  66. populatePasswordSuggestions() {
  67. this.fetchingRandoms = true;
  68. let ordering = this.generateRandomMapping();
  69. let promiseChain: IPromise<any> = this.$q.when();
  70. ordering.forEach((index: number) => {
  71. promiseChain = promiseChain.then(this.passwordSuggestionFactory(index));
  72. });
  73. promiseChain.then(() => {
  74. this.fetchingRandoms = false;
  75. });
  76. }
  77. }