From 1d3ed93bff2bb418af4f66a7cbc920cd7a62760e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 15 May 2018 11:30:56 -0400 Subject: [PATCH] edit command --- src/commands/edit.command.ts | 66 +++++++++++++++++++++++++ src/models/request/cardRequest.ts | 3 +- src/models/request/cipherRequest.ts | 3 +- src/models/request/fieldRequest.ts | 3 +- src/models/request/folderRequest.ts | 3 +- src/models/request/identityRequest.ts | 3 +- src/models/request/loginRequest.ts | 3 +- src/models/request/loginUriRequest.ts | 3 +- src/models/request/secureNoteRequest.ts | 3 +- src/program.ts | 9 ++-- 10 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 src/commands/edit.command.ts diff --git a/src/commands/edit.command.ts b/src/commands/edit.command.ts new file mode 100644 index 0000000000..3647b34ac0 --- /dev/null +++ b/src/commands/edit.command.ts @@ -0,0 +1,66 @@ +import * as program from 'commander'; + +import { CipherService } from 'jslib/abstractions/cipher.service'; +import { FolderService } from 'jslib/services/folder.service'; + +import { Response } from '../models/response'; + +import { CipherRequest } from '../models/request/cipherRequest'; +import { FolderRequest } from '../models/request/folderRequest'; + +export class EditCommand { + constructor(private cipherService: CipherService, private folderService: FolderService) { } + + async run(object: string, id: string, requestData: string, cmd: program.Command): Promise { + let req: any = null; + try { + const reqJson = new Buffer(requestData, 'base64').toString(); + req = JSON.parse(reqJson); + } catch (e) { + return Response.badRequest('Error parsing the encoded request data.'); + } + + switch (object.toLowerCase()) { + case 'item': + return await this.editCipher(id, req); + case 'folder': + return await this.editFolder(id, req); + default: + return Response.badRequest('Unknown object.'); + } + } + + private async editCipher(id: string, req: CipherRequest) { + const cipher = await this.cipherService.get(id); + if (cipher == null) { + return Response.notFound(); + } + + let cipherView = await cipher.decrypt(); + cipherView = CipherRequest.toView(req, cipherView); + const encCipher = await this.cipherService.encrypt(cipherView); + try { + await this.cipherService.saveWithServer(encCipher); + return Response.success(); + } catch (e) { + return Response.error(e); + } + } + + private async editFolder(id: string, req: FolderRequest) { + const folder = await this.folderService.get(id); + if (folder == null) { + return Response.notFound(); + } + + let folderView = await folder.decrypt(); + folderView = FolderRequest.toView(req, folderView); + const encFolder = await this.folderService.encrypt(folderView); + try { + await this.folderService.saveWithServer(encFolder); + return Response.success(); + } catch (e) { + return Response.error(e); + } + } +} diff --git a/src/models/request/cardRequest.ts b/src/models/request/cardRequest.ts index 25053e7bed..f1359af61e 100644 --- a/src/models/request/cardRequest.ts +++ b/src/models/request/cardRequest.ts @@ -12,8 +12,7 @@ export class CardRequest { return req; } - static toView(req: CardRequest) { - const view = new CardView(); + static toView(req: CardRequest, view = new CardView()) { view.cardholderName = req.cardholderName; view.brand = req.brand; view.number = req.number; diff --git a/src/models/request/cipherRequest.ts b/src/models/request/cipherRequest.ts index f237d2ea0b..1fb9b31a06 100644 --- a/src/models/request/cipherRequest.ts +++ b/src/models/request/cipherRequest.ts @@ -25,8 +25,7 @@ export class CipherRequest { return req; } - static toView(req: CipherRequest) { - const view = new CipherView(); + static toView(req: CipherRequest, view = new CipherView()) { view.type = req.type; view.folderId = req.folderId; view.organizationId = req.organizationId; diff --git a/src/models/request/fieldRequest.ts b/src/models/request/fieldRequest.ts index 59952b43f8..3747ac378e 100644 --- a/src/models/request/fieldRequest.ts +++ b/src/models/request/fieldRequest.ts @@ -10,8 +10,7 @@ export class FieldRequest { return req; } - static toView(req: FieldRequest) { - const view = new FieldView(); + static toView(req: FieldRequest, view = new FieldView()) { view.type = req.type; view.value = req.value; view.name = req.name; diff --git a/src/models/request/folderRequest.ts b/src/models/request/folderRequest.ts index 2e969a98ee..93d6511caa 100644 --- a/src/models/request/folderRequest.ts +++ b/src/models/request/folderRequest.ts @@ -1,8 +1,7 @@ import { FolderView } from 'jslib/models/view/folderView'; export class FolderRequest { - static toView(req: FolderRequest) { - const view = new FolderView(); + static toView(req: FolderRequest, view = new FolderView()) { view.name = req.name; return view; } diff --git a/src/models/request/identityRequest.ts b/src/models/request/identityRequest.ts index 80fd09238d..bc62ede189 100644 --- a/src/models/request/identityRequest.ts +++ b/src/models/request/identityRequest.ts @@ -24,8 +24,7 @@ export class IdentityRequest { return req; } - static toView(req: IdentityRequest) { - const view = new IdentityView(); + static toView(req: IdentityRequest, view = new IdentityView()) { view.title = req.title; view.firstName = req.firstName; view.middleName = req.middleName; diff --git a/src/models/request/loginRequest.ts b/src/models/request/loginRequest.ts index 47251493c1..1917f21727 100644 --- a/src/models/request/loginRequest.ts +++ b/src/models/request/loginRequest.ts @@ -12,8 +12,7 @@ export class LoginRequest { return req; } - static toView(req: LoginRequest) { - const view = new LoginView(); + static toView(req: LoginRequest, view = new LoginView()) { if (req.uris != null) { view.uris = req.uris.map((u) => LoginUriRequest.toView(u)); } diff --git a/src/models/request/loginUriRequest.ts b/src/models/request/loginUriRequest.ts index 1bf43f0719..d645090b7f 100644 --- a/src/models/request/loginUriRequest.ts +++ b/src/models/request/loginUriRequest.ts @@ -9,8 +9,7 @@ export class LoginUriRequest { return req; } - static toView(req: LoginUriRequest) { - const view = new LoginUriView(); + static toView(req: LoginUriRequest, view = new LoginUriView()) { view.uri = req.uri; view.match = req.match; return view; diff --git a/src/models/request/secureNoteRequest.ts b/src/models/request/secureNoteRequest.ts index 24357d4cc3..790488cca1 100644 --- a/src/models/request/secureNoteRequest.ts +++ b/src/models/request/secureNoteRequest.ts @@ -8,8 +8,7 @@ export class SecureNoteRequest { return req; } - static toView(req: SecureNoteRequest) { - const view = new SecureNoteView(); + static toView(req: SecureNoteRequest, view = new SecureNoteView()) { view.type = req.type; return view; } diff --git a/src/program.ts b/src/program.ts index eea0804edf..d31eb46930 100644 --- a/src/program.ts +++ b/src/program.ts @@ -5,6 +5,7 @@ import { Main } from './bw'; import { CreateCommand } from './commands/create.command'; import { DeleteCommand } from './commands/delete.command'; +import { EditCommand } from './commands/edit.command'; import { EncodeCommand } from './commands/encode.command'; import { GetCommand } from './commands/get.command'; import { ListCommand } from './commands/list.command'; @@ -84,10 +85,12 @@ export class Program { }); program - .command('edit ') + .command('edit ') .description('Edit an object.') - .action((object, id, cmd) => { - // TODO + .action(async (object, id, encodedData, cmd) => { + const command = new EditCommand(this.main.cipherService, this.main.folderService); + const response = await command.run(object, id, encodedData, cmd); + this.processResponse(response); }); program