mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-08 19:18:02 +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;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toView(req: CardRequest) {
|
static toView(req: CardRequest, view = new CardView()) {
|
||||||
const view = new CardView();
|
|
||||||
view.cardholderName = req.cardholderName;
|
view.cardholderName = req.cardholderName;
|
||||||
view.brand = req.brand;
|
view.brand = req.brand;
|
||||||
view.number = req.number;
|
view.number = req.number;
|
||||||
|
@ -25,8 +25,7 @@ export class CipherRequest {
|
|||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toView(req: CipherRequest) {
|
static toView(req: CipherRequest, view = new CipherView()) {
|
||||||
const view = new CipherView();
|
|
||||||
view.type = req.type;
|
view.type = req.type;
|
||||||
view.folderId = req.folderId;
|
view.folderId = req.folderId;
|
||||||
view.organizationId = req.organizationId;
|
view.organizationId = req.organizationId;
|
||||||
|
@ -10,8 +10,7 @@ export class FieldRequest {
|
|||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toView(req: FieldRequest) {
|
static toView(req: FieldRequest, view = new FieldView()) {
|
||||||
const view = new FieldView();
|
|
||||||
view.type = req.type;
|
view.type = req.type;
|
||||||
view.value = req.value;
|
view.value = req.value;
|
||||||
view.name = req.name;
|
view.name = req.name;
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { FolderView } from 'jslib/models/view/folderView';
|
import { FolderView } from 'jslib/models/view/folderView';
|
||||||
|
|
||||||
export class FolderRequest {
|
export class FolderRequest {
|
||||||
static toView(req: FolderRequest) {
|
static toView(req: FolderRequest, view = new FolderView()) {
|
||||||
const view = new FolderView();
|
|
||||||
view.name = req.name;
|
view.name = req.name;
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,7 @@ export class IdentityRequest {
|
|||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toView(req: IdentityRequest) {
|
static toView(req: IdentityRequest, view = new IdentityView()) {
|
||||||
const view = new IdentityView();
|
|
||||||
view.title = req.title;
|
view.title = req.title;
|
||||||
view.firstName = req.firstName;
|
view.firstName = req.firstName;
|
||||||
view.middleName = req.middleName;
|
view.middleName = req.middleName;
|
||||||
|
@ -12,8 +12,7 @@ export class LoginRequest {
|
|||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toView(req: LoginRequest) {
|
static toView(req: LoginRequest, view = new LoginView()) {
|
||||||
const view = new LoginView();
|
|
||||||
if (req.uris != null) {
|
if (req.uris != null) {
|
||||||
view.uris = req.uris.map((u) => LoginUriRequest.toView(u));
|
view.uris = req.uris.map((u) => LoginUriRequest.toView(u));
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,7 @@ export class LoginUriRequest {
|
|||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toView(req: LoginUriRequest) {
|
static toView(req: LoginUriRequest, view = new LoginUriView()) {
|
||||||
const view = new LoginUriView();
|
|
||||||
view.uri = req.uri;
|
view.uri = req.uri;
|
||||||
view.match = req.match;
|
view.match = req.match;
|
||||||
return view;
|
return view;
|
||||||
|
@ -8,8 +8,7 @@ export class SecureNoteRequest {
|
|||||||
return req;
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toView(req: SecureNoteRequest) {
|
static toView(req: SecureNoteRequest, view = new SecureNoteView()) {
|
||||||
const view = new SecureNoteView();
|
|
||||||
view.type = req.type;
|
view.type = req.type;
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import { Main } from './bw';
|
|||||||
|
|
||||||
import { CreateCommand } from './commands/create.command';
|
import { CreateCommand } from './commands/create.command';
|
||||||
import { DeleteCommand } from './commands/delete.command';
|
import { DeleteCommand } from './commands/delete.command';
|
||||||
|
import { EditCommand } from './commands/edit.command';
|
||||||
import { EncodeCommand } from './commands/encode.command';
|
import { EncodeCommand } from './commands/encode.command';
|
||||||
import { GetCommand } from './commands/get.command';
|
import { GetCommand } from './commands/get.command';
|
||||||
import { ListCommand } from './commands/list.command';
|
import { ListCommand } from './commands/list.command';
|
||||||
@ -84,10 +85,12 @@ export class Program {
|
|||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('edit <object> <id>')
|
.command('edit <object> <id> <encodedData>')
|
||||||
.description('Edit an object.')
|
.description('Edit an object.')
|
||||||
.action((object, id, cmd) => {
|
.action(async (object, id, encodedData, cmd) => {
|
||||||
// TODO
|
const command = new EditCommand(this.main.cipherService, this.main.folderService);
|
||||||
|
const response = await command.run(object, id, encodedData, cmd);
|
||||||
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
Loading…
Reference in New Issue
Block a user