autogen-change-password.controller.ts 3.2 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-2021 The PWM Project
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. import {IHelpDeskService, IRandomPasswordResponse, ISuccessResponse} from '../../services/helpdesk.service';
  21. import {IPromise, IQService} from 'angular';
  22. import {IChangePasswordSuccess} from './success-change-password.controller';
  23. const RANDOM_MAPPING_SIZE = 20;
  24. require('./autogen-change-password.component.scss');
  25. export default class AutogenChangePasswordController {
  26. fetchingRandoms: boolean;
  27. passwordSuggestions: string[];
  28. static $inject = [ '$q', 'HelpDeskService', 'IasDialogService', 'personUserKey' ];
  29. constructor(private $q: IQService,
  30. private HelpDeskService: IHelpDeskService,
  31. private IasDialogService: any,
  32. private personUserKey: string) {
  33. this.passwordSuggestions = [];
  34. for (let i = 0; i < 20; i++) {
  35. this.passwordSuggestions.push('');
  36. }
  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. }