1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 15:23:33 +00:00

disable edit send if policy requires

This commit is contained in:
jaasen-livefront
2024-09-20 09:38:16 -07:00
parent 992964fc08
commit 8624ac64fd
6 changed files with 166 additions and 126 deletions

View File

@@ -1,8 +1,8 @@
import { CommonModule } from "@angular/common";
import { Component, Input, OnInit } from "@angular/core";
import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormBuilder, ReactiveFormsModule } from "@angular/forms";
import { map } from "rxjs";
import { map, Subject, takeUntil } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
@@ -38,7 +38,9 @@ import { SendFormContainer } from "../../send-form-container";
CommonModule,
],
})
export class SendOptionsComponent implements OnInit {
export class SendOptionsComponent implements OnInit, OnDestroy {
private destroy$ = new Subject<void>();
@Input({ required: true })
config: SendFormConfig;
@Input()
@@ -97,6 +99,7 @@ export class SendOptionsComponent implements OnInit {
});
});
}
ngOnInit() {
if (this.sendFormContainer.originalSendView) {
this.sendOptionsForm.patchValue({
@@ -107,5 +110,18 @@ export class SendOptionsComponent implements OnInit {
notes: this.sendFormContainer.originalSendView.notes,
});
}
this.policyService
.policyAppliesToActiveUser$(PolicyType.DisableSend)
.pipe(takeUntil(this.destroy$))
.subscribe((policyAppliesToActiveUser) => {
if (policyAppliesToActiveUser) {
this.sendOptionsForm.disable();
}
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}

View File

@@ -1,107 +0,0 @@
import { DatePipe } from "@angular/common";
import { Component, Input, OnInit } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormBuilder, FormControl, Validators } from "@angular/forms";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { SendView } from "@bitwarden/common/tools/send/models/view/send.view";
import { SendFormConfig } from "../../abstractions/send-form-config.service";
import { SendFormContainer } from "../../send-form-container";
// Value = hours
export enum DatePreset {
OneHour = 1,
OneDay = 24,
TwoDays = 48,
ThreeDays = 72,
SevenDays = 168,
FourteenDays = 336,
ThirtyDays = 720,
}
export interface DatePresetSelectOption {
name: string;
value: DatePreset | string;
}
@Component({
selector: "base-send-details-behavior",
template: "",
})
export class BaseSendDetailsComponent implements OnInit {
@Input() config: SendFormConfig;
@Input() originalSendView?: SendView;
customDeletionDateOption: DatePresetSelectOption | null = null;
datePresetOptions: DatePresetSelectOption[] = [];
sendDetailsForm = this.formBuilder.group({
name: new FormControl("", Validators.required),
selectedDeletionDatePreset: new FormControl(DatePreset.SevenDays || "", Validators.required),
});
constructor(
protected sendFormContainer: SendFormContainer,
protected formBuilder: FormBuilder,
protected i18nService: I18nService,
protected datePipe: DatePipe,
) {
this.sendDetailsForm.valueChanges.pipe(takeUntilDestroyed()).subscribe((value) => {
this.sendFormContainer.patchSend((send) => {
return Object.assign(send, {
name: value.name,
deletionDate: new Date(this.formattedDeletionDate),
expirationDate: new Date(this.formattedDeletionDate),
} as SendView);
});
});
this.sendFormContainer.registerChildForm("sendDetailsForm", this.sendDetailsForm);
}
async ngOnInit() {
this.setupDeletionDatePresets();
if (this.originalSendView) {
this.sendDetailsForm.patchValue({
name: this.originalSendView.name,
selectedDeletionDatePreset: this.originalSendView.deletionDate.toString(),
});
if (this.originalSendView.deletionDate) {
this.customDeletionDateOption = {
name: this.datePipe.transform(this.originalSendView.deletionDate, "MM/dd/yyyy, hh:mm a"),
value: this.originalSendView.deletionDate.toString(),
};
this.datePresetOptions.unshift(this.customDeletionDateOption);
}
}
}
setupDeletionDatePresets() {
const defaultSelections: DatePresetSelectOption[] = [
{ name: this.i18nService.t("oneHour"), value: DatePreset.OneHour },
{ name: this.i18nService.t("oneDay"), value: DatePreset.OneDay },
{ name: this.i18nService.t("days", "2"), value: DatePreset.TwoDays },
{ name: this.i18nService.t("days", "3"), value: DatePreset.ThreeDays },
{ name: this.i18nService.t("days", "7"), value: DatePreset.SevenDays },
{ name: this.i18nService.t("days", "14"), value: DatePreset.FourteenDays },
{ name: this.i18nService.t("days", "30"), value: DatePreset.ThirtyDays },
];
this.datePresetOptions = defaultSelections;
}
get formattedDeletionDate(): string {
const now = new Date();
const selectedValue = this.sendDetailsForm.controls.selectedDeletionDatePreset.value;
if (typeof selectedValue === "string") {
return selectedValue;
}
const milliseconds = now.setTime(now.getTime() + (selectedValue as number) * 60 * 60 * 1000);
return new Date(milliseconds).toString();
}
}

View File

@@ -1,12 +1,16 @@
import { CommonModule, DatePipe } from "@angular/common";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, ReactiveFormsModule } from "@angular/forms";
import { firstValueFrom } from "rxjs";
import { Component, OnInit, Input, OnDestroy } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormBuilder, FormControl, ReactiveFormsModule, Validators } from "@angular/forms";
import { firstValueFrom, Subject, takeUntil } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { SendType } from "@bitwarden/common/tools/send/enums/send-type";
import { SendView } from "@bitwarden/common/tools/send/models/view/send.view";
import {
SectionComponent,
SectionHeaderComponent,
@@ -18,13 +22,29 @@ import {
SelectModule,
} from "@bitwarden/components";
import { SendFormConfig } from "../../abstractions/send-form-config.service";
import { SendFormContainer } from "../../send-form-container";
import { SendOptionsComponent } from "../options/send-options.component";
import { BaseSendDetailsComponent } from "./base-send-details.component";
import { SendFileDetailsComponent } from "./send-file-details.component";
import { SendTextDetailsComponent } from "./send-text-details.component";
// Value = hours
export enum DatePreset {
OneHour = 1,
OneDay = 24,
TwoDays = 48,
ThreeDays = 72,
SevenDays = 168,
FourteenDays = 336,
ThirtyDays = 720,
}
export interface DatePresetSelectOption {
name: string;
value: DatePreset | string;
}
@Component({
selector: "tools-send-details",
templateUrl: "./send-details.component.html",
@@ -46,10 +66,22 @@ import { SendTextDetailsComponent } from "./send-text-details.component";
SelectModule,
],
})
export class SendDetailsComponent extends BaseSendDetailsComponent implements OnInit {
export class SendDetailsComponent implements OnInit, OnDestroy {
private destroy$ = new Subject<void>();
@Input() config: SendFormConfig;
@Input() originalSendView?: SendView;
FileSendType = SendType.File;
TextSendType = SendType.Text;
sendLink: string | null = null;
customDeletionDateOption: DatePresetSelectOption | null = null;
datePresetOptions: DatePresetSelectOption[] = [];
sendDetailsForm = this.formBuilder.group({
name: new FormControl("", Validators.required),
selectedDeletionDatePreset: new FormControl(DatePreset.SevenDays || "", Validators.required),
});
constructor(
protected sendFormContainer: SendFormContainer,
@@ -57,19 +89,81 @@ export class SendDetailsComponent extends BaseSendDetailsComponent implements On
protected i18nService: I18nService,
protected datePipe: DatePipe,
protected environmentService: EnvironmentService,
private policyService: PolicyService,
) {
super(sendFormContainer, formBuilder, i18nService, datePipe);
}
this.sendDetailsForm.valueChanges.pipe(takeUntilDestroyed()).subscribe((value) => {
this.sendFormContainer.patchSend((send) => {
return Object.assign(send, {
name: value.name,
deletionDate: new Date(this.formattedDeletionDate),
expirationDate: new Date(this.formattedDeletionDate),
} as SendView);
});
});
async getSendLink() {}
this.sendFormContainer.registerChildForm("sendDetailsForm", this.sendDetailsForm);
}
async ngOnInit() {
await super.ngOnInit();
if (!this.originalSendView) {
return;
this.setupDeletionDatePresets();
if (this.originalSendView) {
this.sendDetailsForm.patchValue({
name: this.originalSendView.name,
selectedDeletionDatePreset: this.originalSendView.deletionDate.toString(),
});
if (this.originalSendView.deletionDate) {
this.customDeletionDateOption = {
name: this.datePipe.transform(this.originalSendView.deletionDate, "MM/dd/yyyy, hh:mm a"),
value: this.originalSendView.deletionDate.toString(),
};
this.datePresetOptions.unshift(this.customDeletionDateOption);
}
const env = await firstValueFrom(this.environmentService.environment$);
this.sendLink =
env.getSendUrl() + this.originalSendView.accessId + "/" + this.originalSendView.urlB64Key;
}
const env = await firstValueFrom(this.environmentService.environment$);
this.sendLink =
env.getSendUrl() + this.originalSendView.accessId + "/" + this.originalSendView.urlB64Key;
this.policyService
.policyAppliesToActiveUser$(PolicyType.DisableSend)
.pipe(takeUntil(this.destroy$))
.subscribe((policyAppliesToActiveUser) => {
if (policyAppliesToActiveUser) {
this.sendDetailsForm.disable();
}
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
setupDeletionDatePresets() {
const defaultSelections: DatePresetSelectOption[] = [
{ name: this.i18nService.t("oneHour"), value: DatePreset.OneHour },
{ name: this.i18nService.t("oneDay"), value: DatePreset.OneDay },
{ name: this.i18nService.t("days", "2"), value: DatePreset.TwoDays },
{ name: this.i18nService.t("days", "3"), value: DatePreset.ThreeDays },
{ name: this.i18nService.t("days", "7"), value: DatePreset.SevenDays },
{ name: this.i18nService.t("days", "14"), value: DatePreset.FourteenDays },
{ name: this.i18nService.t("days", "30"), value: DatePreset.ThirtyDays },
];
this.datePresetOptions = defaultSelections;
}
get formattedDeletionDate(): string {
const now = new Date();
const selectedValue = this.sendDetailsForm.controls.selectedDeletionDatePreset.value;
if (typeof selectedValue === "string") {
return selectedValue;
}
const milliseconds = now.setTime(now.getTime() + (selectedValue as number) * 60 * 60 * 1000);
return new Date(milliseconds).toString();
}
}

View File

@@ -2,8 +2,11 @@ import { CommonModule } from "@angular/common";
import { Component, Input, OnInit } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormBuilder, Validators, ReactiveFormsModule, FormsModule } from "@angular/forms";
import { Subject, takeUntil } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { SendType } from "@bitwarden/common/tools/send/enums/send-type";
import { SendFileView } from "@bitwarden/common/tools/send/models/view/send-file.view";
import { SendView } from "@bitwarden/common/tools/send/models/view/send.view";
@@ -27,6 +30,8 @@ import { SendFormContainer } from "../../send-form-container";
],
})
export class SendFileDetailsComponent implements OnInit {
private destroy$ = new Subject<void>();
@Input() config: SendFormConfig;
@Input() originalSendView?: SendView;
@@ -40,6 +45,7 @@ export class SendFileDetailsComponent implements OnInit {
constructor(
private formBuilder: FormBuilder,
protected sendFormContainer: SendFormContainer,
private policyService: PolicyService,
) {
this.sendFormContainer.registerChildForm("sendFileDetailsForm", this.sendFileDetailsForm);
@@ -67,5 +73,14 @@ export class SendFileDetailsComponent implements OnInit {
file: this.originalSendView.file,
});
}
this.policyService
.policyAppliesToActiveUser$(PolicyType.DisableSend)
.pipe(takeUntil(this.destroy$))
.subscribe((policyAppliesToActiveUser) => {
if (policyAppliesToActiveUser) {
this.sendFileDetailsForm.disable();
}
});
}
}

