1
0
mirror of https://github.com/bitwarden/cli synced 2026-01-07 02:33:14 +00:00
Files
cli/src/commands/delete.command.ts
2021-10-06 16:23:57 -04:00

132 lines
4.7 KiB
TypeScript

import { ApiService } from 'jslib-common/abstractions/api.service';
import { CipherService } from 'jslib-common/abstractions/cipher.service';
import { FolderService } from 'jslib-common/abstractions/folder.service';
import { UserService } from 'jslib-common/abstractions/user.service';
import { Response } from 'jslib-node/cli/models/response';
import { Utils } from 'jslib-common/misc/utils';
import { CliUtils } from '../utils';
export class DeleteCommand {
constructor(private cipherService: CipherService, private folderService: FolderService,
private userService: UserService, private apiService: ApiService) { }
async run(object: string, id: string, cmdOptions: Record<string, any>): Promise<Response> {
if (id != null) {
id = id.toLowerCase();
}
const normalizedOptions = this.normalizeOptions(cmdOptions);
switch (object.toLowerCase()) {
case 'item':
return await this.deleteCipher(id, normalizedOptions);
case 'attachment':
return await this.deleteAttachment(id, normalizedOptions);
case 'folder':
return await this.deleteFolder(id);
case 'org-collection':
return await this.deleteOrganizationCollection(id, normalizedOptions);
default:
return Response.badRequest('Unknown object.');
}
}
private async deleteCipher(id: string, options: Options) {
const cipher = await this.cipherService.get(id);
if (cipher == null) {
return Response.notFound();
}
try {
if (options.permanent) {
await this.cipherService.deleteWithServer(id);
} else {
await this.cipherService.softDeleteWithServer(id);
}
return Response.success();
} catch (e) {
return Response.error(e);
}
}
private async deleteAttachment(id: string, options: Options) {
if (options.itemId == null || options.itemId === '') {
return Response.badRequest('`itemid` option is required.');
}
const itemId = options.itemId.toLowerCase();
const cipher = await this.cipherService.get(itemId);
if (cipher == null) {
return Response.notFound();
}
if (cipher.attachments == null || cipher.attachments.length === 0) {
return Response.error('No attachments available for this item.');
}
const attachments = cipher.attachments.filter(a => a.id.toLowerCase() === id);
if (attachments.length === 0) {
return Response.error('Attachment `' + id + '` was not found.');
}
if (cipher.organizationId == null && !(await this.userService.canAccessPremium())) {
return Response.error('Premium status is required to use this feature.');
}
try {
await this.cipherService.deleteAttachmentWithServer(cipher.id, attachments[0].id);
return Response.success();
} catch (e) {
return Response.error(e);
}
}
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) {
return Response.error(e);
}
}
private async deleteOrganizationCollection(id: string, options: Options) {
if (options.organizationId == null || options.organizationId === '') {
return Response.badRequest('`organizationid` option is required.');
}
if (!Utils.isGuid(id)) {
return Response.badRequest('`' + id + '` is not a GUID.');
}
if (!Utils.isGuid(options.organizationId)) {
return Response.badRequest('`' + options.organizationId + '` is not a GUID.');
}
try {
await this.apiService.deleteCollection(options.organizationId, id);
return Response.success();
} catch (e) {
return Response.error(e);
}
}
private normalizeOptions(passedOptions: Record<string, any>): Options {
const typedOptions = new Options();
typedOptions.organizationId = passedOptions.organizationid || passedOptions.organizationId;
typedOptions.itemId = passedOptions.itemid || passedOptions.itemId;
typedOptions.permanent = CliUtils.convertBooleanOption(passedOptions.permanent);
return typedOptions;
}
}
class Options {
itemId: string;
organizationId: string;
permanent: boolean;
}