diff --git a/src/commands/create.command.ts b/src/commands/create.command.ts index 4027b1a646..6dfad4973d 100644 --- a/src/commands/create.command.ts +++ b/src/commands/create.command.ts @@ -25,6 +25,8 @@ import { OrganizationCollectionRequest } from '../models/request/organizationCol import { CliUtils } from '../utils'; +import { Utils } from 'jslib/misc/utils'; + export class CreateCommand { constructor(private cipherService: CipherService, private folderService: FolderService, private userService: UserService, private cryptoService: CryptoService, @@ -57,7 +59,7 @@ export class CreateCommand { case 'folder': return await this.createFolder(req); case 'org-collection': - return await this.createOrganizationCollection(req); + return await this.createOrganizationCollection(req, cmd); default: return Response.badRequest('Unknown object.'); } @@ -130,7 +132,16 @@ export class CreateCommand { } } - private async createOrganizationCollection(req: OrganizationCollectionRequest) { + private async createOrganizationCollection(req: OrganizationCollectionRequest, cmd: program.Command) { + if (cmd.organizationid == null || cmd.organizationid === '') { + return Response.badRequest('--organizationid required.'); + } + if (!Utils.isGuid(cmd.organizationid)) { + return Response.error('`' + cmd.organizationid + '` is not a GUID.'); + } + if (cmd.organizationid !== req.organizationId) { + return Response.error('--organizationid does not match request object.'); + } try { const orgKey = await this.cryptoService.getOrgKey(req.organizationId); if (orgKey == null) { diff --git a/src/commands/edit.command.ts b/src/commands/edit.command.ts index 571e0af0a3..864f224427 100644 --- a/src/commands/edit.command.ts +++ b/src/commands/edit.command.ts @@ -1,20 +1,32 @@ import * as program from 'commander'; +import { ApiService } from 'jslib/abstractions/api.service'; import { CipherService } from 'jslib/abstractions/cipher.service'; -import { FolderService } from 'jslib/services/folder.service'; +import { CryptoService } from 'jslib/abstractions/crypto.service'; +import { FolderService } from 'jslib/abstractions/folder.service'; import { Cipher } from 'jslib/models/export/cipher'; +import { Collection } from 'jslib/models/export/collection'; import { Folder } from 'jslib/models/export/folder'; +import { CollectionRequest } from 'jslib/models/request/collectionRequest'; +import { SelectionReadOnlyRequest } from 'jslib/models/request/selectionReadOnlyRequest'; + import { Response } from 'jslib/cli/models/response'; import { CipherResponse } from '../models/response/cipherResponse'; import { FolderResponse } from '../models/response/folderResponse'; +import { OrganizationCollectionResponse } from '../models/response/organizationCollectionResponse'; + +import { OrganizationCollectionRequest } from '../models/request/organizationCollectionRequest'; import { CliUtils } from '../utils'; +import { Utils } from 'jslib/misc/utils'; + export class EditCommand { - constructor(private cipherService: CipherService, private folderService: FolderService) { } + constructor(private cipherService: CipherService, private folderService: FolderService, + private cryptoService: CryptoService, private apiService: ApiService) { } async run(object: string, id: string, requestJson: string, cmd: program.Command): Promise { if (requestJson == null || requestJson === '') { @@ -44,6 +56,8 @@ export class EditCommand { return await this.editCipherCollections(id, req); case 'folder': return await this.editFolder(id, req); + case 'org-collection': + return await this.editOrganizationCollection(id, req, cmd); default: return Response.badRequest('Unknown object.'); } @@ -109,4 +123,37 @@ export class EditCommand { return Response.error(e); } } + + private async editOrganizationCollection(id: string, req: OrganizationCollectionRequest, cmd: program.Command) { + if (cmd.organizationid == null || cmd.organizationid === '') { + return Response.badRequest('--organizationid required.'); + } + if (!Utils.isGuid(id)) { + return Response.error('`' + id + '` is not a GUID.'); + } + if (!Utils.isGuid(cmd.organizationid)) { + return Response.error('`' + cmd.organizationid + '` is not a GUID.'); + } + if (cmd.organizationid !== req.organizationId) { + return Response.error('--organizationid does not match request object.'); + } + try { + const orgKey = await this.cryptoService.getOrgKey(req.organizationId); + if (orgKey == null) { + throw new Error('No encryption key for this organization.'); + } + + const groups = req.groups == null ? null : + req.groups.map((g) => new SelectionReadOnlyRequest(g.id, g.readOnly)); + const request = new CollectionRequest(); + request.name = (await this.cryptoService.encrypt(req.name, orgKey)).encryptedString; + request.externalId = req.externalId; + request.groups = groups; + await this.apiService.putCollection(req.organizationId, id, request); + const res = new OrganizationCollectionResponse(Collection.toView(req), groups); + return Response.success(res); + } catch (e) { + return Response.error(e); + } + } } diff --git a/src/program.ts b/src/program.ts index 76a943ffa8..08b098d33a 100644 --- a/src/program.ts +++ b/src/program.ts @@ -313,6 +313,7 @@ export class Program extends BaseProgram { .command('create [encodedJson]') .option('--file ', 'Path to file for attachment.') .option('--itemid ', 'Attachment\'s item id.') + .option('--organizationid ', 'Organization id for an organization object.') .description('Create an object in the vault.') .on('--help', () => { writeLn('\n Objects:'); @@ -344,6 +345,7 @@ export class Program extends BaseProgram { program .command('edit [encodedJson]') + .option('--organizationid ', 'Organization id for an organization object.') .description('Edit an object from the vault.') .on('--help', () => { writeLn('\n Objects:'); @@ -351,6 +353,7 @@ export class Program extends BaseProgram { writeLn(' item'); writeLn(' item-collections'); writeLn(' folder'); + writeLn(' org-collection'); writeLn(''); writeLn(' Id:'); writeLn(''); @@ -371,7 +374,8 @@ export class Program extends BaseProgram { }) .action(async (object, id, encodedJson, cmd) => { await this.exitIfLocked(); - const command = new EditCommand(this.main.cipherService, this.main.folderService); + const command = new EditCommand(this.main.cipherService, this.main.folderService, + this.main.cryptoService, this.main.apiService); const response = await command.run(object, id, encodedJson, cmd); this.processResponse(response); });