diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index 017dbbbed9..152ce49d12 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -29,6 +29,8 @@ import { CipherView } from 'jslib/models/view/cipherView'; import { CollectionView } from 'jslib/models/view/collectionView'; import { FolderView } from 'jslib/models/view/folderView'; +import { CipherString } from 'jslib/models/domain/cipherString'; + import { Response } from 'jslib/cli/models/response'; import { MessageResponse } from 'jslib/cli/models/response/messageResponse'; import { StringResponse } from 'jslib/cli/models/response/stringResponse'; @@ -36,11 +38,14 @@ import { StringResponse } from 'jslib/cli/models/response/stringResponse'; import { CipherResponse } from '../models/response/cipherResponse'; import { CollectionResponse } from '../models/response/collectionResponse'; import { FolderResponse } from '../models/response/folderResponse'; +import { OrganizationCollectionResponse } from '../models/response/organizationCollectionResponse'; import { OrganizationResponse } from '../models/response/organizationResponse'; import { TemplateResponse } from '../models/response/templateResponse'; import { OrganizationCollectionRequest } from '../models/request/organizationCollectionRequest'; +import { SelectionReadOnly } from '../models/selectionReadOnly'; + import { CliUtils } from '../utils'; import { Utils } from 'jslib/misc/utils'; @@ -76,6 +81,8 @@ export class GetCommand { return await this.getFolder(id); case 'collection': return await this.getCollection(id); + case 'org-collection': + return await this.getOrganizationCollection(id, cmd); case 'organization': return await this.getOrganization(id); case 'template': @@ -326,6 +333,35 @@ export class GetCommand { return Response.success(res); } + private async getOrganizationCollection(id: string, cmd: program.Command) { + if (cmd.organizationid == null || cmd.organizationid === '') { + return Response.badRequest('--organizationid required.'); + } + if (!this.isGuid(id)) { + return Response.error('`' + id + '` is not a GUID.'); + } + if (!this.isGuid(cmd.organizationid)) { + return Response.error('`' + cmd.organizationid + '` is not a GUID.'); + } + try { + const orgKey = await this.cryptoService.getOrgKey(cmd.organizationid); + if (orgKey == null) { + throw new Error('No encryption key for this organization.'); + } + + const response = await this.apiService.getCollectionDetails(cmd.organizationid, id); + const decCollection = new CollectionView(response); + decCollection.name = await this.cryptoService.decryptToUtf8( + new CipherString(response.name), orgKey); + const groups = response.groups == null ? null : + response.groups.map((g) => new SelectionReadOnly(g.id, g.readOnly)); + const res = new OrganizationCollectionResponse(decCollection, groups); + return Response.success(res); + } catch (e) { + return Response.error(e); + } + } + private async getOrganization(id: string) { let org: Organization = null; if (this.isGuid(id)) { diff --git a/src/models/response/organizationCollectionResponse.ts b/src/models/response/organizationCollectionResponse.ts index e4f3c357f6..8e87bc5fa2 100644 --- a/src/models/response/organizationCollectionResponse.ts +++ b/src/models/response/organizationCollectionResponse.ts @@ -9,6 +9,7 @@ export class OrganizationCollectionResponse extends CollectionResponse { constructor(o: CollectionView, groups: SelectionReadOnly[]) { super(o); + this.object = 'org-collection'; this.groups = groups; } } diff --git a/src/program.ts b/src/program.ts index 480042329b..9f272d68a7 100644 --- a/src/program.ts +++ b/src/program.ts @@ -264,6 +264,7 @@ export class Program extends BaseProgram { .description('Get an object from the vault.') .option('--itemid ', 'Attachment\'s item id.') .option('--output ', 'Output directory or filename for attachment.') + .option('--organizationid ', 'Organization id for an organization object.') .on('--help', () => { writeLn('\n Objects:'); writeLn('');