2018-04-10 01:05:13 +02:00
|
|
|
import {
|
2020-08-17 18:14:40 +02:00
|
|
|
Directive,
|
2018-04-10 01:05:13 +02:00
|
|
|
EventEmitter,
|
2021-09-14 16:32:06 +02:00
|
|
|
OnInit,
|
2018-04-10 01:05:13 +02:00
|
|
|
Output,
|
|
|
|
} from '@angular/core';
|
2021-11-09 17:01:22 +01:00
|
|
|
import { FormBuilder } from '@angular/forms';
|
2018-04-10 01:05:13 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
|
|
|
import { EventService } from 'jslib-common/abstractions/event.service';
|
|
|
|
import { ExportService } from 'jslib-common/abstractions/export.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-19 10:32:14 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-06-03 18:58:57 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
2021-09-14 16:32:06 +02:00
|
|
|
import { PolicyService } from 'jslib-common/abstractions/policy.service';
|
2021-11-09 17:01:22 +01:00
|
|
|
import { UserVerificationService } from 'jslib-common/abstractions/userVerification.service';
|
2021-06-09 23:24:31 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { EventType } from 'jslib-common/enums/eventType';
|
2021-09-14 16:32:06 +02:00
|
|
|
import { PolicyType } from 'jslib-common/enums/policyType';
|
2018-04-10 01:05:13 +02:00
|
|
|
|
2020-08-17 18:14:40 +02:00
|
|
|
@Directive()
|
2021-09-14 16:32:06 +02:00
|
|
|
export class ExportComponent implements OnInit {
|
2018-04-10 01:05:13 +02:00
|
|
|
@Output() onSaved = new EventEmitter();
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
formPromise: Promise<string>;
|
2021-09-14 16:32:06 +02:00
|
|
|
disabledByPolicy: boolean = false;
|
2018-04-10 01:05:13 +02:00
|
|
|
|
2021-11-09 17:01:22 +01:00
|
|
|
exportForm = this.fb.group({
|
|
|
|
format: ['json'],
|
|
|
|
secret: [''],
|
|
|
|
});
|
|
|
|
|
|
|
|
formatOptions = [
|
|
|
|
{ name: '.json', value: 'json' },
|
|
|
|
{ name: '.csv', value: 'csv' },
|
|
|
|
{ name: '.json (Encrypted)', value: 'encrypted_json' },
|
|
|
|
];
|
|
|
|
|
2018-10-03 06:03:49 +02:00
|
|
|
constructor(protected cryptoService: CryptoService, protected i18nService: I18nService,
|
2018-08-14 21:12:10 +02:00
|
|
|
protected platformUtilsService: PlatformUtilsService, protected exportService: ExportService,
|
2021-10-19 10:32:14 +02:00
|
|
|
protected eventService: EventService, private policyService: PolicyService, protected win: Window,
|
2021-11-09 17:01:22 +01:00
|
|
|
private logService: LogService, private userVerificationService: UserVerificationService,
|
|
|
|
private fb: FormBuilder) { }
|
2021-09-14 16:32:06 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
await this.checkExportDisabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkExportDisabled() {
|
|
|
|
this.disabledByPolicy = await this.policyService.policyAppliesToUser(PolicyType.DisablePersonalVaultExport);
|
2021-11-09 17:01:22 +01:00
|
|
|
if (this.disabledByPolicy) {
|
|
|
|
this.exportForm.disable();
|
|
|
|
}
|
2021-09-14 16:32:06 +02:00
|
|
|
}
|
2018-04-10 01:05:13 +02:00
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
get encryptedFormat() {
|
|
|
|
return this.format === 'encrypted_json';
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:05:13 +02:00
|
|
|
async submit() {
|
2021-09-14 16:32:06 +02:00
|
|
|
if (this.disabledByPolicy) {
|
|
|
|
this.platformUtilsService.showToast('error', null, this.i18nService.t('personalVaultExportPolicyInEffect'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:01:22 +01:00
|
|
|
const acceptedWarning = await this.warningDialog();
|
|
|
|
if (!acceptedWarning) {
|
2018-04-10 01:05:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:01:22 +01:00
|
|
|
const secret = this.exportForm.get('secret').value;
|
2021-11-16 10:43:37 +01:00
|
|
|
try {
|
|
|
|
await this.userVerificationService.verifyUser(secret);
|
|
|
|
} catch (e) {
|
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), e.message);
|
2020-12-22 22:14:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:01:22 +01:00
|
|
|
try {
|
|
|
|
this.formPromise = this.getExportData();
|
|
|
|
const data = await this.formPromise;
|
|
|
|
this.downloadFile(data);
|
|
|
|
this.saved();
|
|
|
|
await this.collectEvent();
|
|
|
|
this.exportForm.get('secret').setValue('');
|
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2018-04-10 01:05:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 23:56:20 +01:00
|
|
|
async warningDialog() {
|
|
|
|
if (this.encryptedFormat) {
|
|
|
|
return await this.platformUtilsService.showDialog(
|
|
|
|
'<p>' + this.i18nService.t('encExportKeyWarningDesc') +
|
|
|
|
'<p>' + this.i18nService.t('encExportAccountWarningDesc'),
|
|
|
|
this.i18nService.t('confirmVaultExport'), this.i18nService.t('exportVault'),
|
|
|
|
this.i18nService.t('cancel'), 'warning',
|
|
|
|
true);
|
|
|
|
} else {
|
|
|
|
return await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t('exportWarningDesc'),
|
|
|
|
this.i18nService.t('confirmVaultExport'), this.i18nService.t('exportVault'),
|
|
|
|
this.i18nService.t('cancel'), 'warning');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:05:13 +02:00
|
|
|
protected saved() {
|
|
|
|
this.onSaved.emit();
|
|
|
|
}
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
protected getExportData() {
|
2018-12-17 16:54:03 +01:00
|
|
|
return this.exportService.getExport(this.format);
|
2018-07-05 20:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected getFileName(prefix?: string) {
|
2020-12-03 21:20:38 +01:00
|
|
|
let extension = this.format;
|
|
|
|
if (this.format === 'encrypted_json') {
|
|
|
|
if (prefix == null) {
|
|
|
|
prefix = 'encrypted';
|
|
|
|
} else {
|
|
|
|
prefix = 'encrypted_' + prefix;
|
|
|
|
}
|
|
|
|
extension = 'json';
|
|
|
|
}
|
|
|
|
return this.exportService.getFileName(prefix, extension);
|
2018-07-05 20:39:58 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 23:11:36 +02:00
|
|
|
protected async collectEvent(): Promise<any> {
|
|
|
|
await this.eventService.collect(EventType.User_ClientExportedVault);
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:01:22 +01:00
|
|
|
get format() {
|
|
|
|
return this.exportForm.get('format').value;
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:05:13 +02:00
|
|
|
private downloadFile(csv: string): void {
|
2018-07-05 20:39:58 +02:00
|
|
|
const fileName = this.getFileName();
|
2018-04-10 01:05:13 +02:00
|
|
|
this.platformUtilsService.saveFile(this.win, csv, { type: 'text/plain' }, fileName);
|
|
|
|
}
|
|
|
|
}
|