1 import { Component } from '@angular/core';
2 import { Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import _ from 'lodash';
7 import { UserService } from '~/app/shared/api/user.service';
8 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
9 import { Icons } from '~/app/shared/enum/icons.enum';
10 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
11 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
12 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
13 import { CdValidators } from '~/app/shared/forms/cd-validators';
14 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
15 import { NotificationService } from '~/app/shared/services/notification.service';
16 import { PasswordPolicyService } from '~/app/shared/services/password-policy.service';
19 selector: 'cd-user-password-form',
20 templateUrl: './user-password-form.component.html',
21 styleUrls: ['./user-password-form.component.scss']
23 export class UserPasswordFormComponent {
24 userForm: CdFormGroup;
27 passwordPolicyHelpText = '';
28 passwordStrengthLevelClass: string;
29 passwordValuation: string;
33 public actionLabels: ActionLabelsI18n,
34 public notificationService: NotificationService,
35 public userService: UserService,
36 public authStorageService: AuthStorageService,
37 public formBuilder: CdFormBuilder,
38 public router: Router,
39 public passwordPolicyService: PasswordPolicyService
41 this.action = this.actionLabels.CHANGE;
42 this.resource = $localize`password`;
47 this.passwordPolicyService.getHelpText().subscribe((helpText: string) => {
48 this.passwordPolicyHelpText = helpText;
50 this.userForm = this.formBuilder.group(
56 CdValidators.custom('notmatch', () => {
59 this.userForm.getValue('newpassword') === this.userForm.getValue('oldpassword')
68 CdValidators.custom('notmatch', () => {
71 this.userForm.getValue('oldpassword') === this.userForm.getValue('newpassword')
76 CdValidators.passwordPolicy(
78 () => this.authStorageService.getUsername(),
79 (_valid: boolean, credits: number, valuation: string) => {
80 this.passwordStrengthLevelClass = this.passwordPolicyService.mapCreditsToCssClass(
83 this.passwordValuation = _.defaultTo(valuation, '');
88 confirmnewpassword: [null, [Validators.required]]
91 validators: [CdValidators.match('newpassword', 'confirmnewpassword')]
97 if (this.userForm.pristine) {
100 const username = this.authStorageService.getUsername();
101 const oldPassword = this.userForm.getValue('oldpassword');
102 const newPassword = this.userForm.getValue('newpassword');
103 this.userService.changePassword(username, oldPassword, newPassword).subscribe(
104 () => this.onPasswordChange(),
106 this.userForm.setErrors({ cdSubmitButton: true });
112 * The function that is called after the password has been changed.
113 * Override this in derived classes to change the behaviour.
116 this.notificationService.show(NotificationType.success, $localize`Updated user password"`);
117 this.router.navigate(['/login']);