View File

@@ -1,9 +1,12 @@
import { CommonModule } from "@angular/common";
import { Component, Input, OnInit } from "@angular/core";
import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormBuilder, FormControl, Validators, ReactiveFormsModule } from "@angular/forms";
import { Subject, takeUntil } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { SendView } from "@bitwarden/common/tools/send/models/view/send.view";
import { CheckboxModule, FormFieldModule, SectionComponent } from "@bitwarden/components";
@@ -23,7 +26,9 @@ import { SendFormContainer } from "../../send-form-container";
SectionComponent,
],
})
export class SendTextDetailsComponent implements OnInit {
export class SendTextDetailsComponent implements OnInit, OnDestroy {
private destroy$ = new Subject<void>();
@Input() config: SendFormConfig;
@Input() originalSendView?: SendView;
@@ -35,6 +40,7 @@ export class SendTextDetailsComponent implements OnInit {
constructor(
private formBuilder: FormBuilder,
protected sendFormContainer: SendFormContainer,
private policyService: PolicyService,
) {
this.sendFormContainer.registerChildForm("sendTextDetailsForm", this.sendTextDetailsForm);
@@ -57,5 +63,19 @@ export class SendTextDetailsComponent implements OnInit {
hidden: this.originalSendView.text?.hidden || false,
});
}
this.policyService
.policyAppliesToActiveUser$(PolicyType.DisableSend)
.pipe(takeUntil(this.destroy$))
.subscribe((policyAppliesToActiveUser) => {
if (policyAppliesToActiveUser) {
this.sendTextDetailsForm.disable();
}
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}

View File

@@ -182,6 +182,8 @@ export class SendFormComponent implements AfterViewInit, OnInit, OnChanges, Send
this.loading = false;
}
formDisabled = false;
constructor(
private formBuilder: FormBuilder,
private addEditFormService: SendFormService,