diff --git a/jslib b/jslib index 53d08067df..034aefa652 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 53d08067df953e7a66ebf0b972e6443e1f275d86 +Subproject commit 034aefa652459c9ed5a660fe13593e314ee368dc diff --git a/src/commands/delete.command.ts b/src/commands/delete.command.ts index 3e891c67fe..58eeb4375d 100644 --- a/src/commands/delete.command.ts +++ b/src/commands/delete.command.ts @@ -1,14 +1,17 @@ import * as program from 'commander'; +import { ApiService } from 'jslib/abstractions/api.service'; import { CipherService } from 'jslib/abstractions/cipher.service'; import { FolderService } from 'jslib/abstractions/folder.service'; import { UserService } from 'jslib/abstractions/user.service'; import { Response } from 'jslib/cli/models/response'; +import { Utils } from 'jslib/misc/utils'; + export class DeleteCommand { constructor(private cipherService: CipherService, private folderService: FolderService, - private userService: UserService) { } + private userService: UserService, private apiService: ApiService) { } async run(object: string, id: string, cmd: program.Command): Promise { if (id != null) { @@ -22,6 +25,8 @@ export class DeleteCommand { return await this.deleteAttachment(id, cmd); case 'folder': return await this.deleteFolder(id); + case 'org-collection': + return await this.deleteOrganizationCollection(id, cmd); default: return Response.badRequest('Unknown object.'); } @@ -86,4 +91,22 @@ export class DeleteCommand { return Response.error(e); } } + + private async deleteOrganizationCollection(id: string, 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.'); + } + try { + await this.apiService.deleteCollection(cmd.organizationid, id); + return Response.success(); + } catch (e) { + return Response.error(e); + } + } } diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index 152ce49d12..9056a6ff6f 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -96,7 +96,7 @@ export class GetCommand { private async getCipherView(id: string): Promise { let decCipher: CipherView = null; - if (this.isGuid(id)) { + if (Utils.isGuid(id)) { const cipher = await this.cipherService.get(id); if (cipher != null) { decCipher = await cipher.decrypt(); @@ -285,7 +285,7 @@ export class GetCommand { private async getFolder(id: string) { let decFolder: FolderView = null; - if (this.isGuid(id)) { + if (Utils.isGuid(id)) { const folder = await this.folderService.get(id); if (folder != null) { decFolder = await folder.decrypt(); @@ -310,7 +310,7 @@ export class GetCommand { private async getCollection(id: string) { let decCollection: CollectionView = null; - if (this.isGuid(id)) { + if (Utils.isGuid(id)) { const collection = await this.collectionService.get(id); if (collection != null) { decCollection = await collection.decrypt(); @@ -337,10 +337,10 @@ export class GetCommand { if (cmd.organizationid == null || cmd.organizationid === '') { return Response.badRequest('--organizationid required.'); } - if (!this.isGuid(id)) { + if (!Utils.isGuid(id)) { return Response.error('`' + id + '` is not a GUID.'); } - if (!this.isGuid(cmd.organizationid)) { + if (!Utils.isGuid(cmd.organizationid)) { return Response.error('`' + cmd.organizationid + '` is not a GUID.'); } try { @@ -364,7 +364,7 @@ export class GetCommand { private async getOrganization(id: string) { let org: Organization = null; - if (this.isGuid(id)) { + if (Utils.isGuid(id)) { org = await this.userService.getOrganization(id); } else if (id.trim() !== '') { let orgs = await this.userService.getAllOrganizations(); @@ -384,10 +384,6 @@ export class GetCommand { return Response.success(res); } - private isGuid(id: string) { - return RegExp(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, 'i').test(id); - } - private async getTemplate(id: string) { let template: any = null; switch (id.toLowerCase()) { @@ -436,7 +432,7 @@ export class GetCommand { let fingerprint: string[] = null; if (id === 'me') { fingerprint = await this.cryptoService.getFingerprint(await this.userService.getUserId()); - } else if (this.isGuid(id)) { + } else if (Utils.isGuid(id)) { try { const response = await this.apiService.getUserPublicKey(id); const pubKey = Utils.fromB64ToArray(response.publicKey); diff --git a/src/program.ts b/src/program.ts index 9f272d68a7..76a943ffa8 100644 --- a/src/program.ts +++ b/src/program.ts @@ -277,6 +277,7 @@ export class Program extends BaseProgram { writeLn(' attachment'); writeLn(' folder'); writeLn(' collection'); + writeLn(' org-collection'); writeLn(' organization'); writeLn(' template'); writeLn(' fingerprint'); @@ -319,6 +320,7 @@ export class Program extends BaseProgram { writeLn(' item'); writeLn(' attachment'); writeLn(' folder'); + writeLn(' org-collection'); writeLn(''); writeLn(' Notes:'); writeLn(''); @@ -377,6 +379,7 @@ export class Program extends BaseProgram { program .command('delete ') .option('--itemid ', 'Attachment\'s item id.') + .option('--organizationid ', 'Organization id for an organization object.') .description('Delete an object from the vault.') .on('--help', () => { writeLn('\n Objects:'); @@ -384,6 +387,7 @@ export class Program extends BaseProgram { writeLn(' item'); writeLn(' attachment'); writeLn(' folder'); + writeLn(' org-collection'); writeLn(''); writeLn(' Id:'); writeLn(''); @@ -399,7 +403,7 @@ export class Program extends BaseProgram { .action(async (object, id, cmd) => { await this.exitIfLocked(); const command = new DeleteCommand(this.main.cipherService, this.main.folderService, - this.main.userService); + this.main.userService, this.main.apiService); const response = await command.run(object, id, cmd); this.processResponse(response); });