Newer
Older
import {Component, Inject} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
import {User} from "../../../security/user.model";
import {GlobalLookups} from "../../global-lookups";
import {UserDetailsService} from "../../../user/user-details-dialog/user-details.service";
import {AccessTokenRo} from "./access-token-ro.model";
import {SecurityService} from "../../../security/security.service";
import {SmpConstants} from "../../../smp.constants";
import {SearchTableEntityStatus} from "../../search-table/search-table-entity-status.model";
@Component({
selector: 'smp-access-token-generation-dialog',
templateUrl: './access-token-generation-dialog.component.html',
styleUrls: ['./access-token-generation-dialog.component.css']
})
export class AccessTokenGenerationDialogComponent {
dateTimeFormat: string = SmpConstants.DATE_TIME_FORMAT;

Joze RIHTARSIC
committed
formTitle = "Access token generation dialog";
dialogForm: FormGroup;
hideCurrPwdFiled: boolean = true;
hideNewPwdFiled: boolean = true;
hideConfPwdFiled: boolean = true;
tokenChanged: boolean = false;
adminUser: boolean = false;
current: User;
message: string;
messageType: string = "alert-error";
constructor(
public dialogRef: MatDialogRef<AccessTokenGenerationDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private lookups: GlobalLookups,
private userDetailsService: UserDetailsService,
public securityService: SecurityService,
private fb: FormBuilder
) {
dialogRef.disableClose = true;//disable default close operation
this.current = {...data.user}
this.adminUser = data.adminUser
this.dialogForm = fb.group({
'email': new FormControl({value: null, readonly: true}, null),
'username': new FormControl({value: null, readonly: true}, null),
'accessTokenId': new FormControl({value: null, readonly: true}, null),
'accessTokenExpireOn': new FormControl({value: null, readonly: true}, null),
'current-password': new FormControl({value: null, readonly: false}, this.securityService.getCurrentUser().casAuthenticated?null:[Validators.required]),
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
});
this.dialogForm.controls['email'].setValue(this.isEmptyEmailAddress ? "Empty email address!" : this.current.emailAddress);
this.dialogForm.controls['username'].setValue(this.current.username);
this.dialogForm.controls['accessTokenId'].setValue(this.current.accessTokenId);
this.dialogForm.controls['accessTokenExpireOn'].setValue(this.current.accessTokenExpireOn);
this.dialogForm.controls['current-password'].setValue('');
this.tokenChanged = false;
}
public passwordError = (controlName: string, errorName: string) => {
return this.dialogForm.controls[controlName].hasError(errorName);
}
get isEmptyEmailAddress() {
return !this.current.emailAddress;
}
get getPasswordTitle(): string{
return this.adminUser?"Admin password for user ["+this.securityService.getCurrentUser().username+"]":"Current password";
}
regenerateAccessToken() {
this.clearAlert();
if (this.adminUser) {
// update password
this.userDetailsService.regenerateAccessTokenAdmin(this.securityService.getCurrentUser().userId,
this.dialogForm.controls['current-password'].value,
this.current.userId
).subscribe((response: AccessTokenRo) => {
this.showSuccessMessage("Token with id: " + response.identifier + " and value: " + response.value + " was generated!")
this.current.accessTokenId = response.identifier;
this.current.accessTokenExpireOn = response.expireOn;
// set to current form
this.dialogForm.controls['accessTokenId'].setValue(this.current.accessTokenId);
this.dialogForm.controls['accessTokenExpireOn'].setValue(this.current.accessTokenExpireOn);
this.tokenChanged = true;
},
(err) => {
this.showErrorMessage(err.error.errorDescription);
}
);
} else {
// update access token for currently logged-in user
this.userDetailsService.regenerateAccessToken(this.current.userId,
this.dialogForm.controls['current-password'].value).subscribe((response: AccessTokenRo) => {
this.showSuccessMessage("Token with id: " + response.identifier + " and value: " + response.value + " was generated!")
this.current.accessTokenId = response.identifier;
this.current.accessTokenExpireOn = response.expireOn;
// set to current form
this.dialogForm.controls['accessTokenId'].setValue(this.current.accessTokenId);
this.dialogForm.controls['accessTokenExpireOn'].setValue(this.current.accessTokenExpireOn);
// save new values
const user = {...this.current, status: SearchTableEntityStatus.UPDATED};
this.securityService.updateUserDetails(user);
this.tokenChanged = true;
},
(err) => {
this.showErrorMessage(err.error.errorDescription);
}
);
}
}
showSuccessMessage(value: string) {
this.message = value;
this.messageType = "success";
}
showErrorMessage(value: string) {
this.message = value;
this.messageType = "error";
}
clearAlert() {
this.message = null;
this.messageType = null;
}
public getCurrent() {
if (this.tokenChanged) {
return this.current;
}
return null;
}
closeDialog() {
this.dialogRef.close(this.getCurrent())
}
}