0
0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden synced 2024-11-12 13:01:51 +01:00
bitwarden_rs/src/api/core/folders.rs

126 lines
3.4 KiB
Rust
Raw Normal View History

use rocket::State;
use rocket_contrib::json::Json;
use serde_json::Value;
2018-02-10 01:00:55 +01:00
use db::DbConn;
use db::models::*;
use api::{JsonResult, EmptyResult, JsonUpcase, WebSocketUsers, UpdateType};
2018-02-10 01:00:55 +01:00
use auth::Headers;
use rocket::Route;
pub fn routes() -> Vec<Route> {
routes![
get_folders,
get_folder,
post_folders,
post_folder,
put_folder,
delete_folder_post,
delete_folder,
]
}
2018-02-10 01:00:55 +01:00
#[get("/folders")]
fn get_folders(headers: Headers, conn: DbConn) -> JsonResult {
2018-02-10 01:00:55 +01:00
let folders = Folder::find_by_user(&headers.user.uuid, &conn);
let folders_json: Vec<Value> = folders.iter().map(|c| c.to_json()).collect();
Ok(Json(json!({
"Data": folders_json,
"Object": "list",
"ContinuationToken": null,
2018-02-10 01:00:55 +01:00
})))
}
#[get("/folders/<uuid>")]
fn get_folder(uuid: String, headers: Headers, conn: DbConn) -> JsonResult {
2018-02-15 00:53:11 +01:00
let folder = match Folder::find_by_uuid(&uuid, &conn) {
2018-02-10 01:00:55 +01:00
Some(folder) => folder,
_ => err!("Invalid folder")
};
if folder.user_uuid != headers.user.uuid {
err!("Folder belongs to another user")
}
Ok(Json(folder.to_json()))
}
#[derive(Deserialize)]
#[allow(non_snake_case)]
pub struct FolderData {
pub Name: String
}
2018-02-10 01:00:55 +01:00
#[post("/folders", data = "<data>")]
fn post_folders(data: JsonUpcase<FolderData>, headers: Headers, conn: DbConn, ws: State<WebSocketUsers>) -> JsonResult {
let data: FolderData = data.into_inner().data;
2018-02-10 01:00:55 +01:00
let mut folder = Folder::new(headers.user.uuid.clone(), data.Name);
2018-02-10 01:00:55 +01:00
if folder.save(&conn).is_err() {
err!("Failed to save folder")
}
ws.send_folder_update(UpdateType::SyncFolderCreate, &folder);
2018-02-10 01:00:55 +01:00
Ok(Json(folder.to_json()))
}
#[post("/folders/<uuid>", data = "<data>")]
fn post_folder(uuid: String, data: JsonUpcase<FolderData>, headers: Headers, conn: DbConn, ws: State<WebSocketUsers>) -> JsonResult {
put_folder(uuid, data, headers, conn, ws)
2018-02-10 01:00:55 +01:00
}
#[put("/folders/<uuid>", data = "<data>")]
fn put_folder(uuid: String, data: JsonUpcase<FolderData>, headers: Headers, conn: DbConn, ws: State<WebSocketUsers>) -> JsonResult {
let data: FolderData = data.into_inner().data;
2018-02-10 01:00:55 +01:00
let mut folder = match Folder::find_by_uuid(&uuid, &conn) {
Some(folder) => folder,
_ => err!("Invalid folder")
};
if folder.user_uuid != headers.user.uuid {
err!("Folder belongs to another user")
}
folder.name = data.Name;
2018-02-10 01:00:55 +01:00
if folder.save(&conn).is_err() {
err!("Failed to save folder")
}
ws.send_folder_update(UpdateType::SyncFolderUpdate, &folder);
2018-02-10 01:00:55 +01:00
Ok(Json(folder.to_json()))
}
2018-02-17 23:38:55 +01:00
#[post("/folders/<uuid>/delete")]
fn delete_folder_post(uuid: String, headers: Headers, conn: DbConn, ws: State<WebSocketUsers>) -> EmptyResult {
delete_folder(uuid, headers, conn, ws)
2018-02-10 01:00:55 +01:00
}
#[delete("/folders/<uuid>")]
fn delete_folder(uuid: String, headers: Headers, conn: DbConn, ws: State<WebSocketUsers>) -> EmptyResult {
2018-02-10 01:00:55 +01:00
let folder = match Folder::find_by_uuid(&uuid, &conn) {
Some(folder) => folder,
_ => err!("Invalid folder")
};
if folder.user_uuid != headers.user.uuid {
err!("Folder belongs to another user")
}
// Delete the actual folder entry
match folder.delete(&conn) {
Ok(()) => {
ws.send_folder_update(UpdateType::SyncFolderDelete, &folder);
Ok(())
}
Err(_) => err!("Failed deleting folder")
}
2018-02-10 01:00:55 +01:00
}