vw_small

Hardened fork of Vaultwarden (https://github.com/dani-garcia/vaultwarden) with fewer features.
git clone https://git.philomathiclife.com/repos/vw_small
Log | Files | Refs | README

folders.rs (2836B)


      1 use crate::{
      2     api::{EmptyResult, JsonResult},
      3     auth::Headers,
      4     db::{models::Folder, DbConn},
      5 };
      6 use rocket::serde::json::Json;
      7 use serde_json::Value;
      8 
      9 pub fn routes() -> Vec<rocket::Route> {
     10     routes![
     11         delete_folder,
     12         delete_folder_post,
     13         get_folder,
     14         get_folders,
     15         post_folder,
     16         post_folders,
     17         put_folder,
     18     ]
     19 }
     20 
     21 #[get("/folders")]
     22 async fn get_folders(headers: Headers, conn: DbConn) -> Json<Value> {
     23     let folders = Folder::find_by_user(&headers.user.uuid, &conn).await;
     24     let folders_json: Vec<Value> = folders.iter().map(Folder::to_json).collect();
     25     Json(json!({
     26       "data": folders_json,
     27       "object": "list",
     28       "continuationToken": null,
     29     }))
     30 }
     31 
     32 #[get("/folders/<uuid>")]
     33 async fn get_folder(uuid: &str, headers: Headers, conn: DbConn) -> JsonResult {
     34     let Some(folder) = Folder::find_by_uuid(uuid, &conn).await else {
     35         err!("Invalid folder")
     36     };
     37     if folder.user_uuid != headers.user.uuid {
     38         err!("Folder belongs to another user")
     39     }
     40     Ok(Json(folder.to_json()))
     41 }
     42 
     43 #[derive(Deserialize)]
     44 #[serde(rename_all = "camelCase")]
     45 pub struct FolderData {
     46     pub name: String,
     47     pub id: Option<String>,
     48 }
     49 
     50 #[post("/folders", data = "<data>")]
     51 async fn post_folders(data: Json<FolderData>, headers: Headers, conn: DbConn) -> JsonResult {
     52     let data: FolderData = data.into_inner();
     53     let mut folder = Folder::new(headers.user.uuid, data.name);
     54     folder.save(&conn).await?;
     55     Ok(Json(folder.to_json()))
     56 }
     57 
     58 #[post("/folders/<uuid>", data = "<data>")]
     59 async fn post_folder(
     60     uuid: &str,
     61     data: Json<FolderData>,
     62     headers: Headers,
     63     conn: DbConn,
     64 ) -> JsonResult {
     65     put_folder(uuid, data, headers, conn).await
     66 }
     67 
     68 #[put("/folders/<uuid>", data = "<data>")]
     69 async fn put_folder(
     70     uuid: &str,
     71     data: Json<FolderData>,
     72     headers: Headers,
     73     conn: DbConn,
     74 ) -> JsonResult {
     75     let data: FolderData = data.into_inner();
     76     let Some(mut folder) = Folder::find_by_uuid(uuid, &conn).await else {
     77         err!("Invalid folder")
     78     };
     79     if folder.user_uuid != headers.user.uuid {
     80         err!("Folder belongs to another user")
     81     }
     82     folder.name = data.name;
     83     folder.save(&conn).await?;
     84     Ok(Json(folder.to_json()))
     85 }
     86 
     87 #[post("/folders/<uuid>/delete")]
     88 async fn delete_folder_post(uuid: &str, headers: Headers, conn: DbConn) -> EmptyResult {
     89     delete_folder(uuid, headers, conn).await
     90 }
     91 
     92 #[delete("/folders/<uuid>")]
     93 async fn delete_folder(uuid: &str, headers: Headers, conn: DbConn) -> EmptyResult {
     94     let Some(folder) = Folder::find_by_uuid(uuid, &conn).await else {
     95         err!("Invalid folder")
     96     };
     97     if folder.user_uuid != headers.user.uuid {
     98         err!("Folder belongs to another user")
     99     }
    100     // Delete the actual folder entry
    101     folder.delete(&conn).await?;
    102     Ok(())
    103 }