1 import { Component } from '@angular/core';
2 import { Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
8 import { UserService } from '../../../shared/api/user.service';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { Icons } from '../../../shared/enum/icons.enum';
11 import { NotificationType } from '../../../shared/enum/notification-type.enum';
12 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
13 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
14 import { CdValidators } from '../../../shared/forms/cd-validators';
15 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
16 import { NotificationService } from '../../../shared/services/notification.service';
17 import { PasswordPolicyService } from '../../../shared/services/password-policy.service';
20 selector: 'cd-user-password-form',
21 templateUrl: './user-password-form.component.html',
22 styleUrls: ['./user-password-form.component.scss']
24 export class UserPasswordFormComponent {
25 userForm: CdFormGroup;
28 passwordPolicyHelpText: string;
29 passwordStrengthLevelClass: string;
30 passwordValuation: string;
35 public actionLabels: ActionLabelsI18n,
36 private notificationService: NotificationService,
37 private userService: UserService,
38 private authStorageService: AuthStorageService,
39 private formBuilder: CdFormBuilder,
40 private router: Router,
41 private passwordPolicyService: PasswordPolicyService
43 this.action = this.actionLabels.CHANGE;
44 this.resource = this.i18n('password');
49 this.passwordPolicyHelpText = this.passwordPolicyService.getHelpText();
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(
105 this.notificationService.show(
106 NotificationType.success,
107 this.i18n('Updated user password"')
109 // Theoretically it is not necessary to navigate to '/logout' because
110 // the auth token gets invalid after changing the password in the
111 // backend, thus the user would be automatically logged out after the
112 // next periodically API request is executed.
113 this.router.navigate(['/logout']);
116 this.userForm.setErrors({ cdSubmitButton: true });