From c6bc97cf5c26bd025b107b10015ba3e289275275 Mon Sep 17 00:00:00 2001 From: aj-rosado <109146700+aj-rosado@users.noreply.github.com> Date: Wed, 5 Apr 2023 19:15:33 +0100 Subject: [PATCH] [PS-2390] Passing folder and collection id on import (#4802) * PS-2390 - Passing folder and collection id on import Reading groupingid from lastpass csv as collection or folder id * PS-2390 - Added toDomain and toModel on FolderWithIdExport model and created CollectionWithIdExport model * PS-2390 - renamed groupingid into bwcollectionid on lastpass importer * PS-2390 - Updated collection/folder-with-id export to reuse parent toDomain and toView * PS-2390 Undo the lastpass importer groupingId rename * PS-2390 Undo lastpass importer changes * PS-2390 - Removed externalId set. Cleaning collection-with-id-request to user parent properties * Lint prettier --- .../models/request/collection-with-id.request.ts | 14 ++++++++++++++ .../src/models/export/collection-with-id.export.ts | 10 ++++++++++ .../src/models/export/folder-with-id.export.ts | 10 ++++++++++ .../src/models/request/import-ciphers.request.ts | 4 ++-- .../request/import-organization-ciphers.request.ts | 4 ++-- libs/importer/src/importers/base-importer.ts | 1 + .../importers/bitwarden/bitwarden-json-importer.ts | 6 ------ libs/importer/src/services/import.service.ts | 8 ++++---- 8 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 libs/common/src/admin-console/models/request/collection-with-id.request.ts diff --git a/libs/common/src/admin-console/models/request/collection-with-id.request.ts b/libs/common/src/admin-console/models/request/collection-with-id.request.ts new file mode 100644 index 0000000000..ae48395a49 --- /dev/null +++ b/libs/common/src/admin-console/models/request/collection-with-id.request.ts @@ -0,0 +1,14 @@ +import { Collection } from "../domain/collection"; +import { CollectionRequest } from "../request/collection.request"; + +export class CollectionWithIdRequest extends CollectionRequest { + id: string; + + constructor(collection?: Collection) { + if (collection == null) { + return; + } + super(collection); + this.id = collection.id; + } +} diff --git a/libs/common/src/models/export/collection-with-id.export.ts b/libs/common/src/models/export/collection-with-id.export.ts index 603c4fe87f..b97c8b7900 100644 --- a/libs/common/src/models/export/collection-with-id.export.ts +++ b/libs/common/src/models/export/collection-with-id.export.ts @@ -6,6 +6,16 @@ import { CollectionExport } from "./collection.export"; export class CollectionWithIdExport extends CollectionExport { id: string; + static toView(req: CollectionWithIdExport, view = new CollectionView()) { + view.id = req.id; + return super.toView(req, view); + } + + static toDomain(req: CollectionWithIdExport, domain = new CollectionDomain()) { + domain.id = req.id; + return super.toDomain(req, domain); + } + // Use build method instead of ctor so that we can control order of JSON stringify for pretty print build(o: CollectionView | CollectionDomain) { this.id = o.id; diff --git a/libs/common/src/models/export/folder-with-id.export.ts b/libs/common/src/models/export/folder-with-id.export.ts index c0f3c55c4b..1f1964bb4a 100644 --- a/libs/common/src/models/export/folder-with-id.export.ts +++ b/libs/common/src/models/export/folder-with-id.export.ts @@ -6,6 +6,16 @@ import { FolderExport } from "./folder.export"; export class FolderWithIdExport extends FolderExport { id: string; + static toView(req: FolderWithIdExport, view = new FolderView()) { + view.id = req.id; + return super.toView(req, view); + } + + static toDomain(req: FolderWithIdExport, domain = new FolderDomain()) { + domain.id = req.id; + return super.toDomain(req, domain); + } + // Use build method instead of ctor so that we can control order of JSON stringify for pretty print build(o: FolderView | FolderDomain) { this.id = o.id; diff --git a/libs/common/src/models/request/import-ciphers.request.ts b/libs/common/src/models/request/import-ciphers.request.ts index 065f1b2838..14103751b8 100644 --- a/libs/common/src/models/request/import-ciphers.request.ts +++ b/libs/common/src/models/request/import-ciphers.request.ts @@ -1,10 +1,10 @@ import { CipherRequest } from "../../vault/models/request/cipher.request"; -import { FolderRequest } from "../../vault/models/request/folder.request"; +import { FolderWithIdRequest } from "../../vault/models/request/folder-with-id.request"; import { KvpRequest } from "./kvp.request"; export class ImportCiphersRequest { ciphers: CipherRequest[] = []; - folders: FolderRequest[] = []; + folders: FolderWithIdRequest[] = []; folderRelationships: KvpRequest[] = []; } diff --git a/libs/common/src/models/request/import-organization-ciphers.request.ts b/libs/common/src/models/request/import-organization-ciphers.request.ts index 03d0056090..5f1a4ee4c3 100644 --- a/libs/common/src/models/request/import-organization-ciphers.request.ts +++ b/libs/common/src/models/request/import-organization-ciphers.request.ts @@ -1,10 +1,10 @@ -import { CollectionRequest } from "../../admin-console/models/request/collection.request"; +import { CollectionWithIdRequest } from "../../admin-console/models/request/collection-with-id.request"; import { CipherRequest } from "../../vault/models/request/cipher.request"; import { KvpRequest } from "./kvp.request"; export class ImportOrganizationCiphersRequest { ciphers: CipherRequest[] = []; - collections: CollectionRequest[] = []; + collections: CollectionWithIdRequest[] = []; collectionRelationships: KvpRequest[] = []; } diff --git a/libs/importer/src/importers/base-importer.ts b/libs/importer/src/importers/base-importer.ts index ee93c0144c..4aef45f61e 100644 --- a/libs/importer/src/importers/base-importer.ts +++ b/libs/importer/src/importers/base-importer.ts @@ -335,6 +335,7 @@ export abstract class BaseImporter { result.collections = result.folders.map((f) => { const collection = new CollectionView(); collection.name = f.name; + collection.id = f.id; return collection; }); result.folderRelationships = []; diff --git a/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts b/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts index 567b4a3596..20dbb7c4f8 100644 --- a/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts +++ b/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts @@ -62,7 +62,6 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { for (const c of this.results.collections as CollectionWithIdExport[]) { const collection = CollectionWithIdExport.toDomain(c); if (collection != null) { - collection.id = null; collection.organizationId = this.organizationId; const view = await collection.decrypt(); groupingsMap.set(c.id, this.result.collections.length); @@ -73,7 +72,6 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { for (const f of this.results.folders as FolderWithIdExport[]) { const folder = FolderWithIdExport.toDomain(f); if (folder != null) { - folder.id = null; const view = await folder.decrypt(); groupingsMap.set(f.id, this.result.folders.length); this.result.folders.push(view); @@ -85,7 +83,6 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { const cipher = CipherWithIdExport.toDomain(c); // reset ids incase they were set for some reason cipher.id = null; - cipher.folderId = null; cipher.organizationId = this.organizationId; cipher.collectionIds = null; @@ -124,7 +121,6 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { this.results.collections.forEach((c: CollectionWithIdExport) => { const collection = CollectionWithIdExport.toView(c); if (collection != null) { - collection.id = null; collection.organizationId = null; groupingsMap.set(c.id, this.result.collections.length); this.result.collections.push(collection); @@ -134,7 +130,6 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { this.results.folders.forEach((f: FolderWithIdExport) => { const folder = FolderWithIdExport.toView(f); if (folder != null) { - folder.id = null; groupingsMap.set(f.id, this.result.folders.length); this.result.folders.push(folder); } @@ -145,7 +140,6 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { const cipher = CipherWithIdExport.toView(c); // reset ids incase they were set for some reason cipher.id = null; - cipher.folderId = null; cipher.organizationId = null; cipher.collectionIds = null; diff --git a/libs/importer/src/services/import.service.ts b/libs/importer/src/services/import.service.ts index 7a8b570976..9175bd91d1 100644 --- a/libs/importer/src/services/import.service.ts +++ b/libs/importer/src/services/import.service.ts @@ -1,7 +1,7 @@ import { CryptoService } from "@bitwarden/common/abstractions/crypto.service"; import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; import { CollectionService } from "@bitwarden/common/admin-console/abstractions/collection.service"; -import { CollectionRequest } from "@bitwarden/common/admin-console/models/request/collection.request"; +import { CollectionWithIdRequest } from "@bitwarden/common/admin-console/models/request/collection-with-id.request"; import { Utils } from "@bitwarden/common/misc/utils"; import { ImportCiphersRequest } from "@bitwarden/common/models/request/import-ciphers.request"; import { ImportOrganizationCiphersRequest } from "@bitwarden/common/models/request/import-organization-ciphers.request"; @@ -11,7 +11,7 @@ import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.servi import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { CipherType } from "@bitwarden/common/vault/enums/cipher-type"; import { CipherRequest } from "@bitwarden/common/vault/models/request/cipher.request"; -import { FolderRequest } from "@bitwarden/common/vault/models/request/folder.request"; +import { FolderWithIdRequest } from "@bitwarden/common/vault/models/request/folder-with-id.request"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; import { @@ -304,7 +304,7 @@ export class ImportService implements ImportServiceAbstraction { if (importResult.folders != null) { for (let i = 0; i < importResult.folders.length; i++) { const f = await this.folderService.encrypt(importResult.folders[i]); - request.folders.push(new FolderRequest(f)); + request.folders.push(new FolderWithIdRequest(f)); } } if (importResult.folderRelationships != null) { @@ -324,7 +324,7 @@ export class ImportService implements ImportServiceAbstraction { for (let i = 0; i < importResult.collections.length; i++) { importResult.collections[i].organizationId = organizationId; const c = await this.collectionService.encrypt(importResult.collections[i]); - request.collections.push(new CollectionRequest(c)); + request.collections.push(new CollectionWithIdRequest(c)); } } if (importResult.collectionRelationships != null) {