mirror of
https://github.com/bitwarden/browser
synced 2025-12-19 01:33:33 +00:00
Added --trash to delete cmd, added restore cmd
This commit is contained in:
@@ -13,15 +13,16 @@ export class ConfigCommand {
|
||||
setting = setting.toLowerCase();
|
||||
switch (setting) {
|
||||
case 'server':
|
||||
return await this.getOrSetServer(value);
|
||||
return await this.getOrSetServer(value, cmd);
|
||||
default:
|
||||
return Response.badRequest('Unknown setting.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async getOrSetServer(url: string): Promise<Response> {
|
||||
if (url == null || url.trim() === '') {
|
||||
private async getOrSetServer(url: string, cmd: program.Command): Promise<Response> {
|
||||
if ((url == null || url.trim() === '') &&
|
||||
!cmd.webVault && !cmd.api && !cmd.identity && !cmd.icons && !cmd.notifications && !cmd.events) {
|
||||
const baseUrl = this.environmentService.baseUrl;
|
||||
const stringRes = new StringResponse(baseUrl == null ? 'https://bitwarden.com' : baseUrl);
|
||||
return Response.success(stringRes);
|
||||
@@ -30,6 +31,12 @@ export class ConfigCommand {
|
||||
url = (url === 'null' || url === 'bitwarden.com' || url === 'https://bitwarden.com' ? null : url);
|
||||
await this.environmentService.setUrls({
|
||||
base: url,
|
||||
webVault: cmd.webVault || null,
|
||||
api: cmd.api || null,
|
||||
identity: cmd.identity || null,
|
||||
icons: cmd.icons || null,
|
||||
notifications: cmd.notifications || null,
|
||||
events: cmd.events || null,
|
||||
});
|
||||
const res = new MessageResponse('Saved setting `config`.', null);
|
||||
return Response.success(res);
|
||||
|
||||
@@ -20,7 +20,7 @@ export class DeleteCommand {
|
||||
|
||||
switch (object.toLowerCase()) {
|
||||
case 'item':
|
||||
return await this.deleteCipher(id);
|
||||
return await this.deleteCipher(id, cmd);
|
||||
case 'attachment':
|
||||
return await this.deleteAttachment(id, cmd);
|
||||
case 'folder':
|
||||
@@ -32,14 +32,18 @@ export class DeleteCommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async deleteCipher(id: string) {
|
||||
private async deleteCipher(id: string, cmd: program.Command) {
|
||||
const cipher = await this.cipherService.get(id);
|
||||
if (cipher == null) {
|
||||
return Response.notFound();
|
||||
}
|
||||
|
||||
try {
|
||||
await this.cipherService.deleteWithServer(id);
|
||||
if (cmd.trash) {
|
||||
await this.cipherService.softDeleteWithServer(id);
|
||||
} else {
|
||||
await this.cipherService.deleteWithServer(id);
|
||||
}
|
||||
return Response.success();
|
||||
} catch (e) {
|
||||
return Response.error(e);
|
||||
|
||||
@@ -70,6 +70,9 @@ export class EditCommand {
|
||||
}
|
||||
|
||||
let cipherView = await cipher.decrypt();
|
||||
if (cipherView.isDeleted) {
|
||||
return Response.badRequest('You may not edit a deleted cipher. Use restore item <id> command first.');
|
||||
}
|
||||
cipherView = Cipher.toView(req, cipherView);
|
||||
const encCipher = await this.cipherService.encrypt(cipherView);
|
||||
try {
|
||||
|
||||
@@ -66,6 +66,9 @@ export class ListCommand {
|
||||
|
||||
if (cmd.folderid != null || cmd.collectionid != null || cmd.organizationid != null) {
|
||||
ciphers = ciphers.filter((c) => {
|
||||
if (cmd.trash && !c.isDeleted) {
|
||||
return false;
|
||||
}
|
||||
if (cmd.folderid != null) {
|
||||
if (cmd.folderid === 'notnull' && c.folderId != null) {
|
||||
return true;
|
||||
@@ -100,10 +103,12 @@ export class ListCommand {
|
||||
}
|
||||
return false;
|
||||
});
|
||||
} else if (cmd.search == null || cmd.search.trim() === '') {
|
||||
ciphers = ciphers.filter((c) => (cmd.trash || false) === c.isDeleted);
|
||||
}
|
||||
|
||||
if (cmd.search != null && cmd.search.trim() !== '') {
|
||||
ciphers = this.searchService.searchCiphersBasic(ciphers, cmd.search);
|
||||
ciphers = this.searchService.searchCiphersBasic(ciphers, cmd.search, cmd.trash || false);
|
||||
}
|
||||
|
||||
const res = new ListResponse(ciphers.map((o) => new CipherResponse(o)));
|
||||
|
||||
39
src/commands/restore.command.ts
Normal file
39
src/commands/restore.command.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import * as program from 'commander';
|
||||
|
||||
import { CipherService } from 'jslib/abstractions/cipher.service';
|
||||
|
||||
import { Response } from 'jslib/cli/models/response';
|
||||
|
||||
export class RestoreCommand {
|
||||
constructor(private cipherService: CipherService) { }
|
||||
|
||||
async run(object: string, id: string, cmd: program.Command): Promise<Response> {
|
||||
if (id != null) {
|
||||
id = id.toLowerCase();
|
||||
}
|
||||
|
||||
switch (object.toLowerCase()) {
|
||||
case 'item':
|
||||
return await this.restoreCipher(id, cmd);
|
||||
default:
|
||||
return Response.badRequest('Unknown object.');
|
||||
}
|
||||
}
|
||||
|
||||
private async restoreCipher(id: string, cmd: program.Command) {
|
||||
const cipher = await this.cipherService.get(id);
|
||||
if (cipher == null) {
|
||||
return Response.notFound();
|
||||
}
|
||||
if (cipher.deletedDate == null) {
|
||||
return Response.badRequest('Cipher is not in trash.');
|
||||
}
|
||||
|
||||
try {
|
||||
await this.cipherService.restoreWithServer(id);
|
||||
return Response.success();
|
||||
} catch (e) {
|
||||
return Response.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user