autogen-change-password.controller.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2017 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 DialogService from '../ux/ias-dialog.service';
  25. import {IChangePasswordSuccess} from './success-change-password.controller';
  26. const RANDOM_MAPPING_SIZE = 20;
  27. require('changepassword/autogen-change-password.component.scss');
  28. export default class AutogenChangePasswordController {
  29. fetchingRandoms: boolean;
  30. passwordSuggestions: string[];
  31. static $inject = [ '$q', 'HelpDeskService', 'IasDialogService', 'personUserKey' ];
  32. constructor(private $q: IQService,
  33. private HelpDeskService: IHelpDeskService,
  34. private IasDialogService: DialogService,
  35. private personUserKey: string) {
  36. this.passwordSuggestions = Array(20).fill('');
  37. this.populatePasswordSuggestions();
  38. }
  39. generateRandomMapping(): number[] {
  40. let map: number[] = [];
  41. for (let i = 0; i < RANDOM_MAPPING_SIZE; i++) {
  42. map.push(i);
  43. }
  44. let randomComparatorFunction = () => 0.5 - Math.random();
  45. map.sort(randomComparatorFunction);
  46. map.sort(randomComparatorFunction);
  47. return map;
  48. }
  49. onChoosePasswordSuggestion(index: number) {
  50. let chosenPassword = this.passwordSuggestions[index];
  51. this.HelpDeskService.setPassword(this.personUserKey, false, chosenPassword)
  52. .then((result: ISuccessResponse) => {
  53. // Send the password and success message to the parent element via the close() method.
  54. let data: IChangePasswordSuccess = { password: chosenPassword, successMessage: result.successMessage };
  55. this.IasDialogService.close(data);
  56. });
  57. }
  58. passwordSuggestionFactory(index: number): any {
  59. return () => {
  60. return this.HelpDeskService.getRandomPassword(this.personUserKey).then(
  61. (result: IRandomPasswordResponse) => {
  62. this.passwordSuggestions[index] = result.password;
  63. }
  64. );
  65. };
  66. }
  67. populatePasswordSuggestions() {
  68. this.fetchingRandoms = true;
  69. let ordering = this.generateRandomMapping();
  70. let promiseChain: IPromise<any> = this.$q.when();
  71. ordering.forEach((index: number) => {
  72. promiseChain = promiseChain.then(this.passwordSuggestionFactory(index));
  73. });
  74. promiseChain.then(() => {
  75. this.fetchingRandoms = false;
  76. });
  77. }
  78. }