/
service-account-dialog.component.ts
112 lines (95 loc) · 3.39 KB
/
service-account-dialog.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
import { DialogRef, DIALOG_DATA } from "@angular/cdk/dialog";
import { Component, Inject } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { BitValidators } from "@bitwarden/components";
import { ServiceAccountView } from "../../models/view/service-account.view";
import { ServiceAccountService } from "../service-account.service";
export enum OperationType {
Add,
Edit,
}
export interface ServiceAccountOperation {
organizationId: string;
serviceAccountId?: string;
operation: OperationType;
organizationEnabled: boolean;
}
@Component({
templateUrl: "./service-account-dialog.component.html",
})
export class ServiceAccountDialogComponent {
protected formGroup = new FormGroup(
{
name: new FormControl("", {
validators: [Validators.required, Validators.maxLength(500), BitValidators.trimValidator],
updateOn: "submit",
}),
},
{},
);
protected loading = false;
constructor(
public dialogRef: DialogRef,
@Inject(DIALOG_DATA) private data: ServiceAccountOperation,
private serviceAccountService: ServiceAccountService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
) {}
async ngOnInit() {
if (this.data.operation == OperationType.Edit) {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.loadData();
}
}
async loadData() {
this.loading = true;
const serviceAccount: ServiceAccountView =
await this.serviceAccountService.getByServiceAccountId(
this.data.serviceAccountId,
this.data.organizationId,
);
this.formGroup.patchValue({ name: serviceAccount.name });
this.loading = false;
}
submit = async () => {
if (!this.data.organizationEnabled) {
this.platformUtilsService.showToast(
"error",
null,
this.i18nService.t("machineAccountsCannotCreate"),
);
return;
}
this.formGroup.markAllAsTouched();
if (this.formGroup.invalid) {
return;
}
const serviceAccountView = this.getServiceAccountView();
let serviceAccountMessage: string;
if (this.data.operation == OperationType.Add) {
await this.serviceAccountService.create(this.data.organizationId, serviceAccountView);
serviceAccountMessage = this.i18nService.t("serviceAccountCreated");
} else {
await this.serviceAccountService.update(
this.data.serviceAccountId,
this.data.organizationId,
serviceAccountView,
);
serviceAccountMessage = this.i18nService.t("serviceAccountUpdated");
}
this.platformUtilsService.showToast("success", null, serviceAccountMessage);
this.dialogRef.close();
};
private getServiceAccountView() {
const serviceAccountView = new ServiceAccountView();
serviceAccountView.organizationId = this.data.organizationId;
serviceAccountView.name = this.formGroup.value.name;
return serviceAccountView;
}
get title() {
return this.data.operation === OperationType.Add ? "newServiceAccount" : "editServiceAccount";
}
}