bitwarden-estensione-browser/src/commands/delete.command.ts

90 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-05-14 21:08:48 +02:00
import * as program from 'commander';
import { CipherService } from 'jslib/abstractions/cipher.service';
import { FolderService } from 'jslib/abstractions/folder.service';
import { UserService } from 'jslib/abstractions/user.service';
2018-05-14 21:08:48 +02:00
2019-03-16 03:34:59 +01:00
import { Response } from 'jslib/cli/models/response';
2018-05-14 21:08:48 +02:00
export class DeleteCommand {
2018-05-18 16:55:50 +02:00
constructor(private cipherService: CipherService, private folderService: FolderService,
private userService: UserService) { }
2018-05-14 21:08:48 +02:00
async run(object: string, id: string, cmd: program.Command): Promise<Response> {
2018-05-16 17:54:59 +02:00
if (id != null) {
id = id.toLowerCase();
}
2018-05-14 22:25:14 +02:00
switch (object.toLowerCase()) {
2018-05-14 21:08:48 +02:00
case 'item':
2018-05-17 21:55:44 +02:00
return await this.deleteCipher(id);
case 'attachment':
return await this.deleteAttachment(id, cmd);
2018-05-14 21:08:48 +02:00
case 'folder':
return await this.deleteFolder(id);
default:
return Response.badRequest('Unknown object.');
}
}
private async deleteCipher(id: string) {
const cipher = await this.cipherService.get(id);
if (cipher == null) {
return Response.notFound();
}
try {
await this.cipherService.deleteWithServer(id);
return Response.success();
} catch (e) {
2018-05-15 17:08:55 +02:00
return Response.error(e);
2018-05-14 21:08:48 +02:00
}
}
2018-05-17 21:55:44 +02:00
private async deleteAttachment(id: string, cmd: program.Command) {
if (cmd.itemid == null || cmd.itemid === '') {
return Response.badRequest('--itemid <itemid> required.');
}
2018-05-17 19:43:53 +02:00
2018-05-17 21:55:44 +02:00
const itemId = cmd.itemid.toLowerCase();
const cipher = await this.cipherService.get(itemId);
if (cipher == null) {
2018-05-17 19:43:53 +02:00
return Response.notFound();
}
if (cipher.attachments == null || cipher.attachments.length === 0) {
return Response.error('No attachments available for this item.');
}
2018-05-17 21:55:44 +02:00
const attachments = cipher.attachments.filter((a) => a.id.toLowerCase() === id);
2018-05-17 19:43:53 +02:00
if (attachments.length === 0) {
2018-05-17 21:55:44 +02:00
return Response.error('Attachment `' + id + '` was not found.');
2018-05-17 19:43:53 +02:00
}
if (cipher.organizationId == null && !(await this.userService.canAccessPremium())) {
return Response.error('Premium status is required to use this feature.');
2018-05-18 16:55:50 +02:00
}
2018-05-17 19:43:53 +02:00
try {
2018-05-17 21:55:44 +02:00
await this.cipherService.deleteAttachmentWithServer(cipher.id, attachments[0].id);
2018-05-17 19:43:53 +02:00
return Response.success();
} catch (e) {
return Response.error(e);
}
}
2018-05-14 21:08:48 +02:00
private async deleteFolder(id: string) {
const folder = await this.folderService.get(id);
if (folder == null) {
return Response.notFound();
}
try {
await this.folderService.deleteWithServer(id);
return Response.success();
} catch (e) {
2018-05-15 17:08:55 +02:00
return Response.error(e);
2018-05-14 21:08:48 +02:00
}
}
}