]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormControl, Validators } from '@angular/forms'; | |
3 | ||
4 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
5 | import { BsModalRef } from 'ngx-bootstrap/modal'; | |
6 | ||
7 | import { IscsiService } from '../../../shared/api/iscsi.service'; | |
8 | import { NotificationType } from '../../../shared/enum/notification-type.enum'; | |
9 | import { CdFormGroup } from '../../../shared/forms/cd-form-group'; | |
10 | import { CdValidators } from '../../../shared/forms/cd-validators'; | |
81eedcae TL |
11 | import { Permission } from '../../../shared/models/permissions'; |
12 | import { AuthStorageService } from '../../../shared/services/auth-storage.service'; | |
11fdf7f2 TL |
13 | import { NotificationService } from '../../../shared/services/notification.service'; |
14 | ||
15 | @Component({ | |
16 | selector: 'cd-iscsi-target-discovery-modal', | |
17 | templateUrl: './iscsi-target-discovery-modal.component.html', | |
18 | styleUrls: ['./iscsi-target-discovery-modal.component.scss'] | |
19 | }) | |
20 | export class IscsiTargetDiscoveryModalComponent implements OnInit { | |
21 | discoveryForm: CdFormGroup; | |
81eedcae TL |
22 | permission: Permission; |
23 | hasPermission: boolean; | |
11fdf7f2 TL |
24 | |
25 | USER_REGEX = /[\w\.:@_-]{8,64}/; | |
26 | PASSWORD_REGEX = /[\w@\-_\/]{12,16}/; | |
27 | ||
28 | constructor( | |
81eedcae | 29 | private authStorageService: AuthStorageService, |
11fdf7f2 TL |
30 | public bsModalRef: BsModalRef, |
31 | private iscsiService: IscsiService, | |
32 | private notificationService: NotificationService, | |
33 | private i18n: I18n | |
34 | ) { | |
81eedcae TL |
35 | this.permission = this.authStorageService.getPermissions().iscsi; |
36 | } | |
37 | ||
38 | ngOnInit() { | |
39 | this.hasPermission = this.permission.update; | |
40 | this.createForm(); | |
41 | this.iscsiService.getDiscovery().subscribe((auth) => { | |
42 | this.discoveryForm.patchValue(auth); | |
43 | }); | |
44 | } | |
45 | ||
46 | createForm() { | |
11fdf7f2 | 47 | this.discoveryForm = new CdFormGroup({ |
81eedcae TL |
48 | user: new FormControl({ value: '', disabled: !this.hasPermission }), |
49 | password: new FormControl({ value: '', disabled: !this.hasPermission }), | |
50 | mutual_user: new FormControl({ value: '', disabled: !this.hasPermission }), | |
51 | mutual_password: new FormControl({ value: '', disabled: !this.hasPermission }) | |
11fdf7f2 TL |
52 | }); |
53 | ||
54 | CdValidators.validateIf( | |
55 | this.discoveryForm.get('user'), | |
56 | () => | |
57 | this.discoveryForm.getValue('password') || | |
58 | this.discoveryForm.getValue('mutual_user') || | |
59 | this.discoveryForm.getValue('mutual_password'), | |
60 | [Validators.required], | |
61 | [Validators.pattern(this.USER_REGEX)], | |
62 | [ | |
63 | this.discoveryForm.get('password'), | |
64 | this.discoveryForm.get('mutual_user'), | |
65 | this.discoveryForm.get('mutual_password') | |
66 | ] | |
67 | ); | |
68 | ||
69 | CdValidators.validateIf( | |
70 | this.discoveryForm.get('password'), | |
71 | () => | |
72 | this.discoveryForm.getValue('user') || | |
73 | this.discoveryForm.getValue('mutual_user') || | |
74 | this.discoveryForm.getValue('mutual_password'), | |
75 | [Validators.required], | |
76 | [Validators.pattern(this.PASSWORD_REGEX)], | |
77 | [ | |
78 | this.discoveryForm.get('user'), | |
79 | this.discoveryForm.get('mutual_user'), | |
80 | this.discoveryForm.get('mutual_password') | |
81 | ] | |
82 | ); | |
83 | ||
84 | CdValidators.validateIf( | |
85 | this.discoveryForm.get('mutual_user'), | |
86 | () => this.discoveryForm.getValue('mutual_password'), | |
87 | [Validators.required], | |
88 | [Validators.pattern(this.USER_REGEX)], | |
89 | [ | |
90 | this.discoveryForm.get('user'), | |
91 | this.discoveryForm.get('password'), | |
92 | this.discoveryForm.get('mutual_password') | |
93 | ] | |
94 | ); | |
95 | ||
96 | CdValidators.validateIf( | |
97 | this.discoveryForm.get('mutual_password'), | |
98 | () => this.discoveryForm.getValue('mutual_user'), | |
99 | [Validators.required], | |
100 | [Validators.pattern(this.PASSWORD_REGEX)], | |
101 | [ | |
102 | this.discoveryForm.get('user'), | |
103 | this.discoveryForm.get('password'), | |
104 | this.discoveryForm.get('mutual_user') | |
105 | ] | |
106 | ); | |
107 | } | |
108 | ||
11fdf7f2 TL |
109 | submitAction() { |
110 | this.iscsiService.updateDiscovery(this.discoveryForm.value).subscribe( | |
111 | () => { | |
112 | this.notificationService.show( | |
113 | NotificationType.success, | |
114 | this.i18n('Updated discovery authentication') | |
115 | ); | |
116 | this.bsModalRef.hide(); | |
117 | }, | |
118 | () => { | |
92f5a8d4 | 119 | this.discoveryForm.setErrors({ cdSubmitButton: true }); |
11fdf7f2 TL |
120 | } |
121 | ); | |
122 | } | |
123 | } |