password-suggestions.controller.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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} from '../services/helpdesk.service';
  23. import {IPromise, IQService} from 'angular';
  24. let RANDOM_MAPPING_LENGTH = 20;
  25. require('helpdesk/password-suggestions-dialog.scss');
  26. export default class PasswordSuggestionsDialogController {
  27. fetchingRandoms: boolean;
  28. passwordSuggestions: string[];
  29. static $inject = [ '$q', 'HelpDeskService', 'personUserKey' ];
  30. constructor(private $q: IQService, private HelpDeskService: IHelpDeskService, private personUserKey: string) {
  31. this.passwordSuggestions = Array(20).fill('');
  32. this.fetchRandoms();
  33. }
  34. onChoosePassword(index: number) {
  35. let password = this.passwordSuggestions[index];
  36. // change password
  37. }
  38. onMoreRandomsButtonClick() {
  39. this.fetchRandoms();
  40. }
  41. fetchRandoms() {
  42. this.fetchingRandoms = true;
  43. let ordering = this.generateRandomMapping();
  44. let promiseChain: IPromise<any> = this.$q.when();
  45. ordering.forEach((index: number) => {
  46. promiseChain = promiseChain.then(this.passwordSuggestionFactory(index));
  47. });
  48. promiseChain.then(() => {
  49. this.fetchingRandoms = false;
  50. });
  51. }
  52. generateRandomMapping(): number[] {
  53. let map: number[] = [];
  54. for (let i = 0; i < RANDOM_MAPPING_LENGTH; i++) {
  55. map.push(i);
  56. }
  57. let randomComparatorFunction = () => 0.5 - Math.random();
  58. map.sort(randomComparatorFunction);
  59. map.sort(randomComparatorFunction);
  60. return map;
  61. }
  62. passwordSuggestionFactory(index: number): any {
  63. return () => {
  64. return this.HelpDeskService.getRandomPassword(this.personUserKey).then(
  65. (result: IRandomPasswordResponse) => {
  66. this.passwordSuggestions[index] = result.password;
  67. }
  68. );
  69. };
  70. }
  71. }