bitwarden-estensione-browser/apps/web/src/app/organizations/manage/policy-edit.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-01-20 14:57:55 +01:00
import {
ChangeDetectorRef,
2020-01-20 14:57:55 +01:00
Component,
ComponentFactoryResolver,
2020-01-20 14:57:55 +01:00
EventEmitter,
Input,
Output,
ViewChild,
ViewContainerRef,
2020-01-20 14:57:55 +01:00
} from "@angular/core";
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { PolicyType } from "@bitwarden/common/enums/policyType";
import { PolicyRequest } from "@bitwarden/common/models/request/policyRequest";
import { PolicyResponse } from "@bitwarden/common/models/response/policyResponse";
2020-01-20 14:57:55 +01:00
import { BasePolicy, BasePolicyComponent } from "../policies/base-policy.component";
2020-01-20 14:57:55 +01:00
@Component({
selector: "app-policy-edit",
templateUrl: "policy-edit.component.html",
})
export class PolicyEditComponent {
@Input() policy: BasePolicy;
2020-01-20 14:57:55 +01:00
@Input() organizationId: string;
@Input() policiesEnabledMap: Map<PolicyType, boolean> = new Map<PolicyType, boolean>();
2020-01-20 14:57:55 +01:00
@Output() onSavedPolicy = new EventEmitter();
2021-12-17 15:57:11 +01:00
@ViewChild("policyForm", { read: ViewContainerRef, static: true })
policyFormRef: ViewContainerRef;
2021-12-17 15:57:11 +01:00
2020-01-29 23:49:20 +01:00
policyType = PolicyType;
2020-01-20 14:57:55 +01:00
loading = true;
enabled = false;
formPromise: Promise<any>;
defaultTypes: any[];
policyComponent: BasePolicyComponent;
2021-12-17 15:57:11 +01:00
private policyResponse: PolicyResponse;
2021-12-17 15:57:11 +01:00
2020-01-20 14:57:55 +01:00
constructor(
private apiService: ApiService,
private i18nService: I18nService,
2021-12-07 20:41:45 +01:00
private platformUtilsService: PlatformUtilsService,
private componentFactoryResolver: ComponentFactoryResolver,
private cdr: ChangeDetectorRef,
private logService: LogService
2021-12-17 15:57:11 +01:00
) {}
async ngAfterViewInit() {
2020-01-20 14:57:55 +01:00
await this.load();
this.loading = false;
2021-12-17 15:57:11 +01:00
const factory = this.componentFactoryResolver.resolveComponentFactory(this.policy.component);
this.policyComponent = this.policyFormRef.createComponent(factory)
.instance as BasePolicyComponent;
this.policyComponent.policy = this.policy;
this.policyComponent.policyResponse = this.policyResponse;
2021-12-17 15:57:11 +01:00
this.cdr.detectChanges();
2021-12-17 15:57:11 +01:00
}
2020-01-20 14:57:55 +01:00
async load() {
2021-12-17 15:57:11 +01:00
try {
this.policyResponse = await this.apiService.getPolicy(this.organizationId, this.policy.type);
} catch (e) {
2020-01-20 14:57:55 +01:00
if (e.statusCode === 404) {
this.policyResponse = new PolicyResponse({ Enabled: false });
2021-12-17 15:57:11 +01:00
} else {
throw e;
}
}
2021-12-17 15:57:11 +01:00
}
async submit() {
let request: PolicyRequest;
try {
request = await this.policyComponent.buildRequest(this.policiesEnabledMap);
2020-01-20 14:57:55 +01:00
} catch (e) {
2022-02-07 22:15:49 +01:00
this.platformUtilsService.showToast("error", null, e.message);
return;
2020-01-20 14:57:55 +01:00
}
try {
this.formPromise = this.apiService.putPolicy(this.organizationId, this.policy.type, request);
await this.formPromise;
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast(
"success",
null,
this.i18nService.t("editedPolicyId", this.i18nService.t(this.policy.name))
);
this.onSavedPolicy.emit();
} catch (e) {
this.logService.error(e);
2020-01-20 14:57:55 +01:00
}
2021-12-17 15:57:11 +01:00
}
2020-01-20 14:57:55 +01:00
}