-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathshare-access.component.ts
More file actions
438 lines (412 loc) · 14.7 KB
/
share-access.component.ts
File metadata and controls
438 lines (412 loc) · 14.7 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Component, EventEmitter, inject, OnDestroy, OnInit, Output } from "@angular/core";
import { FormBuilder, FormControl, FormGroup, Validators } from "@angular/forms";
import { ShareAccessService } from "../../../service/user/share-access/share-access.service";
import { Privilege, ShareAccess } from "../../../type/share-access.interface";
import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy";
import { UserService } from "../../../../common/service/user/user.service";
import { GmailService } from "../../../../common/service/gmail/gmail.service";
import { NZ_MODAL_DATA, NzModalRef, NzModalService } from "ng-zorro-antd/modal";
import { NotificationService } from "../../../../common/service/notification/notification.service";
import { HttpErrorResponse } from "@angular/common/http";
import { NzMessageService } from "ng-zorro-antd/message";
import { DatasetService } from "../../../service/user/dataset/dataset.service";
import { WorkflowPersistService } from "src/app/common/service/workflow-persist/workflow-persist.service";
import { WorkflowActionService } from "src/app/workspace/service/workflow-graph/model/workflow-action.service";
@UntilDestroy()
@Component({
selector: "texera-share-access",
templateUrl: "share-access.component.html",
styleUrls: ["./share-access.component.scss"],
})
export class ShareAccessComponent implements OnInit, OnDestroy {
readonly nzModalData = inject(NZ_MODAL_DATA);
readonly type: string = this.nzModalData.type;
readonly id: number = this.nzModalData.id;
readonly allOwners: string[] = this.nzModalData.allOwners;
readonly inWorkspace: boolean = this.nzModalData.inWorkspace;
public validateForm: FormGroup;
public accessList: ReadonlyArray<ShareAccess> = [];
public owner: string = "";
public filteredOwners: Array<string> = [];
public ownerSearchValue?: string;
public emailTags: string[] = [];
currentEmail: string | undefined = "";
isPublic: boolean | null = null;
private shouldRefresh = false;
@Output() refresh = new EventEmitter<void>();
constructor(
private accessService: ShareAccessService,
private formBuilder: FormBuilder,
private userService: UserService,
private gmailService: GmailService,
private notificationService: NotificationService,
private message: NzMessageService,
private modalService: NzModalService,
private workflowPersistService: WorkflowPersistService,
private datasetService: DatasetService,
private workflowActionService: WorkflowActionService,
private modalRef: NzModalRef
) {
this.validateForm = this.formBuilder.group({
email: [null, Validators.email],
accessLevel: ["WRITE"],
});
this.currentEmail = this.userService.getCurrentUser()?.email;
}
get hasWriteAccess(): boolean {
if (!this.currentEmail) {
return false;
}
if (this.currentEmail === this.owner) {
return true;
}
const currentUserAccess = this.accessList.find(entry => entry.email === this.currentEmail);
return currentUserAccess?.privilege === Privilege.WRITE;
}
ngOnInit(): void {
this.accessService
.getAccessList(this.type, this.id)
.pipe(untilDestroyed(this))
.subscribe(access => (this.accessList = access));
this.accessService
.getOwner(this.type, this.id)
.pipe(untilDestroyed(this))
.subscribe(name => {
this.owner = name;
});
if (this.type === "workflow") {
this.workflowPersistService
.getWorkflowIsPublished(this.id)
.pipe(untilDestroyed(this))
.subscribe(dashboardWorkflow => {
this.isPublic = dashboardWorkflow === "Public";
});
} else if (this.type === "dataset") {
this.datasetService
.getDataset(this.id)
.pipe(untilDestroyed(this))
.subscribe(dashboardDataset => {
this.isPublic = dashboardDataset.dataset.isPublic;
});
}
}
ngOnDestroy(): void {
if (this.shouldRefresh) {
this.refresh.emit();
}
}
public handleInputConfirm(event?: Event): void {
if (event) {
event.preventDefault();
}
const emailInput = this.validateForm.get("email")?.value;
if (emailInput) {
const emailArray: string[] = emailInput.split(/[\s,;]+/);
emailArray.forEach(email => {
if (email) {
const emailControl = new FormControl(email, Validators.email);
if (!emailControl.errors && !this.emailTags.includes(email)) {
this.emailTags.push(email);
} else if (this.emailTags.includes(email)) {
this.message.error(`${email} is already in the tags`);
} else {
this.message.error(`${email} is not a valid email`);
}
}
});
}
this.validateForm.get("email")?.reset();
}
public removeEmailTag(email: string): void {
this.emailTags = this.emailTags.filter(tag => tag !== email);
}
public grantAccess(): void {
this.handleInputConfirm();
if (this.emailTags.length > 0) {
this.emailTags.forEach(email => {
let message = `${this.userService.getCurrentUser()?.email} shared a ${this.type} with you`;
if (this.type !== "computing-unit")
message += `, access the ${this.type} at ${location.origin}/dashboard/user/workflow/${this.id}`;
this.accessService
.grantAccess(this.type, this.id, email, this.validateForm.value.accessLevel)
.pipe(untilDestroyed(this))
.subscribe({
next: () => {
this.notificationService.success(this.type + " shared with " + email + " successfully.");
this.gmailService.sendEmail(
"Texera: " + this.userService.getCurrentUser()?.email + " shared a " + this.type + " with you",
message,
email
);
this.ngOnInit();
},
error: (error: unknown) => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error(error.error.message);
}
},
});
});
this.emailTags = [];
}
}
public onPaste(event: ClipboardEvent): void {
event.preventDefault();
const pasteData = event.clipboardData?.getData("text");
if (pasteData) {
const currentEmailValue = this.validateForm.get("email")?.value || "";
// concaste new emails and old emails
const newValue = currentEmailValue + pasteData;
this.validateForm.get("email")?.setValue(newValue);
this.handleInputConfirm();
}
}
public onChange(value: string): void {
if (value === null || value === undefined) {
this.filteredOwners = [];
} else {
this.filteredOwners = this.allOwners.filter(owner => owner.toLowerCase().indexOf(value.toLowerCase()) !== -1);
}
}
public verifyRevokeAccess(userToRemove: string): void {
const isRevokingOwnAccess = userToRemove === this.userService.getCurrentUser()?.email;
const modalTitle = isRevokingOwnAccess ? "Revoke Your Access" : "Revoke Access";
const modalContent = isRevokingOwnAccess
? `Are you sure you want to revoke your own access to this ${this.type}? You will no longer be able to view or edit it.`
: `Are you sure you want to revoke ${userToRemove}'s access to this ${this.type}?`;
const modal: NzModalRef = this.modalService.create({
nzTitle: modalTitle,
nzContent: modalContent,
nzFooter: [
{
label: "Cancel",
onClick: () => modal.close(),
},
{
label: "Revoke",
type: "primary",
danger: true,
onClick: () => {
this.revokeAccess(userToRemove);
modal.close();
},
},
],
});
}
private revokeAccess(userToRemove: string): void {
this.accessService
.revokeAccess(this.type, this.id, userToRemove)
.pipe(untilDestroyed(this))
.subscribe({
next: () => {
if (userToRemove == this.userService.getCurrentUser()?.email) {
this.shouldRefresh = true;
this.modalRef.close({ userRevokedOwnAccess: true });
}
this.ngOnInit();
},
error: (error: unknown) => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error(error.error.message);
}
},
});
}
public changeAccessLevel(email: string, newPrivilege: string): void {
const isOwnAccess = email === this.currentEmail;
const currentUserAccess = this.accessList.find(entry => entry.email === email);
const isDowngrade = currentUserAccess?.privilege === Privilege.WRITE && newPrivilege === "READ";
if (isOwnAccess && isDowngrade) {
const modal: NzModalRef = this.modalService.create({
nzTitle: "Downgrade Your Access",
nzContent: `Are you sure you want to change your own access to READ? You will no longer be able to edit this ${this.type} or manage access.`,
nzFooter: [
{
label: "Cancel",
onClick: () => {
modal.close();
this.ngOnInit();
},
},
{
label: "Confirm",
type: "primary",
danger: true,
onClick: () => {
this.applyAccessLevelChange(email, newPrivilege);
modal.close();
},
},
],
});
} else {
this.applyAccessLevelChange(email, newPrivilege);
}
}
private applyAccessLevelChange(email: string, newPrivilege: string): void {
this.accessService
.grantAccess(this.type, this.id, email, newPrivilege)
.pipe(untilDestroyed(this))
.subscribe({
next: () => {
this.notificationService.success(`Access level for ${email} changed to ${newPrivilege}.`);
this.ngOnInit();
},
error: (error: unknown) => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error(error.error.message);
}
this.ngOnInit();
},
});
}
public verifyPublish(): void {
if (!this.isPublic) {
const modal: NzModalRef = this.modalService.create({
nzTitle: "Notice",
nzContent: `Publishing your ${this.type} would grant all Texera users read access to your ${this.type} along with the right to clone your work.`,
nzFooter: [
{
label: "Cancel",
onClick: () => modal.close(),
},
{
label: "Publish",
type: "primary",
onClick: () => {
if (this.type === "workflow") {
this.publishWorkflow();
if (this.inWorkspace) {
this.workflowActionService.setWorkflowIsPublished(1);
}
} else if (this.type === "dataset") {
this.publishDataset();
}
modal.close();
},
},
],
});
}
}
public verifyUnpublish(): void {
if (this.isPublic) {
const modal: NzModalRef = this.modalService.create({
nzTitle: "Notice",
nzContent: `All other users would lose access to your ${this.type} if you unpublish it.`,
nzFooter: [
{
label: "Cancel",
onClick: () => modal.close(),
},
{
label: "Unpublish",
type: "primary",
onClick: () => {
if (this.type === "workflow") {
this.unpublishWorkflow();
if (this.inWorkspace) {
this.workflowActionService.setWorkflowIsPublished(0);
}
} else if (this.type === "dataset") {
this.unpublishDataset();
}
modal.close();
},
},
],
});
}
}
public publishWorkflow(): void {
if (!this.isPublic) {
this.workflowPersistService
.updateWorkflowIsPublished(this.id, true)
.pipe(untilDestroyed(this))
.subscribe({
next: () => {
this.isPublic = true;
this.notificationService.success("Workflow published successfully");
},
error: (error: unknown) => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error(error.error.message);
}
},
});
}
}
public unpublishWorkflow(): void {
if (this.isPublic) {
this.workflowPersistService
.updateWorkflowIsPublished(this.id, false)
.pipe(untilDestroyed(this))
.subscribe({
next: () => {
this.isPublic = false;
this.notificationService.success("Workflow unpublished successfully");
},
error: (error: unknown) => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error(error.error.message);
}
},
});
}
}
public publishDataset(): void {
if (!this.isPublic) {
this.datasetService
.updateDatasetPublicity(this.id)
.pipe(untilDestroyed(this))
.subscribe({
next: (res: Response) => {
this.isPublic = true;
this.notificationService.success("Dataset published successfully");
},
error: (error: unknown) => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error(error.error.message);
}
},
});
}
}
public unpublishDataset(): void {
if (this.isPublic) {
this.datasetService
.updateDatasetPublicity(this.id)
.pipe(untilDestroyed(this))
.subscribe({
next: (res: Response) => {
this.isPublic = false;
this.notificationService.success("Dataset unpublished successfully");
},
error: (error: unknown) => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error(error.error.message);
}
},
});
}
}
}