-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
secret-dialog.component.ts
270 lines (223 loc) · 8.79 KB
/
secret-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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { DialogRef, DIALOG_DATA } from "@angular/cdk/dialog";
import { Component, Inject, OnInit } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";
import { lastValueFrom, Subject, takeUntil } from "rxjs";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { DialogService, BitValidators } from "@bitwarden/components";
import { ProjectListView } from "../../models/view/project-list.view";
import { ProjectView } from "../../models/view/project.view";
import { SecretListView } from "../../models/view/secret-list.view";
import { SecretProjectView } from "../../models/view/secret-project.view";
import { SecretView } from "../../models/view/secret.view";
import { ProjectService } from "../../projects/project.service";
import { SecretService } from "../secret.service";
import { SecretDeleteDialogComponent, SecretDeleteOperation } from "./secret-delete.component";
export enum OperationType {
Add,
Edit,
}
export interface SecretOperation {
organizationId: string;
operation: OperationType;
projectId?: string;
secretId?: string;
organizationEnabled: boolean;
}
@Component({
templateUrl: "./secret-dialog.component.html",
})
export class SecretDialogComponent implements OnInit {
protected formGroup = new FormGroup({
name: new FormControl("", {
validators: [Validators.required, Validators.maxLength(500), BitValidators.trimValidator],
updateOn: "submit",
}),
value: new FormControl("", [Validators.required, Validators.maxLength(25000)]),
notes: new FormControl("", {
validators: [Validators.maxLength(7000), BitValidators.trimValidator],
updateOn: "submit",
}),
project: new FormControl("", [Validators.required]),
newProjectName: new FormControl("", {
validators: [Validators.maxLength(500), BitValidators.trimValidator],
updateOn: "submit",
}),
});
private destroy$ = new Subject<void>();
private loading = true;
projects: ProjectListView[];
addNewProject = false;
newProjectGuid = Utils.newGuid();
constructor(
public dialogRef: DialogRef,
@Inject(DIALOG_DATA) private data: SecretOperation,
private secretService: SecretService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private projectService: ProjectService,
private dialogService: DialogService,
private organizationService: OrganizationService,
) {}
async ngOnInit() {
if (this.data.operation === OperationType.Edit && this.data.secretId) {
await this.loadData();
} else if (this.data.operation !== OperationType.Add) {
this.dialogRef.close();
throw new Error(`The secret dialog was not called with the appropriate operation values.`);
} else if (this.data.operation == OperationType.Add) {
await this.loadProjects(true);
if (this.data.projectId == null || this.data.projectId == "") {
this.addNewProjectOptionToProjectsDropDown();
}
}
if (this.data.projectId) {
this.formGroup.get("project").setValue(this.data.projectId);
}
if ((await this.organizationService.get(this.data.organizationId))?.isAdmin) {
this.formGroup.get("project").removeValidators(Validators.required);
this.formGroup.get("project").updateValueAndValidity();
}
}
async loadData() {
this.formGroup.disable();
const secret: SecretView = await this.secretService.getBySecretId(this.data.secretId);
await this.loadProjects(secret.write);
this.formGroup.setValue({
name: secret.name,
value: secret.value,
notes: secret.note,
project: secret.projects[0]?.id ?? "",
newProjectName: "",
});
this.loading = false;
if (secret.write) {
this.formGroup.enable();
}
}
async loadProjects(filterByPermission: boolean) {
this.projects = await this.projectService
.getProjects(this.data.organizationId)
.then((projects) => projects.sort((a, b) => a.name.localeCompare(b.name)));
if (filterByPermission) {
this.projects = this.projects.filter((p) => p.write);
}
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
private addNewProjectOptionToProjectsDropDown() {
this.formGroup
.get("project")
.valueChanges.pipe(takeUntil(this.destroy$))
.subscribe((val: string) => {
this.dropDownSelected(val);
});
const addNewProject = new ProjectListView();
addNewProject.name = this.i18nService.t("newProject");
addNewProject.id = this.newProjectGuid;
this.projects.unshift(addNewProject);
}
private dropDownSelected(val: string) {
this.addNewProject = val == this.newProjectGuid;
if (this.addNewProject) {
this.formGroup.get("newProjectName").addValidators([Validators.required]);
} else {
this.formGroup.get("newProjectName").clearValidators();
}
this.formGroup.get("newProjectName").updateValueAndValidity();
}
get title() {
return this.data.operation === OperationType.Add ? "newSecret" : "editSecret";
}
get showSpinner() {
return this.data.operation === OperationType.Edit && this.loading;
}
submit = async () => {
if (!this.data.organizationEnabled) {
this.platformUtilsService.showToast("error", null, this.i18nService.t("secretsCannotCreate"));
return;
}
this.formGroup.markAllAsTouched();
if (this.formGroup.invalid) {
return;
}
const secretView = this.getSecretView();
if (this.addNewProject) {
const newProject = await this.createProject(this.getNewProjectView());
secretView.projects = [newProject];
}
if (this.data.operation === OperationType.Add) {
await this.createSecret(secretView);
} else {
secretView.id = this.data.secretId;
await this.updateSecret(secretView);
}
this.dialogRef.close();
};
get deleteButtonIsVisible(): boolean {
return this.data.operation === OperationType.Edit;
}
private async createProject(projectView: ProjectView) {
return await this.projectService.create(this.data.organizationId, projectView);
}
protected openDeleteSecretDialog() {
const secretListView: SecretListView[] = this.getSecretListView();
const dialogRef = this.dialogService.open<unknown, SecretDeleteOperation>(
SecretDeleteDialogComponent,
{
data: {
secrets: secretListView,
},
},
);
// If the secret is deleted, chain close this dialog after the delete dialog
// 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
lastValueFrom(dialogRef.closed).then(
(closeData) => closeData !== undefined && this.dialogRef.close(),
);
}
private async createSecret(secretView: SecretView) {
await this.secretService.create(this.data.organizationId, secretView);
this.platformUtilsService.showToast("success", null, this.i18nService.t("secretCreated"));
}
private getNewProjectView() {
const projectView = new ProjectView();
projectView.organizationId = this.data.organizationId;
projectView.name = this.formGroup.value.newProjectName;
return projectView;
}
private async updateSecret(secretView: SecretView) {
await this.secretService.update(this.data.organizationId, secretView);
this.platformUtilsService.showToast("success", null, this.i18nService.t("secretEdited"));
}
private getSecretView() {
const secretView = new SecretView();
secretView.organizationId = this.data.organizationId;
secretView.name = this.formGroup.value.name;
secretView.value = this.formGroup.value.value;
secretView.note = this.formGroup.value.notes;
const project = this.projects.find((p) => p.id == this.formGroup.value.project);
secretView.projects = project != undefined ? [project] : [];
return secretView;
}
private getSecretListView() {
const secretListViews: SecretListView[] = [];
const emptyProjects: SecretProjectView[] = [];
const secretListView = new SecretListView();
if (this.formGroup.value.project) {
secretListView.projects = [this.projects.find((p) => p.id == this.formGroup.value.project)];
} else {
secretListView.projects = emptyProjects;
}
secretListView.organizationId = this.data.organizationId;
secretListView.id = this.data.secretId;
secretListView.name = this.formGroup.value.name;
secretListViews.push(secretListView);
return secretListViews;
}
}