mirror of
https://github.com/bitwarden/browser.git
synced 2024-10-30 08:10:34 +01:00
edit command
This commit is contained in:
parent
83e91b70ff
commit
1d3ed93bff
66
src/commands/edit.command.ts
Normal file
66
src/commands/edit.command.ts
Normal file
@ -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<Response> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 <object> <id>')
|
||||
.command('edit <object> <id> <encodedData>')
|
||||
.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
|
||||
|
Loading…
Reference in New Issue
Block a user