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 = '';
29 passwordStrengthLevelClass: string;
30 passwordValuation: string;
35 public actionLabels: ActionLabelsI18n,
36 public notificationService: NotificationService,
37 public userService: UserService,
38 public authStorageService: AuthStorageService,
39 public formBuilder: CdFormBuilder,
40 public router: Router,
41 public passwordPolicyService: PasswordPolicyService
43 this.action = this.actionLabels.CHANGE;
44 this.resource = this.i18n('password');
49 this.passwordPolicyService.getHelpText().subscribe((helpText: string) => {
50 this.passwordPolicyHelpText = helpText;
52 this.userForm = this.formBuilder.group(
58 CdValidators.custom('notmatch', () => {
61 this.userForm.getValue('newpassword') === this.userForm.getValue('oldpassword')
70 CdValidators.custom('notmatch', () => {
73 this.userForm.getValue('oldpassword') === this.userForm.getValue('newpassword')
78 CdValidators.passwordPolicy(
80 () => this.authStorageService.getUsername(),
81 (_valid: boolean, credits: number, valuation: string) => {
82 this.passwordStrengthLevelClass = this.passwordPolicyService.mapCreditsToCssClass(
85 this.passwordValuation = _.defaultTo(valuation, '');
90 confirmnewpassword: [null, [Validators.required]]
93 validators: [CdValidators.match('newpassword', 'confirmnewpassword')]
99 if (this.userForm.pristine) {
102 const username = this.authStorageService.getUsername();
103 const oldPassword = this.userForm.getValue('oldpassword');
104 const newPassword = this.userForm.getValue('newpassword');
105 this.userService.changePassword(username, oldPassword, newPassword).subscribe(
106 () => this.onPasswordChange(),
108 this.userForm.setErrors({ cdSubmitButton: true });
114 * The function that is called after the password has been changed.
115 * Override this in derived classes to change the behaviour.
118 this.notificationService.show(NotificationType.success, this.i18n('Updated user password"'));
119 // Theoretically it is not necessary to navigate to '/logout' because
120 // the auth token gets invalid after changing the password in the
121 // backend, thus the user would be automatically logged out after the
122 // next periodically API request is executed.
123 this.router.navigate(['/logout']);