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

92 lines
2.9 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';
import { ToasterService } from 'angular2-toaster';
import { ApiService } from 'jslib-common/abstractions/api.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
2020-01-20 14:57:55 +01:00
import { PolicyType } from 'jslib-common/enums/policyType';
2020-01-20 14:57:55 +01:00
import { PolicyRequest } from 'jslib-common/models/request/policyRequest';
2020-01-20 14:57:55 +01:00
import { PolicyResponse } from 'jslib-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();
@ViewChild('policyForm', { read: ViewContainerRef, static: true }) policyFormRef: ViewContainerRef;
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;
2020-01-20 14:57:55 +01:00
private policyResponse: PolicyResponse;
2020-01-20 14:57:55 +01:00
constructor(private apiService: ApiService, private i18nService: I18nService,
private toasterService: ToasterService, private componentFactoryResolver: ComponentFactoryResolver,
private cdr: ChangeDetectorRef) {
}
2020-01-20 14:57:55 +01:00
async ngAfterViewInit() {
2020-01-20 14:57:55 +01:00
await this.load();
this.loading = false;
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;
this.cdr.detectChanges();
2020-01-20 14:57:55 +01:00
}
async load() {
try {
this.policyResponse = await this.apiService.getPolicy(this.organizationId, this.policy.type);
2020-01-20 14:57:55 +01:00
} catch (e) {
if (e.statusCode === 404) {
this.policyResponse = new PolicyResponse({Enabled: false});
2020-01-20 14:57:55 +01:00
} else {
throw e;
}
}
}
async submit() {
let request: PolicyRequest;
try {
request = await this.policyComponent.buildRequest(this.policiesEnabledMap);
} catch (e) {
this.toasterService.pop('error', null, e);
return;
}
try {
this.formPromise = this.apiService.putPolicy(this.organizationId, this.policy.type, request);
await this.formPromise;
this.toasterService.popAsync('success', null, this.i18nService.t('editedPolicyId', this.i18nService.t(this.policy.name)));
this.onSavedPolicy.emit();
} catch {}
2020-01-20 14:57:55 +01:00
}
}