]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
239072c72033b912b81e900afed54a20d124e7d6
[ceph.git] /
1 import { Component } from '@angular/core';
2 import { Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
4
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7
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';
18
19 @Component({
20   selector: 'cd-user-password-form',
21   templateUrl: './user-password-form.component.html',
22   styleUrls: ['./user-password-form.component.scss']
23 })
24 export class UserPasswordFormComponent {
25   userForm: CdFormGroup;
26   action: string;
27   resource: string;
28   passwordPolicyHelpText: string;
29   passwordStrengthLevelClass: string;
30   passwordValuation: string;
31   icons = Icons;
32
33   constructor(
34     private i18n: I18n,
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
42   ) {
43     this.action = this.actionLabels.CHANGE;
44     this.resource = this.i18n('password');
45     this.createForm();
46   }
47
48   createForm() {
49     this.passwordPolicyHelpText = this.passwordPolicyService.getHelpText();
50     this.userForm = this.formBuilder.group(
51       {
52         oldpassword: [
53           null,
54           [
55             Validators.required,
56             CdValidators.custom('notmatch', () => {
57               return (
58                 this.userForm &&
59                 this.userForm.getValue('newpassword') === this.userForm.getValue('oldpassword')
60               );
61             })
62           ]
63         ],
64         newpassword: [
65           null,
66           [
67             Validators.required,
68             CdValidators.custom('notmatch', () => {
69               return (
70                 this.userForm &&
71                 this.userForm.getValue('oldpassword') === this.userForm.getValue('newpassword')
72               );
73             })
74           ],
75           [
76             CdValidators.passwordPolicy(
77               this.userService,
78               () => this.authStorageService.getUsername(),
79               (_valid: boolean, credits: number, valuation: string) => {
80                 this.passwordStrengthLevelClass = this.passwordPolicyService.mapCreditsToCssClass(
81                   credits
82                 );
83                 this.passwordValuation = _.defaultTo(valuation, '');
84               }
85             )
86           ]
87         ],
88         confirmnewpassword: [null, [Validators.required]]
89       },
90       {
91         validators: [CdValidators.match('newpassword', 'confirmnewpassword')]
92       }
93     );
94   }
95
96   onSubmit() {
97     if (this.userForm.pristine) {
98       return;
99     }
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       () => {
105         this.notificationService.show(
106           NotificationType.success,
107           this.i18n('Updated user password"')
108         );
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']);
114       },
115       () => {
116         this.userForm.setErrors({ cdSubmitButton: true });
117       }
118     );
119   }
120 }