2023-01-31 22:08:37 +01:00
|
|
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
2022-11-18 13:20:19 +01:00
|
|
|
|
|
|
|
import { Response } from "../models/response";
|
2020-04-14 19:04:19 +02:00
|
|
|
|
|
|
|
export class RestoreCommand {
|
|
|
|
constructor(private cipherService: CipherService) {}
|
|
|
|
|
2022-01-19 16:45:14 +01:00
|
|
|
async run(object: string, id: string): Promise<Response> {
|
2020-04-14 19:04:19 +02:00
|
|
|
if (id != null) {
|
|
|
|
id = id.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (object.toLowerCase()) {
|
|
|
|
case "item":
|
2022-01-19 16:45:14 +01:00
|
|
|
return await this.restoreCipher(id);
|
2020-04-14 19:04:19 +02:00
|
|
|
default:
|
|
|
|
return Response.badRequest("Unknown object.");
|
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2020-04-14 19:04:19 +02:00
|
|
|
|
2022-01-19 16:45:14 +01:00
|
|
|
private async restoreCipher(id: string) {
|
2020-04-14 19:04:19 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2020-04-14 19:04:19 +02:00
|
|
|
}
|